@ray-js/t-agent-plugin-aistream 0.2.2-beta-2 → 0.2.3-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 +2 -1
- package/dist/ChatHistoryLocalStore.d.ts +22 -0
- package/dist/ChatHistoryLocalStore.js +191 -0
- package/dist/ChatHistoryStore.d.ts +2 -23
- package/dist/ChatHistoryStore.js +1 -191
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/utils/AIStream.d.ts +0 -1
- package/dist/utils/AIStream.js +1 -11
- package/dist/utils/sendMessage.js +6 -7
- package/dist/withAIStream.d.ts +13 -8
- package/dist/withAIStream.js +25 -12
- package/package.json +2 -2
package/dist/AIStreamTypes.d.ts
CHANGED
|
@@ -1573,7 +1573,8 @@ export type ReceivedTextAsrPacket = ReceivedTextPacketBase<ReceivedTextPacketTyp
|
|
|
1573
1573
|
text: string;
|
|
1574
1574
|
}>;
|
|
1575
1575
|
export type ReceivedTextNlgPacket = ReceivedTextPacketBase<ReceivedTextPacketType.NLG, {
|
|
1576
|
-
|
|
1576
|
+
reasoningContent?: string;
|
|
1577
|
+
content?: string;
|
|
1577
1578
|
appendMode: 'append';
|
|
1578
1579
|
finish: boolean;
|
|
1579
1580
|
}>;
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
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
|
+
}
|
|
@@ -10,7 +10,7 @@ export interface StoredMessageObject {
|
|
|
10
10
|
homeId?: number;
|
|
11
11
|
indexId?: string;
|
|
12
12
|
}
|
|
13
|
-
interface
|
|
13
|
+
export interface ChatHistoryStorePagination {
|
|
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: ChatHistoryStorePagination): Promise<{
|
|
34
34
|
total: number;
|
|
35
35
|
records: StoredMessageObject[];
|
|
36
36
|
}>;
|
|
@@ -45,24 +45,3 @@ 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,191 +1 @@
|
|
|
1
|
-
|
|
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
|
-
}
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/utils/AIStream.d.ts
CHANGED
package/dist/utils/AIStream.js
CHANGED
|
@@ -8,7 +8,7 @@ import "core-js/modules/esnext.iterator.for-each.js";
|
|
|
8
8
|
import "core-js/modules/esnext.iterator.map.js";
|
|
9
9
|
import "core-js/modules/web.dom-collections.iterator.js";
|
|
10
10
|
import { AIStreamAttributeType, AIStreamErrorCode, AIStreamServerErrorCode, BizTag, ConnectClientType, ConnectState, EventType, NetworkType, SessionState } from '../AIStreamTypes';
|
|
11
|
-
import { closeSession, connect, createSession, disconnect, getCurrentHomeInfo, getNetworkType, isConnected, queryAgentToken,
|
|
11
|
+
import { closeSession, connect, createSession, disconnect, getCurrentHomeInfo, getNetworkType, isConnected, queryAgentToken, sendEventChatBreak, sendEventEnd, sendEventPayloadEnd, sendEventStart, sendImageData, sendTextData, startRecordAndSendAudioData, stopRecordAndSendAudioData } from './ttt';
|
|
12
12
|
import { AIStreamObserver, AIStreamObserverPool } from './observer';
|
|
13
13
|
import { isAbortError } from '@ray-js/t-agent';
|
|
14
14
|
import logger from './logger';
|
|
@@ -456,11 +456,6 @@ export class AIStreamEvent {
|
|
|
456
456
|
}
|
|
457
457
|
stream.started = true;
|
|
458
458
|
if (source.type === 'audio') {
|
|
459
|
-
if (source.amplitudeCount) {
|
|
460
|
-
registerRecordAmplitudes({
|
|
461
|
-
count: source.amplitudeCount
|
|
462
|
-
});
|
|
463
|
-
}
|
|
464
459
|
startPromise = startRecordAndSendAudioData({
|
|
465
460
|
sessionId: this.sessionId,
|
|
466
461
|
dataChannel,
|
|
@@ -486,11 +481,6 @@ export class AIStreamEvent {
|
|
|
486
481
|
startPromise = null;
|
|
487
482
|
}
|
|
488
483
|
if (source.type === 'audio') {
|
|
489
|
-
if (source.amplitudeCount) {
|
|
490
|
-
unregisterVoiceAmplitudes({
|
|
491
|
-
count: source.amplitudeCount
|
|
492
|
-
});
|
|
493
|
-
}
|
|
494
484
|
stopRecordAndSendAudioData({
|
|
495
485
|
sessionId: this.sessionId,
|
|
496
486
|
dataChannel,
|
|
@@ -22,14 +22,12 @@ export function sendBlocksToAIStream(params) {
|
|
|
22
22
|
signal
|
|
23
23
|
} = params;
|
|
24
24
|
let audioEmitter = null;
|
|
25
|
-
let amplitudeCount = 0;
|
|
26
25
|
for (const block of blocks) {
|
|
27
26
|
if (block.type === 'audio') {
|
|
28
27
|
if (audioEmitter) {
|
|
29
28
|
throw new Error('only one audio emitter is allowed');
|
|
30
29
|
}
|
|
31
30
|
audioEmitter = block.audio_emitter;
|
|
32
|
-
amplitudeCount = block.amplitude_count || 0;
|
|
33
31
|
}
|
|
34
32
|
}
|
|
35
33
|
const attribute = _objectSpread({
|
|
@@ -144,11 +142,13 @@ export function sendBlocksToAIStream(params) {
|
|
|
144
142
|
}
|
|
145
143
|
const packet = safeParseJSON(data.body.text);
|
|
146
144
|
if (packet.bizType === ReceivedTextPacketType.NLG) {
|
|
147
|
-
|
|
148
|
-
const
|
|
145
|
+
// TODO: 处理 reasoningContent 推理能力
|
|
146
|
+
const delta = packet.data.content || '';
|
|
147
|
+
logger.debug('sendBlocksToAIStream Receive NLG', delta);
|
|
148
|
+
const text = prevText + delta;
|
|
149
149
|
enqueue({
|
|
150
150
|
type: 'text',
|
|
151
|
-
delta
|
|
151
|
+
delta,
|
|
152
152
|
text,
|
|
153
153
|
meta
|
|
154
154
|
});
|
|
@@ -291,8 +291,7 @@ export function sendBlocksToAIStream(params) {
|
|
|
291
291
|
if (audioEmitter) {
|
|
292
292
|
await new Promise(resolve => {
|
|
293
293
|
const s = event.stream({
|
|
294
|
-
type: 'audio'
|
|
295
|
-
amplitudeCount
|
|
294
|
+
type: 'audio'
|
|
296
295
|
});
|
|
297
296
|
audioEmitter.addEventListener('confirm', async () => {
|
|
298
297
|
if (!canceled) {
|
package/dist/withAIStream.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { ChatAgent, ChatCardObject, ChatMessage, GetChatPluginHandler, InputBlock } from '@ray-js/t-agent';
|
|
1
|
+
import { ChatAgent, ChatCardObject, ChatMessage, ChatTile, GetChatPluginHandler, InputBlock } from '@ray-js/t-agent';
|
|
2
|
+
import { TTTAction } from './utils';
|
|
2
3
|
import { ConnectClientType, ReceivedTextSkillPacketBody } from './AIStreamTypes';
|
|
3
|
-
import {
|
|
4
|
+
import { ChatHistoryStore, StoredMessageObject } from './ChatHistoryStore';
|
|
4
5
|
export interface AIStreamOptions {
|
|
5
6
|
/** client 类型: 1-作为设备代理, 2-作为 App */
|
|
6
7
|
clientType?: ConnectClientType;
|
|
@@ -24,8 +25,8 @@ export interface AIStreamOptions {
|
|
|
24
25
|
homeId?: number;
|
|
25
26
|
/** 是否在 onAgentStart 阶段就建立连接 */
|
|
26
27
|
earlyStart?: boolean;
|
|
27
|
-
/** 自定义消息存储, 返回的实例需要实现
|
|
28
|
-
createChatHistoryStore?: (agent: ChatAgent) =>
|
|
28
|
+
/** 自定义消息存储, 返回的实例需要实现 ChatHistoryStore 接口, 返回null则不存储历史聊天记录 */
|
|
29
|
+
createChatHistoryStore?: (agent: ChatAgent) => ChatHistoryStore | null;
|
|
29
30
|
/** 是否开启音频合成 */
|
|
30
31
|
enableTts?: boolean;
|
|
31
32
|
}
|
|
@@ -40,12 +41,15 @@ export interface AIStreamHooks {
|
|
|
40
41
|
onSkillsEnd: (skills: ReceivedTextSkillPacketBody[], respMsg: ChatMessage, result: {
|
|
41
42
|
messages: ChatMessage[];
|
|
42
43
|
}) => void;
|
|
44
|
+
onTTTAction: (tile: ChatTile | null, result: {
|
|
45
|
+
action?: TTTAction;
|
|
46
|
+
}) => void;
|
|
43
47
|
onCardsReceived: (skills: ReceivedTextSkillPacketBody[], result: {
|
|
44
48
|
cards: ChatCardObject[];
|
|
45
49
|
}) => void;
|
|
46
50
|
}
|
|
47
51
|
export declare function withAIStream(options?: AIStreamOptions): (agent: ChatAgent) => {
|
|
48
|
-
hooks: import("hookable").Hookable<
|
|
52
|
+
hooks: import("hookable").Hookable<AIStreamHooks, import("hookable").HookKeys<AIStreamHooks>>;
|
|
49
53
|
aiStream: {
|
|
50
54
|
send: (blocks: InputBlock[], signal?: AbortSignal, extraOptions?: Record<string, any>) => {
|
|
51
55
|
response: import("@ray-js/t-agent").StreamResponse;
|
|
@@ -59,9 +63,6 @@ export declare function withAIStream(options?: AIStreamOptions): (agent: ChatAge
|
|
|
59
63
|
options: AIStreamOptions;
|
|
60
64
|
removeMessage: (message: ChatMessage) => Promise<void>;
|
|
61
65
|
clearAllMessages: () => Promise<void>;
|
|
62
|
-
onSkillCompose: (fn: AIStreamHooks['onSkillCompose']) => () => void;
|
|
63
|
-
onSkillsEnd: (fn: AIStreamHooks['onSkillsEnd']) => () => void;
|
|
64
|
-
onCardsReceived: (fn: AIStreamHooks['onCardsReceived']) => () => void;
|
|
65
66
|
feedback: ({ requestId, type }: {
|
|
66
67
|
requestId: string;
|
|
67
68
|
type: string;
|
|
@@ -69,5 +70,9 @@ export declare function withAIStream(options?: AIStreamOptions): (agent: ChatAge
|
|
|
69
70
|
thingjson?: any;
|
|
70
71
|
data: string;
|
|
71
72
|
}>;
|
|
73
|
+
onSkillCompose: (fn: AIStreamHooks['onSkillCompose']) => () => void;
|
|
74
|
+
onSkillsEnd: (fn: AIStreamHooks['onSkillsEnd']) => () => void;
|
|
75
|
+
onCardsReceived: (fn: AIStreamHooks['onCardsReceived']) => () => void;
|
|
76
|
+
onTTTAction: (fn: AIStreamHooks['onTTTAction']) => () => void;
|
|
72
77
|
};
|
|
73
78
|
};
|
package/dist/withAIStream.js
CHANGED
|
@@ -11,9 +11,9 @@ import { BubbleTileStatus, ChatMessageStatus, createHooks, EmitterEvent } from '
|
|
|
11
11
|
import { messageAppraise } from './utils/apis';
|
|
12
12
|
import { getAccountInfo, getCurrentHomeInfo, runTTTAction, sendBlocksToAIStream } from './utils';
|
|
13
13
|
import { BizCode, ConnectClientType } from './AIStreamTypes';
|
|
14
|
-
import { ChatHistoryLocalStore } from './ChatHistoryStore';
|
|
15
14
|
import { DEFAULT_TOKEN_API, DEFAULT_TOKEN_API_VERSION, globalAIStreamClient } from './global';
|
|
16
15
|
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,11 +38,24 @@ export function withAIStream() {
|
|
|
38
38
|
const {
|
|
39
39
|
deviceId,
|
|
40
40
|
agentId,
|
|
41
|
+
clientType = ConnectClientType.APP,
|
|
41
42
|
tokenOptions = {
|
|
42
43
|
api: DEFAULT_TOKEN_API,
|
|
43
44
|
version: DEFAULT_TOKEN_API_VERSION
|
|
44
45
|
}
|
|
45
46
|
} = 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
|
+
}
|
|
46
59
|
let homeId = options.homeId;
|
|
47
60
|
if (!homeId) {
|
|
48
61
|
const info = await getCurrentHomeInfo();
|
|
@@ -56,7 +69,7 @@ export function withAIStream() {
|
|
|
56
69
|
/*
|
|
57
70
|
* 2. 获取历史消息
|
|
58
71
|
*/
|
|
59
|
-
let historyStore
|
|
72
|
+
let historyStore;
|
|
60
73
|
if (typeof options.createChatHistoryStore === 'function') {
|
|
61
74
|
historyStore = options.createChatHistoryStore(agent);
|
|
62
75
|
} else {
|
|
@@ -87,21 +100,18 @@ export function withAIStream() {
|
|
|
87
100
|
|
|
88
101
|
// 创建 streamSession
|
|
89
102
|
const connection = globalAIStreamClient.getConnection({
|
|
90
|
-
clientType
|
|
91
|
-
deviceId
|
|
103
|
+
clientType,
|
|
104
|
+
deviceId
|
|
92
105
|
});
|
|
93
|
-
if (options.clientType === ConnectClientType.DEVICE && !options.deviceId) {
|
|
94
|
-
throw new Error('deviceId is required when clientType is device');
|
|
95
|
-
}
|
|
96
106
|
const streamSession = connection.createSession({
|
|
97
|
-
ownerId:
|
|
107
|
+
ownerId: clientType === ConnectClientType.DEVICE ? deviceId : "".concat(homeId),
|
|
98
108
|
api: tokenOptions.api,
|
|
99
109
|
apiVersion: tokenOptions.version,
|
|
100
110
|
solutionCode: agentId,
|
|
101
|
-
extParams: _objectSpread(
|
|
111
|
+
extParams: _objectSpread({
|
|
102
112
|
needTts: !!options.enableTts,
|
|
103
|
-
deviceId
|
|
104
|
-
})
|
|
113
|
+
deviceId
|
|
114
|
+
}, tokenOptions.extParams)
|
|
105
115
|
});
|
|
106
116
|
await session.set('AIStream.streamSession', streamSession);
|
|
107
117
|
if (options.earlyStart) {
|
|
@@ -518,6 +528,7 @@ export function withAIStream() {
|
|
|
518
528
|
await historyStore.removeAll();
|
|
519
529
|
}
|
|
520
530
|
},
|
|
531
|
+
feedback,
|
|
521
532
|
onSkillCompose: fn => {
|
|
522
533
|
return hooks.hook('onSkillCompose', fn);
|
|
523
534
|
},
|
|
@@ -525,7 +536,9 @@ export function withAIStream() {
|
|
|
525
536
|
onCardsReceived: fn => {
|
|
526
537
|
return hooks.hook('onCardsReceived', fn);
|
|
527
538
|
},
|
|
528
|
-
|
|
539
|
+
onTTTAction: fn => {
|
|
540
|
+
return hooks.hook('onTTTAction', fn);
|
|
541
|
+
}
|
|
529
542
|
}
|
|
530
543
|
};
|
|
531
544
|
};
|
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.3-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": "ed9810075246e1261af2fdaa41550070313a98dc"
|
|
39
39
|
}
|