@ray-js/t-agent-plugin-aistream 0.2.1-beta.3 → 0.2.2-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/dist/AIStreamTypes.d.ts +6 -7
- package/dist/ChatHistoryStore.d.ts +23 -2
- package/dist/ChatHistoryStore.js +191 -1
- package/dist/buildIn/withBuildIn.js +10 -10
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1 -2
- package/dist/utils/observer.d.ts +1 -5
- package/dist/utils/observer.js +14 -21
- package/dist/utils/sendMessage.js +3 -5
- package/dist/withAIStream.d.ts +8 -13
- package/dist/withAIStream.js +14 -45
- package/package.json +2 -2
- package/dist/ChatHistoryLocalStore.d.ts +0 -22
- package/dist/ChatHistoryLocalStore.js +0 -191
package/dist/AIStreamTypes.d.ts
CHANGED
|
@@ -1573,8 +1573,7 @@ export type ReceivedTextAsrPacket = ReceivedTextPacketBase<ReceivedTextPacketTyp
|
|
|
1573
1573
|
text: string;
|
|
1574
1574
|
}>;
|
|
1575
1575
|
export type ReceivedTextNlgPacket = ReceivedTextPacketBase<ReceivedTextPacketType.NLG, {
|
|
1576
|
-
|
|
1577
|
-
content?: string;
|
|
1576
|
+
content: string;
|
|
1578
1577
|
appendMode: 'append';
|
|
1579
1578
|
finish: boolean;
|
|
1580
1579
|
}>;
|
|
@@ -1602,17 +1601,17 @@ export type ReceivedSearchKnowledgeSkill = ReceivedTextSkillPacketBody<BuildInSk
|
|
|
1602
1601
|
};
|
|
1603
1602
|
}>;
|
|
1604
1603
|
export interface ReceivedSmartHomeSkillDevice {
|
|
1605
|
-
|
|
1606
|
-
|
|
1604
|
+
deviceId: string;
|
|
1605
|
+
icon: string;
|
|
1607
1606
|
dps?: Record<string, string>;
|
|
1608
|
-
|
|
1607
|
+
name: string;
|
|
1609
1608
|
room: string;
|
|
1610
1609
|
success: boolean;
|
|
1611
1610
|
}
|
|
1612
1611
|
export interface ReceivedSmartHomeSkillScene {
|
|
1613
1612
|
sceneId: string;
|
|
1614
|
-
|
|
1615
|
-
|
|
1613
|
+
name: string;
|
|
1614
|
+
icon: string;
|
|
1616
1615
|
type: number;
|
|
1617
1616
|
valid: boolean;
|
|
1618
1617
|
displayColor: string;
|
|
@@ -10,7 +10,7 @@ export interface StoredMessageObject {
|
|
|
10
10
|
homeId?: number;
|
|
11
11
|
indexId?: string;
|
|
12
12
|
}
|
|
13
|
-
|
|
13
|
+
interface Pagination {
|
|
14
14
|
sort?: 'asc' | 'desc';
|
|
15
15
|
offset: number;
|
|
16
16
|
limit: number;
|
|
@@ -30,7 +30,7 @@ export interface ChatHistoryStore<Key = number> {
|
|
|
30
30
|
/** 查询消息 */
|
|
31
31
|
query(id: Key): Promise<StoredMessageObject>;
|
|
32
32
|
/** 批量查询 */
|
|
33
|
-
queryAll(pagination:
|
|
33
|
+
queryAll(pagination: Pagination): Promise<{
|
|
34
34
|
total: number;
|
|
35
35
|
records: StoredMessageObject[];
|
|
36
36
|
}>;
|
|
@@ -45,3 +45,24 @@ export interface ChatHistoryStore<Key = number> {
|
|
|
45
45
|
id: Key;
|
|
46
46
|
}>;
|
|
47
47
|
}
|
|
48
|
+
export declare class ChatHistoryLocalStore implements ChatHistoryStore {
|
|
49
|
+
/** 版本号,会作为内部索引的一部分 */
|
|
50
|
+
private readonly version;
|
|
51
|
+
private options;
|
|
52
|
+
constructor(options: ChatHistoryStoreOptions);
|
|
53
|
+
private itemToMessage;
|
|
54
|
+
private getRecordIndex;
|
|
55
|
+
private getQueryCondition;
|
|
56
|
+
query(id: number): Promise<StoredMessageObject | null>;
|
|
57
|
+
queryAll(pagination: Pagination): Promise<{
|
|
58
|
+
total: number;
|
|
59
|
+
records: StoredMessageObject[];
|
|
60
|
+
}>;
|
|
61
|
+
update(id: number, body: ChatMessageObject): Promise<void>;
|
|
62
|
+
remove(id: number): Promise<void>;
|
|
63
|
+
removeAll(ids?: number[]): Promise<void>;
|
|
64
|
+
insert(message: ChatMessageObject): Promise<{
|
|
65
|
+
id: number;
|
|
66
|
+
}>;
|
|
67
|
+
}
|
|
68
|
+
export {};
|
package/dist/ChatHistoryStore.js
CHANGED
|
@@ -1 +1,191 @@
|
|
|
1
|
-
|
|
1
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
2
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
3
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
4
|
+
const _excluded = ["id"];
|
|
5
|
+
import "core-js/modules/es.array.sort.js";
|
|
6
|
+
import "core-js/modules/es.json.stringify.js";
|
|
7
|
+
import "core-js/modules/esnext.iterator.constructor.js";
|
|
8
|
+
import "core-js/modules/esnext.iterator.filter.js";
|
|
9
|
+
import "core-js/modules/esnext.iterator.map.js";
|
|
10
|
+
import { safeParseJSON } from '@ray-js/t-agent';
|
|
11
|
+
import { insertRecord, deleteRecordList, updateRecord, queryRecordList } from './utils';
|
|
12
|
+
import logger from './utils/logger';
|
|
13
|
+
export class ChatHistoryLocalStore {
|
|
14
|
+
constructor(options) {
|
|
15
|
+
/** 版本号,会作为内部索引的一部分 */
|
|
16
|
+
_defineProperty(this, "version", 'v1');
|
|
17
|
+
if (!options.agentId) {
|
|
18
|
+
throw new Error('agentId is required');
|
|
19
|
+
}
|
|
20
|
+
if (!options.bizCode) {
|
|
21
|
+
throw new Error('indexId is required');
|
|
22
|
+
}
|
|
23
|
+
this.options = _objectSpread({
|
|
24
|
+
indexId: 'default',
|
|
25
|
+
homeId: 0
|
|
26
|
+
}, options);
|
|
27
|
+
}
|
|
28
|
+
itemToMessage(item) {
|
|
29
|
+
const messageInfo = safeParseJSON(item.data);
|
|
30
|
+
if (!messageInfo) {
|
|
31
|
+
logger.error('ChatHistoryLocalStore failed parse item: ', item);
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
messageInfo.message.meta.id = item.id;
|
|
35
|
+
return {
|
|
36
|
+
id: item.id,
|
|
37
|
+
indexId: item.index2,
|
|
38
|
+
agentId: item.solutionCode,
|
|
39
|
+
deviceId: item.devId,
|
|
40
|
+
homeId: item.homeId,
|
|
41
|
+
createdAt: messageInfo.createdAt,
|
|
42
|
+
updatedAt: messageInfo.updatedAt,
|
|
43
|
+
message: messageInfo.message
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
getRecordIndex() {
|
|
47
|
+
return {
|
|
48
|
+
bizCode: this.options.bizCode,
|
|
49
|
+
solutionCode: this.options.agentId,
|
|
50
|
+
homeId: this.options.homeId,
|
|
51
|
+
index: "t-agent-".concat(this.version),
|
|
52
|
+
index2: this.options.indexId,
|
|
53
|
+
deviceId: this.options.deviceId
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
getQueryCondition() {
|
|
57
|
+
const indexes = this.getRecordIndex();
|
|
58
|
+
const params = {
|
|
59
|
+
bizCode: [indexes.bizCode],
|
|
60
|
+
solutionCode: [indexes.solutionCode],
|
|
61
|
+
homeId: [indexes.homeId],
|
|
62
|
+
index: [indexes.index],
|
|
63
|
+
index2: [indexes.index2]
|
|
64
|
+
};
|
|
65
|
+
if (indexes.deviceId) {
|
|
66
|
+
params.devId = [indexes.deviceId];
|
|
67
|
+
}
|
|
68
|
+
return params;
|
|
69
|
+
}
|
|
70
|
+
async query(id) {
|
|
71
|
+
if (typeof id !== 'number') {
|
|
72
|
+
logger.warn('ChatHistoryLocalStore query id is not number', {
|
|
73
|
+
id
|
|
74
|
+
});
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
const queryCondition = _objectSpread(_objectSpread({}, this.getQueryCondition()), {}, {
|
|
78
|
+
id: [id],
|
|
79
|
+
offset: 0,
|
|
80
|
+
limit: 1,
|
|
81
|
+
sortType: 1
|
|
82
|
+
});
|
|
83
|
+
const {
|
|
84
|
+
records
|
|
85
|
+
} = await queryRecordList(queryCondition);
|
|
86
|
+
return records[0] ? this.itemToMessage(records[0]) : null;
|
|
87
|
+
}
|
|
88
|
+
async queryAll(pagination) {
|
|
89
|
+
const queryCondition = _objectSpread(_objectSpread({}, this.getQueryCondition()), {}, {
|
|
90
|
+
offset: 0,
|
|
91
|
+
sortType: 1
|
|
92
|
+
});
|
|
93
|
+
const {
|
|
94
|
+
offset,
|
|
95
|
+
limit
|
|
96
|
+
} = pagination || {};
|
|
97
|
+
if (offset) {
|
|
98
|
+
queryCondition.offset = offset;
|
|
99
|
+
}
|
|
100
|
+
if (limit) {
|
|
101
|
+
queryCondition.limit = limit;
|
|
102
|
+
}
|
|
103
|
+
queryCondition.sortType = pagination.sort === 'desc' ? 0 : 1;
|
|
104
|
+
try {
|
|
105
|
+
const result = await queryRecordList(queryCondition);
|
|
106
|
+
const list = result.records.map(this.itemToMessage).filter(item => !!item);
|
|
107
|
+
return {
|
|
108
|
+
total: result.total,
|
|
109
|
+
records: list
|
|
110
|
+
};
|
|
111
|
+
} catch (error) {
|
|
112
|
+
logger.error('ChatHistoryLocalStore queryRecordList error', error);
|
|
113
|
+
return {
|
|
114
|
+
total: 0,
|
|
115
|
+
records: []
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async update(id, body) {
|
|
120
|
+
if (typeof id !== 'number') {
|
|
121
|
+
throw new Error('query id is not number');
|
|
122
|
+
}
|
|
123
|
+
const stored = await this.query(id);
|
|
124
|
+
if (!stored) {
|
|
125
|
+
throw new Error("could not find message: ".concat(id));
|
|
126
|
+
}
|
|
127
|
+
const nowTime = Date.now();
|
|
128
|
+
const msgObject = {
|
|
129
|
+
message: body,
|
|
130
|
+
createdAt: stored.createdAt,
|
|
131
|
+
updatedAt: nowTime
|
|
132
|
+
};
|
|
133
|
+
stored.message.meta.updatedAt = nowTime;
|
|
134
|
+
return updateRecord({
|
|
135
|
+
id,
|
|
136
|
+
data: JSON.stringify(msgObject)
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
async remove(id) {
|
|
140
|
+
if (typeof id !== 'number') {
|
|
141
|
+
throw new Error('query id is not number');
|
|
142
|
+
}
|
|
143
|
+
const stored = await this.query(id);
|
|
144
|
+
if (!stored) {
|
|
145
|
+
logger.warn('ChatHistoryLocalStore not find by id', {
|
|
146
|
+
id
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
await deleteRecordList({
|
|
150
|
+
id: [id]
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
async removeAll(ids) {
|
|
154
|
+
if (ids) {
|
|
155
|
+
if (!Array.isArray(ids)) {
|
|
156
|
+
throw new Error('ids is not array');
|
|
157
|
+
}
|
|
158
|
+
if (ids.length === 0) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
await deleteRecordList(_objectSpread({
|
|
162
|
+
id: ids
|
|
163
|
+
}, this.getQueryCondition()));
|
|
164
|
+
} else {
|
|
165
|
+
await deleteRecordList(this.getQueryCondition());
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async insert(message) {
|
|
169
|
+
const nowTime = Date.now();
|
|
170
|
+
|
|
171
|
+
// 先把 id 提取出来不存,id 始终是在数据库里自增的
|
|
172
|
+
const _message$meta = message.meta,
|
|
173
|
+
{
|
|
174
|
+
id
|
|
175
|
+
} = _message$meta,
|
|
176
|
+
meta = _objectWithoutProperties(_message$meta, _excluded);
|
|
177
|
+
const msgObject = {
|
|
178
|
+
message: _objectSpread(_objectSpread({}, message), {}, {
|
|
179
|
+
meta: _objectSpread(_objectSpread(_objectSpread({}, meta), this.options), {}, {
|
|
180
|
+
createdAt: nowTime,
|
|
181
|
+
updatedAt: nowTime
|
|
182
|
+
})
|
|
183
|
+
}),
|
|
184
|
+
createdAt: nowTime,
|
|
185
|
+
updatedAt: nowTime
|
|
186
|
+
};
|
|
187
|
+
return insertRecord(_objectSpread(_objectSpread({}, this.getRecordIndex()), {}, {
|
|
188
|
+
data: JSON.stringify(msgObject)
|
|
189
|
+
}));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -90,8 +90,8 @@ export function withBuildIn() {
|
|
|
90
90
|
data.sceneInfo.push({
|
|
91
91
|
type: scene.type,
|
|
92
92
|
enabled: scene.enable,
|
|
93
|
-
icon: scene.
|
|
94
|
-
name: scene.
|
|
93
|
+
icon: scene.icon,
|
|
94
|
+
name: scene.name,
|
|
95
95
|
sceneId: scene.sceneId,
|
|
96
96
|
intent: 'queryScene',
|
|
97
97
|
valid: scene.valid,
|
|
@@ -103,8 +103,8 @@ export function withBuildIn() {
|
|
|
103
103
|
for (const scene of content.general.data.scenes) {
|
|
104
104
|
data.sceneInfo.push({
|
|
105
105
|
type: scene.type,
|
|
106
|
-
icon: scene.
|
|
107
|
-
name: scene.
|
|
106
|
+
icon: scene.icon,
|
|
107
|
+
name: scene.name,
|
|
108
108
|
sceneId: scene.sceneId,
|
|
109
109
|
intent: 'controlScene',
|
|
110
110
|
valid: scene.valid,
|
|
@@ -115,9 +115,9 @@ export function withBuildIn() {
|
|
|
115
115
|
} else if (content.general.action === ReceivedSmartHomeSkillAction.QUERY_DEVICE) {
|
|
116
116
|
for (const dev of content.general.data.devices) {
|
|
117
117
|
data.deviceInfo.push({
|
|
118
|
-
icon: dev.
|
|
119
|
-
name: dev.
|
|
120
|
-
deviceId: dev.
|
|
118
|
+
icon: dev.icon,
|
|
119
|
+
name: dev.name,
|
|
120
|
+
deviceId: dev.deviceId,
|
|
121
121
|
intent: 'queryDevice',
|
|
122
122
|
room: dev.room,
|
|
123
123
|
success: dev.success
|
|
@@ -126,9 +126,9 @@ export function withBuildIn() {
|
|
|
126
126
|
} else if (content.general.action === ReceivedSmartHomeSkillAction.CONTROL_DEVICE) {
|
|
127
127
|
for (const dev of content.general.data.devices) {
|
|
128
128
|
data.deviceInfo.push({
|
|
129
|
-
icon: dev.
|
|
130
|
-
name: dev.
|
|
131
|
-
deviceId: dev.
|
|
129
|
+
icon: dev.icon,
|
|
130
|
+
name: dev.name,
|
|
131
|
+
deviceId: dev.deviceId,
|
|
132
132
|
intent: 'controlDevice',
|
|
133
133
|
success: null
|
|
134
134
|
});
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/utils/observer.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AudioBody, ConnectStateBody, EventBody, FileBody, ImageBody,
|
|
1
|
+
import { AudioBody, ConnectStateBody, EventBody, FileBody, ImageBody, SessionStateBody, TextBody, VideoBody } from '../AIStreamTypes';
|
|
2
2
|
export type AIStreamDataEntry = {
|
|
3
3
|
type: 'text';
|
|
4
4
|
body: TextBody;
|
|
@@ -23,9 +23,6 @@ export type AIStreamDataEntry = {
|
|
|
23
23
|
} | {
|
|
24
24
|
type: 'sessionState';
|
|
25
25
|
body: SessionStateBody;
|
|
26
|
-
} | {
|
|
27
|
-
type: 'amplitudes';
|
|
28
|
-
body: RecordAmplitudesBody;
|
|
29
26
|
};
|
|
30
27
|
export interface AIStreamObserverOptions {
|
|
31
28
|
sessionState?: boolean;
|
|
@@ -38,7 +35,6 @@ export interface AIStreamObserverOptions {
|
|
|
38
35
|
image?: boolean;
|
|
39
36
|
dataChannels?: string[];
|
|
40
37
|
sessionId?: string;
|
|
41
|
-
amplitudes?: boolean;
|
|
42
38
|
}
|
|
43
39
|
export declare class AIStreamObserverPool {
|
|
44
40
|
isStarted: boolean;
|
package/dist/utils/observer.js
CHANGED
|
@@ -3,9 +3,9 @@ import "core-js/modules/esnext.iterator.constructor.js";
|
|
|
3
3
|
import "core-js/modules/esnext.iterator.for-each.js";
|
|
4
4
|
import "core-js/modules/web.dom-collections.iterator.js";
|
|
5
5
|
import { ConnectState, EventType, SessionState, StreamFlag } from '../AIStreamTypes';
|
|
6
|
-
import { listenAudioReceived, listenConnectStateChanged, listenEventReceived, listenImageReceived,
|
|
6
|
+
import { listenAudioReceived, listenConnectStateChanged, listenEventReceived, listenImageReceived, listenSessionStateChanged, listenTextReceived } from './ttt';
|
|
7
7
|
import logger from './logger';
|
|
8
|
-
const types = ['connectionState', 'sessionState', 'event', 'text', 'audio', 'video', 'file', 'image'
|
|
8
|
+
const types = ['connectionState', 'sessionState', 'event', 'text', 'audio', 'video', 'file', 'image'];
|
|
9
9
|
export class AIStreamObserverPool {
|
|
10
10
|
constructor() {
|
|
11
11
|
_defineProperty(this, "isStarted", false);
|
|
@@ -18,8 +18,7 @@ export class AIStreamObserverPool {
|
|
|
18
18
|
audio: new Set(),
|
|
19
19
|
video: new Set(),
|
|
20
20
|
file: new Set(),
|
|
21
|
-
image: new Set()
|
|
22
|
-
amplitudes: new Set()
|
|
21
|
+
image: new Set()
|
|
23
22
|
};
|
|
24
23
|
}
|
|
25
24
|
start() {
|
|
@@ -27,21 +26,19 @@ export class AIStreamObserverPool {
|
|
|
27
26
|
return;
|
|
28
27
|
}
|
|
29
28
|
const handle = entry => {
|
|
29
|
+
var _entry$body;
|
|
30
30
|
const observers = this.observerMap[entry.type];
|
|
31
31
|
let state = '';
|
|
32
|
-
if (entry.type
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
} else if ((_entry$body = entry.body) !== null && _entry$body !== void 0 && _entry$body.streamFlag) {
|
|
41
|
-
state = "".concat(StreamFlag[entry.body.streamFlag]);
|
|
42
|
-
}
|
|
43
|
-
logger.debug("Pool received: %c".concat(observers.size ? '⊕' : '×').concat(entry.type, " ").concat(state), 'background: black; color: white', entry);
|
|
32
|
+
if (entry.type === 'connectionState') {
|
|
33
|
+
state = "".concat(ConnectState[entry.body.connectState], " ").concat(entry.body.code || '');
|
|
34
|
+
} else if (entry.type === 'sessionState') {
|
|
35
|
+
state = "".concat(SessionState[entry.body.sessionState], " ").concat(entry.body.code || '');
|
|
36
|
+
} else if (entry.type === 'event') {
|
|
37
|
+
state = "".concat(EventType[entry.body.eventType]);
|
|
38
|
+
} else if ((_entry$body = entry.body) !== null && _entry$body !== void 0 && _entry$body.streamFlag) {
|
|
39
|
+
state = "".concat(StreamFlag[entry.body.streamFlag]);
|
|
44
40
|
}
|
|
41
|
+
logger.debug("Pool received: %c".concat(observers.size ? '⊕' : '×').concat(entry.type, " ").concat(state), 'background: black; color: white', entry);
|
|
45
42
|
observers.forEach(observer => {
|
|
46
43
|
if (entry.type === 'text' || entry.type === 'audio' || entry.type === 'video' || entry.type === 'file' || entry.type === 'image') {
|
|
47
44
|
const {
|
|
@@ -81,9 +78,6 @@ export class AIStreamObserverPool {
|
|
|
81
78
|
})), listenImageReceived(body => handle({
|
|
82
79
|
type: 'image',
|
|
83
80
|
body
|
|
84
|
-
})), listenRecordAmplitudes(body => handle({
|
|
85
|
-
type: 'amplitudes',
|
|
86
|
-
body
|
|
87
81
|
}))
|
|
88
82
|
// listenVideoReceived(body => handle({ type: 'video', body })),
|
|
89
83
|
// listenFileReceived(body => handle({ type: 'file', body })),
|
|
@@ -102,8 +96,7 @@ export class AIStreamObserverPool {
|
|
|
102
96
|
audio: new Set(),
|
|
103
97
|
video: new Set(),
|
|
104
98
|
file: new Set(),
|
|
105
|
-
image: new Set()
|
|
106
|
-
amplitudes: new Set()
|
|
99
|
+
image: new Set()
|
|
107
100
|
};
|
|
108
101
|
}
|
|
109
102
|
connect(observer) {
|
|
@@ -144,13 +144,11 @@ export function sendBlocksToAIStream(params) {
|
|
|
144
144
|
}
|
|
145
145
|
const packet = safeParseJSON(data.body.text);
|
|
146
146
|
if (packet.bizType === ReceivedTextPacketType.NLG) {
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
logger.debug('sendBlocksToAIStream Receive NLG', delta);
|
|
150
|
-
const text = prevText + delta;
|
|
147
|
+
logger.debug('sendBlocksToAIStream Receive NLG', packet.data.content);
|
|
148
|
+
const text = prevText + packet.data.content;
|
|
151
149
|
enqueue({
|
|
152
150
|
type: 'text',
|
|
153
|
-
delta,
|
|
151
|
+
delta: packet.data.content,
|
|
154
152
|
text,
|
|
155
153
|
meta
|
|
156
154
|
});
|
package/dist/withAIStream.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { ChatAgent, ChatMessage,
|
|
2
|
-
import { TTTAction } from './utils';
|
|
1
|
+
import { ChatAgent, ChatMessage, GetChatPluginHandler, InputBlock } from '@ray-js/t-agent';
|
|
3
2
|
import { ConnectClientType, ReceivedTextSkillPacketBody } from './AIStreamTypes';
|
|
4
|
-
import {
|
|
3
|
+
import { ChatHistoryLocalStore, StoredMessageObject } from './ChatHistoryStore';
|
|
5
4
|
export interface AIStreamOptions {
|
|
6
5
|
/** client 类型: 1-作为设备代理, 2-作为 App */
|
|
7
6
|
clientType?: ConnectClientType;
|
|
@@ -25,8 +24,8 @@ export interface AIStreamOptions {
|
|
|
25
24
|
homeId?: number;
|
|
26
25
|
/** 是否在 onAgentStart 阶段就建立连接 */
|
|
27
26
|
earlyStart?: boolean;
|
|
28
|
-
/** 自定义消息存储, 返回的实例需要实现
|
|
29
|
-
createChatHistoryStore?: (agent: ChatAgent) =>
|
|
27
|
+
/** 自定义消息存储, 返回的实例需要实现 ChatHistoryLocalStore 接口, 返回null则不存储历史聊天记录 */
|
|
28
|
+
createChatHistoryStore?: (agent: ChatAgent) => ChatHistoryLocalStore | null;
|
|
30
29
|
/** 是否开启音频合成 */
|
|
31
30
|
enableTts?: boolean;
|
|
32
31
|
}
|
|
@@ -41,15 +40,12 @@ export interface AIStreamHooks {
|
|
|
41
40
|
onSkillsEnd: (skills: ReceivedTextSkillPacketBody[], respMsg: ChatMessage, result: {
|
|
42
41
|
messages: ChatMessage[];
|
|
43
42
|
}) => void;
|
|
44
|
-
onTTTAction: (tile: ChatTile | null, result: {
|
|
45
|
-
action?: TTTAction;
|
|
46
|
-
}) => void;
|
|
47
43
|
}
|
|
48
44
|
export declare function withAIStream(options?: AIStreamOptions): (agent: ChatAgent) => {
|
|
49
|
-
hooks: import("hookable").Hookable<
|
|
45
|
+
hooks: import("hookable").Hookable<any, string>;
|
|
50
46
|
aiStream: {
|
|
51
47
|
send: (blocks: InputBlock[], signal?: AbortSignal, extraOptions?: Record<string, any>) => {
|
|
52
|
-
response: import("@ray-js/t-agent").StreamResponse;
|
|
48
|
+
response: import("@ray-js/t-agent").StreamResponse;
|
|
53
49
|
metaPromise: Promise<Record<string, any>>;
|
|
54
50
|
};
|
|
55
51
|
chat: (blocks: InputBlock[], signal?: AbortSignal, options?: {
|
|
@@ -60,6 +56,8 @@ export declare function withAIStream(options?: AIStreamOptions): (agent: ChatAge
|
|
|
60
56
|
options: AIStreamOptions;
|
|
61
57
|
removeMessage: (message: ChatMessage) => Promise<void>;
|
|
62
58
|
clearAllMessages: () => Promise<void>;
|
|
59
|
+
onSkillCompose: (fn: AIStreamHooks['onSkillCompose']) => () => void;
|
|
60
|
+
onSkillsEnd: (fn: AIStreamHooks['onSkillsEnd']) => () => void;
|
|
63
61
|
feedback: ({ requestId, type }: {
|
|
64
62
|
requestId: string;
|
|
65
63
|
type: string;
|
|
@@ -67,8 +65,5 @@ export declare function withAIStream(options?: AIStreamOptions): (agent: ChatAge
|
|
|
67
65
|
thingjson?: any;
|
|
68
66
|
data: string;
|
|
69
67
|
}>;
|
|
70
|
-
onSkillCompose: (fn: AIStreamHooks['onSkillCompose']) => () => void;
|
|
71
|
-
onSkillsEnd: (fn: AIStreamHooks['onSkillsEnd']) => () => void;
|
|
72
|
-
onTTTAction: (fn: AIStreamHooks['onTTTAction']) => () => void;
|
|
73
68
|
};
|
|
74
69
|
};
|
package/dist/withAIStream.js
CHANGED
|
@@ -9,11 +9,11 @@ import "core-js/modules/esnext.iterator.map.js";
|
|
|
9
9
|
import "core-js/modules/web.dom-collections.iterator.js";
|
|
10
10
|
import { BubbleTileStatus, ChatMessageStatus, createHooks, EmitterEvent } from '@ray-js/t-agent';
|
|
11
11
|
import { messageAppraise } from './utils/apis';
|
|
12
|
-
import {
|
|
13
|
-
import { BizCode, ConnectClientType
|
|
12
|
+
import { getAccountInfo, getCurrentHomeInfo, runTTTAction, sendBlocksToAIStream } from './utils';
|
|
13
|
+
import { BizCode, ConnectClientType } from './AIStreamTypes';
|
|
14
|
+
import { ChatHistoryLocalStore } from './ChatHistoryStore';
|
|
14
15
|
import { DEFAULT_TOKEN_API, DEFAULT_TOKEN_API_VERSION, globalAIStreamClient } from './global';
|
|
15
16
|
import logger from './utils/logger';
|
|
16
|
-
import { ChatHistoryLocalStore } from './ChatHistoryLocalStore';
|
|
17
17
|
export function withAIStream() {
|
|
18
18
|
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
19
19
|
const hooks = createHooks();
|
|
@@ -38,24 +38,11 @@ export function withAIStream() {
|
|
|
38
38
|
const {
|
|
39
39
|
deviceId,
|
|
40
40
|
agentId,
|
|
41
|
-
clientType = ConnectClientType.APP,
|
|
42
41
|
tokenOptions = {
|
|
43
42
|
api: DEFAULT_TOKEN_API,
|
|
44
43
|
version: DEFAULT_TOKEN_API_VERSION
|
|
45
44
|
}
|
|
46
45
|
} = options;
|
|
47
|
-
if (!agentId) {
|
|
48
|
-
throw new Error('agentId is required');
|
|
49
|
-
}
|
|
50
|
-
if (!tokenOptions.api || !tokenOptions.version) {
|
|
51
|
-
throw new Error('tokenOptions.api and tokenOptions.version are required');
|
|
52
|
-
}
|
|
53
|
-
if (clientType === ConnectClientType.DEVICE && !deviceId) {
|
|
54
|
-
throw new Error('deviceId is required when clientType is device');
|
|
55
|
-
}
|
|
56
|
-
if (clientType === ConnectClientType.APP && deviceId) {
|
|
57
|
-
throw new Error('deviceId is not allowed when clientType is app');
|
|
58
|
-
}
|
|
59
46
|
let homeId = options.homeId;
|
|
60
47
|
if (!homeId) {
|
|
61
48
|
const info = await getCurrentHomeInfo();
|
|
@@ -69,7 +56,7 @@ export function withAIStream() {
|
|
|
69
56
|
/*
|
|
70
57
|
* 2. 获取历史消息
|
|
71
58
|
*/
|
|
72
|
-
let historyStore;
|
|
59
|
+
let historyStore = null;
|
|
73
60
|
if (typeof options.createChatHistoryStore === 'function') {
|
|
74
61
|
historyStore = options.createChatHistoryStore(agent);
|
|
75
62
|
} else {
|
|
@@ -100,18 +87,21 @@ export function withAIStream() {
|
|
|
100
87
|
|
|
101
88
|
// 创建 streamSession
|
|
102
89
|
const connection = globalAIStreamClient.getConnection({
|
|
103
|
-
clientType,
|
|
104
|
-
deviceId
|
|
90
|
+
clientType: options.clientType || ConnectClientType.APP,
|
|
91
|
+
deviceId: options.deviceId
|
|
105
92
|
});
|
|
93
|
+
if (options.clientType === ConnectClientType.DEVICE && !options.deviceId) {
|
|
94
|
+
throw new Error('deviceId is required when clientType is device');
|
|
95
|
+
}
|
|
106
96
|
const streamSession = connection.createSession({
|
|
107
|
-
ownerId: clientType === ConnectClientType.DEVICE ? deviceId : "".concat(homeId),
|
|
97
|
+
ownerId: options.clientType === ConnectClientType.DEVICE ? options.deviceId : "".concat(homeId),
|
|
108
98
|
api: tokenOptions.api,
|
|
109
99
|
apiVersion: tokenOptions.version,
|
|
110
100
|
solutionCode: agentId,
|
|
111
|
-
extParams: _objectSpread({
|
|
101
|
+
extParams: _objectSpread(_objectSpread({}, tokenOptions.extParams), {}, {
|
|
112
102
|
needTts: !!options.enableTts,
|
|
113
|
-
deviceId
|
|
114
|
-
}
|
|
103
|
+
deviceId: options.deviceId || undefined
|
|
104
|
+
})
|
|
115
105
|
});
|
|
116
106
|
await session.set('AIStream.streamSession', streamSession);
|
|
117
107
|
if (options.earlyStart) {
|
|
@@ -120,20 +110,6 @@ export function withAIStream() {
|
|
|
120
110
|
logger.error('earlyStart failed', error);
|
|
121
111
|
});
|
|
122
112
|
}
|
|
123
|
-
const observer = new AIStreamObserver(data => {
|
|
124
|
-
if (data.type === 'amplitudes') {
|
|
125
|
-
ui.emitEvent('amplitudes', data);
|
|
126
|
-
} else if (data.type === 'connectionState') {
|
|
127
|
-
ui.emitEvent('networkChange', {
|
|
128
|
-
online: data.body.connectState === ConnectState.CONNECTED
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
}, globalAIStreamClient.pool);
|
|
132
|
-
observer.observe({
|
|
133
|
-
connectionState: true,
|
|
134
|
-
amplitudes: true
|
|
135
|
-
});
|
|
136
|
-
await session.set('AIStream.observer', observer);
|
|
137
113
|
});
|
|
138
114
|
const getHistoryStore = () => {
|
|
139
115
|
const historyStore = session.get('AIStream.historyStore');
|
|
@@ -415,10 +391,6 @@ export function withAIStream() {
|
|
|
415
391
|
};
|
|
416
392
|
});
|
|
417
393
|
onAgentDispose(async () => {
|
|
418
|
-
const observer = session.get('AIStream.observer');
|
|
419
|
-
if (observer) {
|
|
420
|
-
observer.disconnect();
|
|
421
|
-
}
|
|
422
394
|
const streamSession = session.get('AIStream.streamSession');
|
|
423
395
|
|
|
424
396
|
/**
|
|
@@ -497,16 +469,13 @@ export function withAIStream() {
|
|
|
497
469
|
await historyStore.removeAll();
|
|
498
470
|
}
|
|
499
471
|
},
|
|
500
|
-
feedback,
|
|
501
472
|
onSkillCompose: fn => {
|
|
502
473
|
return hooks.hook('onSkillCompose', fn);
|
|
503
474
|
},
|
|
504
475
|
onSkillsEnd: fn => {
|
|
505
476
|
return hooks.hook('onSkillsEnd', fn);
|
|
506
477
|
},
|
|
507
|
-
|
|
508
|
-
return hooks.hook('onTTTAction', fn);
|
|
509
|
-
}
|
|
478
|
+
feedback
|
|
510
479
|
}
|
|
511
480
|
};
|
|
512
481
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/t-agent-plugin-aistream",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2-beta-1",
|
|
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": "0dd513ad93a351ad5c00a1ee8993c7b62f03640f"
|
|
39
39
|
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { ChatMessageObject } from '@ray-js/t-agent';
|
|
2
|
-
import { ChatHistoryStore, ChatHistoryStoreOptions, StoredMessageObject, ChatHistoryStorePagination } from './ChatHistoryStore';
|
|
3
|
-
export declare class ChatHistoryLocalStore implements ChatHistoryStore {
|
|
4
|
-
/** 版本号,会作为内部索引的一部分 */
|
|
5
|
-
private readonly version;
|
|
6
|
-
private options;
|
|
7
|
-
constructor(options: ChatHistoryStoreOptions);
|
|
8
|
-
private itemToMessage;
|
|
9
|
-
private getRecordIndex;
|
|
10
|
-
private getQueryCondition;
|
|
11
|
-
query(id: number): Promise<StoredMessageObject | null>;
|
|
12
|
-
queryAll(pagination: ChatHistoryStorePagination): Promise<{
|
|
13
|
-
total: number;
|
|
14
|
-
records: StoredMessageObject[];
|
|
15
|
-
}>;
|
|
16
|
-
update(id: number, body: ChatMessageObject): Promise<void>;
|
|
17
|
-
remove(id: number): Promise<void>;
|
|
18
|
-
removeAll(ids?: number[]): Promise<void>;
|
|
19
|
-
insert(message: ChatMessageObject): Promise<{
|
|
20
|
-
id: number;
|
|
21
|
-
}>;
|
|
22
|
-
}
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
2
|
-
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
3
|
-
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
4
|
-
const _excluded = ["id"];
|
|
5
|
-
import "core-js/modules/es.array.sort.js";
|
|
6
|
-
import "core-js/modules/es.json.stringify.js";
|
|
7
|
-
import "core-js/modules/esnext.iterator.constructor.js";
|
|
8
|
-
import "core-js/modules/esnext.iterator.filter.js";
|
|
9
|
-
import "core-js/modules/esnext.iterator.map.js";
|
|
10
|
-
import { safeParseJSON } from '@ray-js/t-agent';
|
|
11
|
-
import logger from './utils/logger';
|
|
12
|
-
import { deleteRecordList, insertRecord, queryRecordList, updateRecord } from './utils';
|
|
13
|
-
export class ChatHistoryLocalStore {
|
|
14
|
-
constructor(options) {
|
|
15
|
-
/** 版本号,会作为内部索引的一部分 */
|
|
16
|
-
_defineProperty(this, "version", 'v1');
|
|
17
|
-
if (!options.agentId) {
|
|
18
|
-
throw new Error('agentId is required');
|
|
19
|
-
}
|
|
20
|
-
if (!options.bizCode) {
|
|
21
|
-
throw new Error('bizCode is required');
|
|
22
|
-
}
|
|
23
|
-
this.options = _objectSpread({
|
|
24
|
-
indexId: 'default',
|
|
25
|
-
homeId: 0
|
|
26
|
-
}, options);
|
|
27
|
-
}
|
|
28
|
-
itemToMessage(item) {
|
|
29
|
-
const messageInfo = safeParseJSON(item.data);
|
|
30
|
-
if (!messageInfo) {
|
|
31
|
-
logger.error('ChatHistoryLocalStore failed parse item: ', item);
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
messageInfo.message.meta.id = item.id;
|
|
35
|
-
return {
|
|
36
|
-
id: item.id,
|
|
37
|
-
indexId: item.index2,
|
|
38
|
-
agentId: item.solutionCode,
|
|
39
|
-
deviceId: item.devId,
|
|
40
|
-
homeId: item.homeId,
|
|
41
|
-
createdAt: messageInfo.createdAt,
|
|
42
|
-
updatedAt: messageInfo.updatedAt,
|
|
43
|
-
message: messageInfo.message
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
getRecordIndex() {
|
|
47
|
-
return {
|
|
48
|
-
bizCode: this.options.bizCode,
|
|
49
|
-
solutionCode: this.options.agentId,
|
|
50
|
-
homeId: this.options.homeId,
|
|
51
|
-
index: "t-agent-".concat(this.version),
|
|
52
|
-
index2: this.options.indexId,
|
|
53
|
-
devId: this.options.deviceId
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
getQueryCondition() {
|
|
57
|
-
const indexes = this.getRecordIndex();
|
|
58
|
-
const params = {
|
|
59
|
-
bizCode: [indexes.bizCode],
|
|
60
|
-
solutionCode: [indexes.solutionCode],
|
|
61
|
-
homeId: [indexes.homeId],
|
|
62
|
-
index: [indexes.index],
|
|
63
|
-
index2: [indexes.index2]
|
|
64
|
-
};
|
|
65
|
-
if (indexes.devId) {
|
|
66
|
-
params.devId = [indexes.devId];
|
|
67
|
-
}
|
|
68
|
-
return params;
|
|
69
|
-
}
|
|
70
|
-
async query(id) {
|
|
71
|
-
if (typeof id !== 'number') {
|
|
72
|
-
logger.warn('ChatHistoryLocalStore query id is not number', {
|
|
73
|
-
id
|
|
74
|
-
});
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
const queryCondition = _objectSpread(_objectSpread({}, this.getQueryCondition()), {}, {
|
|
78
|
-
id: [id],
|
|
79
|
-
offset: 0,
|
|
80
|
-
limit: 1,
|
|
81
|
-
sortType: 1
|
|
82
|
-
});
|
|
83
|
-
const {
|
|
84
|
-
records
|
|
85
|
-
} = await queryRecordList(queryCondition);
|
|
86
|
-
return records[0] ? this.itemToMessage(records[0]) : null;
|
|
87
|
-
}
|
|
88
|
-
async queryAll(pagination) {
|
|
89
|
-
const queryCondition = _objectSpread(_objectSpread({}, this.getQueryCondition()), {}, {
|
|
90
|
-
offset: 0,
|
|
91
|
-
sortType: 1
|
|
92
|
-
});
|
|
93
|
-
const {
|
|
94
|
-
offset,
|
|
95
|
-
limit
|
|
96
|
-
} = pagination || {};
|
|
97
|
-
if (offset) {
|
|
98
|
-
queryCondition.offset = offset;
|
|
99
|
-
}
|
|
100
|
-
if (limit) {
|
|
101
|
-
queryCondition.limit = limit;
|
|
102
|
-
}
|
|
103
|
-
queryCondition.sortType = pagination.sort === 'desc' ? 0 : 1;
|
|
104
|
-
try {
|
|
105
|
-
const result = await queryRecordList(queryCondition);
|
|
106
|
-
const list = result.records.map(this.itemToMessage).filter(item => !!item);
|
|
107
|
-
return {
|
|
108
|
-
total: result.total,
|
|
109
|
-
records: list
|
|
110
|
-
};
|
|
111
|
-
} catch (error) {
|
|
112
|
-
logger.error('ChatHistoryLocalStore queryRecordList error', error);
|
|
113
|
-
return {
|
|
114
|
-
total: 0,
|
|
115
|
-
records: []
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
async update(id, body) {
|
|
120
|
-
if (typeof id !== 'number') {
|
|
121
|
-
throw new Error('query id is not number');
|
|
122
|
-
}
|
|
123
|
-
const stored = await this.query(id);
|
|
124
|
-
if (!stored) {
|
|
125
|
-
throw new Error("could not find message: ".concat(id));
|
|
126
|
-
}
|
|
127
|
-
const nowTime = Date.now();
|
|
128
|
-
const msgObject = {
|
|
129
|
-
message: body,
|
|
130
|
-
createdAt: stored.createdAt,
|
|
131
|
-
updatedAt: nowTime
|
|
132
|
-
};
|
|
133
|
-
stored.message.meta.updatedAt = nowTime;
|
|
134
|
-
return updateRecord({
|
|
135
|
-
id,
|
|
136
|
-
data: JSON.stringify(msgObject)
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
async remove(id) {
|
|
140
|
-
if (typeof id !== 'number') {
|
|
141
|
-
throw new Error('query id is not number');
|
|
142
|
-
}
|
|
143
|
-
const stored = await this.query(id);
|
|
144
|
-
if (!stored) {
|
|
145
|
-
logger.warn('ChatHistoryLocalStore not find by id', {
|
|
146
|
-
id
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
await deleteRecordList({
|
|
150
|
-
id: [id]
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
async removeAll(ids) {
|
|
154
|
-
if (ids) {
|
|
155
|
-
if (!Array.isArray(ids)) {
|
|
156
|
-
throw new Error('ids is not array');
|
|
157
|
-
}
|
|
158
|
-
if (ids.length === 0) {
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
await deleteRecordList(_objectSpread({
|
|
162
|
-
id: ids
|
|
163
|
-
}, this.getQueryCondition()));
|
|
164
|
-
} else {
|
|
165
|
-
await deleteRecordList(this.getQueryCondition());
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
async insert(message) {
|
|
169
|
-
const nowTime = Date.now();
|
|
170
|
-
|
|
171
|
-
// 先把 id 提取出来不存,id 始终是在数据库里自增的
|
|
172
|
-
const _message$meta = message.meta,
|
|
173
|
-
{
|
|
174
|
-
id
|
|
175
|
-
} = _message$meta,
|
|
176
|
-
meta = _objectWithoutProperties(_message$meta, _excluded);
|
|
177
|
-
const msgObject = {
|
|
178
|
-
message: _objectSpread(_objectSpread({}, message), {}, {
|
|
179
|
-
meta: _objectSpread(_objectSpread(_objectSpread({}, meta), this.options), {}, {
|
|
180
|
-
createdAt: nowTime,
|
|
181
|
-
updatedAt: nowTime
|
|
182
|
-
})
|
|
183
|
-
}),
|
|
184
|
-
createdAt: nowTime,
|
|
185
|
-
updatedAt: nowTime
|
|
186
|
-
};
|
|
187
|
-
return insertRecord(_objectSpread(_objectSpread({}, this.getRecordIndex()), {}, {
|
|
188
|
-
data: JSON.stringify(msgObject)
|
|
189
|
-
}));
|
|
190
|
-
}
|
|
191
|
-
}
|