@ray-js/t-agent-plugin-aistream 0.2.7-beta.1 → 0.2.7-beta.11
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 +60 -2
- package/README.md +60 -2
- package/dist/AIStreamTypes.d.ts +16 -0
- package/dist/asr/createAsrAgent.js +4 -0
- package/dist/buildIn/withBuildIn.js +14 -8
- package/dist/utils/AIStream.d.ts +2 -1
- package/dist/utils/AIStream.js +2 -1
- package/dist/utils/defaultMock.js +19 -13
- package/dist/utils/mock.d.ts +1 -0
- package/dist/utils/mock.js +2 -1
- package/dist/utils/sendMessage.d.ts +2 -2
- package/dist/utils/sendMessage.js +4 -2
- package/dist/utils/track.d.ts +1 -0
- package/dist/utils/track.js +29 -0
- package/dist/utils/ttt.d.ts +2 -1
- package/dist/utils/ttt.js +2 -1
- package/dist/withAIStream.d.ts +3 -3
- package/dist/withAIStream.js +13 -4
- package/package.json +2 -2
package/README-zh_CN.md
CHANGED
|
@@ -771,6 +771,7 @@ const createAgent = () => {
|
|
|
771
771
|
- `indexId` 索引 ID,默认为 'default'
|
|
772
772
|
- `homeId` 家庭 ID,不填默认当前家庭
|
|
773
773
|
- `earlyStart` 是否在 onAgentStart 阶段就建立连接
|
|
774
|
+
- `eventIdPrefix` eventId 前缀,用于方便云端调试和日志追踪
|
|
774
775
|
- `tokenOptions` 获取 agent token 的参数
|
|
775
776
|
- `api` API 接口名
|
|
776
777
|
- `version` 接口版本
|
|
@@ -779,14 +780,30 @@ const createAgent = () => {
|
|
|
779
780
|
|
|
780
781
|
方法:
|
|
781
782
|
|
|
782
|
-
- `agent.plugins.aiStream.send` 向智能体发送一条消息
|
|
783
|
-
- `
|
|
783
|
+
- `agent.plugins.aiStream.send(blocks, signal, userData)` 向智能体发送一条消息
|
|
784
|
+
- `blocks` 输入块数组
|
|
785
|
+
- `signal` 可选的 AbortSignal,用于中断请求
|
|
786
|
+
- `userData` 可选的用户数据,会附带在发送的消息中
|
|
787
|
+
- `agent.plugins.aiStream.chat(blocks, signal, options)` 向智能体发送一条消息,并生成提问 ChatMessage 对象和 AI 回答 ChatMessage 对象,流式更新
|
|
788
|
+
- `blocks` 输入块数组
|
|
789
|
+
- `signal` 可选的 AbortSignal
|
|
790
|
+
- `options.sendBy` 发送者角色,默认为 'user'
|
|
791
|
+
- `options.responseBy` 响应者角色,默认为 'assistant'
|
|
792
|
+
- `options.userData` 可选的用户数据
|
|
793
|
+
- `agent.plugins.aiStream.getChatId()` 获取当前会话的 chatId,返回 Promise<string>
|
|
784
794
|
|
|
785
795
|
Hooks:
|
|
786
796
|
|
|
787
797
|
- `onMessageParse` 当读取历史消息,解析消息时触发,可以在这个 Hook 里修改消息
|
|
788
798
|
- `msgItem` 存储的消息对象
|
|
789
799
|
- `result.messages` 解析后的消息列表
|
|
800
|
+
- `onChatMessageSent` 当用户消息和响应消息创建后触发
|
|
801
|
+
- `userMessage` 用户发送的消息
|
|
802
|
+
- `respMessage` AI 的响应消息
|
|
803
|
+
- `onTextCompose` 当收到文本数据时触发,用于处理文本的渲染
|
|
804
|
+
- `respMsg` 响应消息
|
|
805
|
+
- `status` 消息状态
|
|
806
|
+
- `result.text` 文本内容,可以修改
|
|
790
807
|
- `onSkillCompose` 当收到技能数据时触发,用于处理技能的渲染
|
|
791
808
|
- `skill` 技能数据数组 (ReceivedTextSkillPacketBody[])
|
|
792
809
|
- `respMsg` 响应消息
|
|
@@ -801,6 +818,47 @@ Hooks:
|
|
|
801
818
|
- `onCardsReceived` 当收到卡片数据时触发
|
|
802
819
|
- `skills` 技能数据列表 (ReceivedTextSkillPacketBody[])
|
|
803
820
|
- `result.cards` 卡片列表
|
|
821
|
+
- `onUserDataRead` **(0.2.x 新增)** 当需要读取用户自定义数据时触发,用于向 AI 平台传递额外的上下文信息
|
|
822
|
+
- `type` 触发类型,可以是 'create-session' 或 'start-event'
|
|
823
|
+
- `'create-session'` 创建会话时触发,只触发一次
|
|
824
|
+
- `'start-event'` 每次发送消息时触发
|
|
825
|
+
- `data` 上下文数据
|
|
826
|
+
- `data.blocks` 当 type 为 'start-event' 时,包含本次发送的输入块
|
|
827
|
+
- `result.userData` 返回的用户数据对象,会被合并后发送给 AI 平台
|
|
828
|
+
|
|
829
|
+
**使用示例**:
|
|
830
|
+
|
|
831
|
+
```tsx
|
|
832
|
+
const agent = createChatAgent(
|
|
833
|
+
withUI(),
|
|
834
|
+
withAIStream({
|
|
835
|
+
agentId: 'your-agent-id',
|
|
836
|
+
})
|
|
837
|
+
);
|
|
838
|
+
|
|
839
|
+
agent.plugins.aiStream.onUserDataRead((type, data, result) => {
|
|
840
|
+
// 在创建会话时传递用户信息
|
|
841
|
+
if (type === 'create-session') {
|
|
842
|
+
result.sessionAttributes = {
|
|
843
|
+
'custom.param': {
|
|
844
|
+
userName: { value: 'John' },
|
|
845
|
+
userLevel: { value: 'Plus' },
|
|
846
|
+
},
|
|
847
|
+
};
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
// 在每次发送消息时传递动态上下文
|
|
851
|
+
if (type === 'start-event') {
|
|
852
|
+
result.userData = {
|
|
853
|
+
'custom.param': {
|
|
854
|
+
timestamp: { value: Date.now() },
|
|
855
|
+
pid: { value: '123456' },
|
|
856
|
+
},
|
|
857
|
+
};
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
});
|
|
861
|
+
```
|
|
804
862
|
|
|
805
863
|
### withBuildIn 插件
|
|
806
864
|
|
package/README.md
CHANGED
|
@@ -766,6 +766,7 @@ Parameters:
|
|
|
766
766
|
- `indexId` Index ID, defaults to 'default'
|
|
767
767
|
- `homeId` Home ID, defaults to current home if not provided
|
|
768
768
|
- `earlyStart` Whether to establish connection during onAgentStart phase
|
|
769
|
+
- `eventIdPrefix` Event ID prefix for cloud debugging and log tracing
|
|
769
770
|
- `tokenOptions` Parameters for getting agent token
|
|
770
771
|
- `api` API interface name
|
|
771
772
|
- `version` Interface version
|
|
@@ -774,14 +775,30 @@ Parameters:
|
|
|
774
775
|
|
|
775
776
|
Methods:
|
|
776
777
|
|
|
777
|
-
- `agent.plugins.aiStream.send
|
|
778
|
-
- `
|
|
778
|
+
- `agent.plugins.aiStream.send(blocks, signal, userData)` Send a message to the agent
|
|
779
|
+
- `blocks` Input block array
|
|
780
|
+
- `signal` Optional AbortSignal for interrupting requests
|
|
781
|
+
- `userData` Optional user data to be included with the message
|
|
782
|
+
- `agent.plugins.aiStream.chat(blocks, signal, options)` Send a message to the agent while generating a ChatMessage object for the question and the AI's answer, updating in a streaming manner
|
|
783
|
+
- `blocks` Input block array
|
|
784
|
+
- `signal` Optional AbortSignal
|
|
785
|
+
- `options.sendBy` Sender role, defaults to 'user'
|
|
786
|
+
- `options.responseBy` Responder role, defaults to 'assistant'
|
|
787
|
+
- `options.userData` Optional user data
|
|
788
|
+
- `agent.plugins.aiStream.getChatId()` Get the current session's chatId, returns Promise<string>
|
|
779
789
|
|
|
780
790
|
Hooks:
|
|
781
791
|
|
|
782
792
|
- `onMessageParse` Triggered when reading history messages and parsing them, allowing for message modification in this Hook
|
|
783
793
|
- `msgItem` Stored message object
|
|
784
794
|
- `result.messages` Parsed message list
|
|
795
|
+
- `onChatMessageSent` Triggered after user message and response message are created
|
|
796
|
+
- `userMessage` User's sent message
|
|
797
|
+
- `respMessage` AI's response message
|
|
798
|
+
- `onTextCompose` Triggered when receiving text data, used for handling text rendering
|
|
799
|
+
- `respMsg` Response message
|
|
800
|
+
- `status` Message status
|
|
801
|
+
- `result.text` Text content, can be modified
|
|
785
802
|
- `onSkillCompose` Triggered when receiving skill data, used for handling skill rendering
|
|
786
803
|
- `skill` Skill data array (ReceivedTextSkillPacketBody[])
|
|
787
804
|
- `respMsg` Response message
|
|
@@ -796,6 +813,47 @@ Hooks:
|
|
|
796
813
|
- `onCardsReceived` Triggered when receiving card data
|
|
797
814
|
- `skills` Skill data list (ReceivedTextSkillPacketBody[])
|
|
798
815
|
- `result.cards` Card list
|
|
816
|
+
- `onUserDataRead` **(0.2.x New)** Triggered when user custom data needs to be read, used to pass additional context information to the AI platform
|
|
817
|
+
- `type` Trigger type, can be 'create-session' or 'start-event'
|
|
818
|
+
- `'create-session'` Triggered when creating a session, only once
|
|
819
|
+
- `'start-event'` Triggered each time a message is sent
|
|
820
|
+
- `data` Context data
|
|
821
|
+
- `data.blocks` When type is 'start-event', contains the input blocks for this send
|
|
822
|
+
- `result.userData` Returned user data object, will be merged and sent to the AI platform
|
|
823
|
+
|
|
824
|
+
**Usage Example**:
|
|
825
|
+
|
|
826
|
+
```tsx
|
|
827
|
+
const agent = createChatAgent(
|
|
828
|
+
withUI(),
|
|
829
|
+
withAIStream({
|
|
830
|
+
agentId: 'your-agent-id',
|
|
831
|
+
})
|
|
832
|
+
);
|
|
833
|
+
|
|
834
|
+
agent.plugins.aiStream.onUserDataRead((type, data, result) => {
|
|
835
|
+
// Pass user information when creating a session
|
|
836
|
+
if (type === 'create-session') {
|
|
837
|
+
result.sessionAttributes = {
|
|
838
|
+
'custom.param': {
|
|
839
|
+
userName: { value: 'John' },
|
|
840
|
+
userLevel: { value: 'Plus' },
|
|
841
|
+
},
|
|
842
|
+
};
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
// Pass dynamic context each time a message is sent
|
|
846
|
+
if (type === 'start-event') {
|
|
847
|
+
result.userData = {
|
|
848
|
+
'custom.param': {
|
|
849
|
+
timestamp: { value: Date.now() },
|
|
850
|
+
pid: { value: '123456' },
|
|
851
|
+
},
|
|
852
|
+
};
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
});
|
|
856
|
+
```
|
|
799
857
|
|
|
800
858
|
### withBuildIn Plugin
|
|
801
859
|
|
package/dist/AIStreamTypes.d.ts
CHANGED
|
@@ -1040,6 +1040,22 @@ export type GetNetworkTypeParams = {
|
|
|
1040
1040
|
};
|
|
1041
1041
|
}) => void;
|
|
1042
1042
|
};
|
|
1043
|
+
export type EventParams = {
|
|
1044
|
+
/** 事件id */
|
|
1045
|
+
eventId: string;
|
|
1046
|
+
/** 事件点对象 */
|
|
1047
|
+
event: Record<string, any>;
|
|
1048
|
+
complete?: () => void;
|
|
1049
|
+
success?: (params: null) => void;
|
|
1050
|
+
fail?: (params: {
|
|
1051
|
+
errorMsg: string;
|
|
1052
|
+
errorCode: string | number;
|
|
1053
|
+
innerError: {
|
|
1054
|
+
errorCode: string | number;
|
|
1055
|
+
errorMsg: string;
|
|
1056
|
+
};
|
|
1057
|
+
}) => void;
|
|
1058
|
+
};
|
|
1043
1059
|
export type CheckConnectParams = {
|
|
1044
1060
|
/** client 类型: 1-作为设备代理, 2-作为 App */
|
|
1045
1061
|
clientType: ConnectClientType;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AsrAgent } from './AsrAgent';
|
|
2
|
+
import { trackEvent } from '../utils/track';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* 创建一个AsrAgent实例,用于语音转文本
|
|
@@ -31,5 +32,8 @@ import { AsrAgent } from './AsrAgent';
|
|
|
31
32
|
*/
|
|
32
33
|
export function createAsrAgent(options) {
|
|
33
34
|
const asrAgent = new AsrAgent(options);
|
|
35
|
+
trackEvent(options.agentId, 'create_asr_agent', {
|
|
36
|
+
recordingOptions: options.recordingOptions
|
|
37
|
+
});
|
|
34
38
|
return asrAgent;
|
|
35
39
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import "core-js/modules/web.dom-collections.iterator.js";
|
|
2
1
|
import { BuildInSkillCode, ReceivedSmartHomeSkillAction } from '../AIStreamTypes';
|
|
3
2
|
export function withBuildIn() {
|
|
4
3
|
return _agent => {
|
|
@@ -56,13 +55,15 @@ export function withBuildIn() {
|
|
|
56
55
|
sceneInfo: [],
|
|
57
56
|
changeInfo: []
|
|
58
57
|
};
|
|
59
|
-
for (
|
|
58
|
+
for (let i = 0; i < skills.length; i++) {
|
|
59
|
+
const skill = skills[i];
|
|
60
60
|
if (skill.code !== BuildInSkillCode.SMART_HOME) {
|
|
61
61
|
continue;
|
|
62
62
|
}
|
|
63
63
|
const content = skill;
|
|
64
64
|
if (content.general.action === ReceivedSmartHomeSkillAction.QUERY_SCENE) {
|
|
65
|
-
for (
|
|
65
|
+
for (let i1 = 0; i1 < content.general.data.scenes.length; i1++) {
|
|
66
|
+
const scene = content.general.data.scenes[i1];
|
|
66
67
|
executeData.sceneInfo.push({
|
|
67
68
|
type: scene.type,
|
|
68
69
|
enabled: scene.enable,
|
|
@@ -76,7 +77,8 @@ export function withBuildIn() {
|
|
|
76
77
|
});
|
|
77
78
|
}
|
|
78
79
|
} else if (content.general.action === ReceivedSmartHomeSkillAction.CONTROL_SCENE) {
|
|
79
|
-
for (
|
|
80
|
+
for (let i1 = 0; i1 < content.general.data.scenes.length; i1++) {
|
|
81
|
+
const scene = content.general.data.scenes[i1];
|
|
80
82
|
operateData.sceneInfo.push({
|
|
81
83
|
type: scene.type,
|
|
82
84
|
icon: scene.icon,
|
|
@@ -89,7 +91,8 @@ export function withBuildIn() {
|
|
|
89
91
|
});
|
|
90
92
|
}
|
|
91
93
|
} else if (content.general.action === ReceivedSmartHomeSkillAction.QUERY_DEVICE) {
|
|
92
|
-
for (
|
|
94
|
+
for (let i1 = 0; i1 < content.general.data.devices.length; i1++) {
|
|
95
|
+
const dev = content.general.data.devices[i1];
|
|
93
96
|
executeData.deviceInfo.push({
|
|
94
97
|
icon: dev.icon,
|
|
95
98
|
name: dev.name,
|
|
@@ -100,7 +103,8 @@ export function withBuildIn() {
|
|
|
100
103
|
});
|
|
101
104
|
}
|
|
102
105
|
} else if (content.general.action === ReceivedSmartHomeSkillAction.CONTROL_DEVICE) {
|
|
103
|
-
for (
|
|
106
|
+
for (let i1 = 0; i1 < content.general.data.devices.length; i1++) {
|
|
107
|
+
const dev = content.general.data.devices[i1];
|
|
104
108
|
operateData.deviceInfo.push({
|
|
105
109
|
icon: dev.icon,
|
|
106
110
|
name: dev.name,
|
|
@@ -128,8 +132,9 @@ export function withBuildIn() {
|
|
|
128
132
|
const data = {
|
|
129
133
|
documents: []
|
|
130
134
|
};
|
|
131
|
-
for (
|
|
135
|
+
for (let i = 0; i < skills.length; i++) {
|
|
132
136
|
var _content$custom;
|
|
137
|
+
const skill = skills[i];
|
|
133
138
|
if (skill.code !== BuildInSkillCode.SEARCH_KNOWLEDGE) {
|
|
134
139
|
continue;
|
|
135
140
|
}
|
|
@@ -137,7 +142,8 @@ export function withBuildIn() {
|
|
|
137
142
|
if (!((_content$custom = content.custom) !== null && _content$custom !== void 0 && (_content$custom = _content$custom.data) !== null && _content$custom !== void 0 && _content$custom.documents)) {
|
|
138
143
|
continue;
|
|
139
144
|
}
|
|
140
|
-
for (
|
|
145
|
+
for (let i1 = 0; i1 < content.custom.data.documents.length; i1++) {
|
|
146
|
+
const doc = content.custom.data.documents[i1];
|
|
141
147
|
data.documents.push({
|
|
142
148
|
title: doc.title,
|
|
143
149
|
url: doc.url
|
package/dist/utils/AIStream.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AIStreamAudioFile, AIStreamUserData, Attribute, BizTag, ConnectClientType, ConnectState, FileFormat, VideoCameraType } from '../AIStreamTypes';
|
|
2
2
|
import { AIStreamDataEntry, AIStreamObserverPool } from './observer';
|
|
3
|
+
import { AbortSignalObject } from '@ray-js/t-agent';
|
|
3
4
|
import { AIStreamError } from './errors';
|
|
4
5
|
interface AIStreamConnectionOptions {
|
|
5
6
|
/** client 类型: 1-作为设备代理, 2-作为 App */
|
|
@@ -43,7 +44,7 @@ interface AIStreamSessionOptions {
|
|
|
43
44
|
getSessionUserData?: () => Promise<AIStreamUserData>;
|
|
44
45
|
}
|
|
45
46
|
interface AIStreamEventOptions {
|
|
46
|
-
signal?:
|
|
47
|
+
signal?: AbortSignalObject;
|
|
47
48
|
eventIdPrefix?: string;
|
|
48
49
|
userData?: Attribute[];
|
|
49
50
|
userDataJson?: string;
|
package/dist/utils/AIStream.js
CHANGED
|
@@ -330,7 +330,8 @@ export class AIStreamSession {
|
|
|
330
330
|
sessionId: this.sessionId
|
|
331
331
|
};
|
|
332
332
|
this.activeObserver = new AIStreamObserver(this.onDataEntry, this.pool);
|
|
333
|
-
for (
|
|
333
|
+
for (let i = 0; i < this.revDataChannels.length; i++) {
|
|
334
|
+
const revDataChannel = this.revDataChannels[i];
|
|
334
335
|
if (revDataChannel.startsWith('text')) {
|
|
335
336
|
observerOptions.text = true;
|
|
336
337
|
}
|
|
@@ -95,17 +95,13 @@ mock.hooks.hook('disconnect', context => {
|
|
|
95
95
|
mock.data.set('sessionMap', new Map());
|
|
96
96
|
});
|
|
97
97
|
mock.hooks.hook('queryAgentToken', context => {
|
|
98
|
-
var _context$options$extP, _context$options$
|
|
98
|
+
var _context$options$extP, _context$options$extP3, _context$options$extP4;
|
|
99
99
|
const agentToken = generateId();
|
|
100
100
|
const map = mock.data.get('tokenMap');
|
|
101
|
-
map.set(agentToken, {
|
|
102
|
-
onlyAsr: ((_context$options$extP = context.options.extParams) === null || _context$options$extP === void 0 ? void 0 : _context$options$extP.onlyAsr) || false,
|
|
103
|
-
needTts: ((_context$options$extP2 = context.options.extParams) === null || _context$options$extP2 === void 0 ? void 0 : _context$options$extP2.needTts) || false
|
|
104
|
-
});
|
|
105
101
|
let chatId;
|
|
106
|
-
if (((_context$options$
|
|
107
|
-
var _context$options$
|
|
108
|
-
if ((_context$options$
|
|
102
|
+
if (((_context$options$extP = context.options.extParams) === null || _context$options$extP === void 0 ? void 0 : _context$options$extP.dialogueMode) === 1) {
|
|
103
|
+
var _context$options$extP2;
|
|
104
|
+
if ((_context$options$extP2 = context.options.extParams) !== null && _context$options$extP2 !== void 0 && _context$options$extP2.chatId) {
|
|
109
105
|
// 如果参数有传吗,则使用传入的 chatId
|
|
110
106
|
chatId = context.options.extParams.chatId;
|
|
111
107
|
} else {
|
|
@@ -113,6 +109,11 @@ mock.hooks.hook('queryAgentToken', context => {
|
|
|
113
109
|
chatId = "chatId-".concat(generateId());
|
|
114
110
|
}
|
|
115
111
|
}
|
|
112
|
+
map.set(agentToken, {
|
|
113
|
+
onlyAsr: ((_context$options$extP3 = context.options.extParams) === null || _context$options$extP3 === void 0 ? void 0 : _context$options$extP3.onlyAsr) || false,
|
|
114
|
+
needTts: ((_context$options$extP4 = context.options.extParams) === null || _context$options$extP4 === void 0 ? void 0 : _context$options$extP4.needTts) || false,
|
|
115
|
+
chatId
|
|
116
|
+
});
|
|
116
117
|
context.result = {
|
|
117
118
|
agentToken,
|
|
118
119
|
bizConfig: {
|
|
@@ -144,7 +145,8 @@ mock.hooks.hook('createSession', context => {
|
|
|
144
145
|
sendDataChannels: ['audio', 'video', 'text', 'image'],
|
|
145
146
|
revDataChannels: ['text', 'audio'],
|
|
146
147
|
currentEvent: null,
|
|
147
|
-
tokenConfig
|
|
148
|
+
tokenConfig,
|
|
149
|
+
chatId: tokenConfig.chatId
|
|
148
150
|
};
|
|
149
151
|
map.set(session.sessionId, session);
|
|
150
152
|
context.result = {
|
|
@@ -273,7 +275,8 @@ mock.hooks.hook('sendEventEnd', async context => {
|
|
|
273
275
|
const ctx = {
|
|
274
276
|
data: event.data,
|
|
275
277
|
responseText: '',
|
|
276
|
-
wordDelayMs: 100
|
|
278
|
+
wordDelayMs: 100,
|
|
279
|
+
chatId: session.chatId
|
|
277
280
|
};
|
|
278
281
|
const [error] = await tryCatch(() => mock.hooks.callHook('sendToAIStream', ctx));
|
|
279
282
|
if (error) {
|
|
@@ -310,7 +313,8 @@ mock.hooks.hook('sendEventEnd', async context => {
|
|
|
310
313
|
}
|
|
311
314
|
const bizId = generateId();
|
|
312
315
|
event.replyText(StreamFlag.START);
|
|
313
|
-
for (
|
|
316
|
+
for (let i = 0; i < words.length; i++) {
|
|
317
|
+
const word = words[i];
|
|
314
318
|
await mock.sleep(wordDelayMs);
|
|
315
319
|
if (event.controller.signal.aborted || session.closed) {
|
|
316
320
|
return;
|
|
@@ -345,7 +349,8 @@ mock.hooks.hook('sendEventEnd', async context => {
|
|
|
345
349
|
return;
|
|
346
350
|
}
|
|
347
351
|
if ((_ctx$responseSkills = ctx.responseSkills) !== null && _ctx$responseSkills !== void 0 && _ctx$responseSkills.length) {
|
|
348
|
-
for (
|
|
352
|
+
for (let i = 0; i < ctx.responseSkills.length; i++) {
|
|
353
|
+
const skill = ctx.responseSkills[i];
|
|
349
354
|
await mock.sleep(100);
|
|
350
355
|
if (event.controller.signal.aborted || session.closed) {
|
|
351
356
|
return;
|
|
@@ -462,7 +467,8 @@ mock.hooks.hook('startRecordAndSendAudioData', async context => {
|
|
|
462
467
|
text
|
|
463
468
|
}
|
|
464
469
|
});
|
|
465
|
-
for (
|
|
470
|
+
for (let i = 0; i < parts.length; i++) {
|
|
471
|
+
const part = parts[i];
|
|
466
472
|
if (finishController.signal.aborted) {
|
|
467
473
|
break;
|
|
468
474
|
}
|
package/dist/utils/mock.d.ts
CHANGED
package/dist/utils/mock.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import '../polyfill';
|
|
2
2
|
import { AIStreamSession } from './AIStream';
|
|
3
3
|
import { AIStreamUserData } from '../AIStreamTypes';
|
|
4
|
-
import { InputBlock, StreamResponse } from '@ray-js/t-agent';
|
|
4
|
+
import { AbortSignalObject, InputBlock, StreamResponse } from '@ray-js/t-agent';
|
|
5
5
|
export interface SendBlocksToAIStreamParams {
|
|
6
6
|
blocks: InputBlock[];
|
|
7
7
|
session: AIStreamSession;
|
|
8
|
-
signal?:
|
|
8
|
+
signal?: AbortSignalObject;
|
|
9
9
|
enableTts?: boolean;
|
|
10
10
|
eventIdPrefix?: string;
|
|
11
11
|
getUserData: () => Promise<AIStreamUserData>;
|
|
@@ -24,7 +24,8 @@ export function sendBlocksToAIStream(params) {
|
|
|
24
24
|
getUserData
|
|
25
25
|
} = params;
|
|
26
26
|
let audioEmitter = null;
|
|
27
|
-
for (
|
|
27
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
28
|
+
const block = blocks[i];
|
|
28
29
|
if (block.type === 'audio') {
|
|
29
30
|
if (audioEmitter) {
|
|
30
31
|
throw new Error('only one audio emitter is allowed');
|
|
@@ -271,7 +272,8 @@ export function sendBlocksToAIStream(params) {
|
|
|
271
272
|
}
|
|
272
273
|
});
|
|
273
274
|
event.on('error', emitError);
|
|
274
|
-
for (
|
|
275
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
276
|
+
const block = blocks[i];
|
|
275
277
|
if (block.type === 'text') {
|
|
276
278
|
event.write({
|
|
277
279
|
type: 'text',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function trackEvent(agent_code: string, type: string, params?: Record<string, any>): Promise<void>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import "core-js/modules/es.json.stringify.js";
|
|
2
|
+
import { getAccountInfo, sendEvent } from './ttt';
|
|
3
|
+
const eventId = 'thing_jKaquNvaV2qMDvTpKcNQvb1ZppBZnlNq';
|
|
4
|
+
let accountInfoPromise = null;
|
|
5
|
+
export async function trackEvent(agent_code, type) {
|
|
6
|
+
let params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
7
|
+
if (!accountInfoPromise) {
|
|
8
|
+
accountInfoPromise = getAccountInfo();
|
|
9
|
+
}
|
|
10
|
+
const {
|
|
11
|
+
miniProgram
|
|
12
|
+
} = await accountInfoPromise;
|
|
13
|
+
try {
|
|
14
|
+
await sendEvent({
|
|
15
|
+
eventId,
|
|
16
|
+
event: {
|
|
17
|
+
type,
|
|
18
|
+
agent_code,
|
|
19
|
+
miniapp_code: miniProgram.appId,
|
|
20
|
+
miniapp_version: miniProgram.version,
|
|
21
|
+
miniapp_name: miniProgram.appName,
|
|
22
|
+
miniapp_env: miniProgram.envVersion,
|
|
23
|
+
params: JSON.stringify(params)
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
} catch (e) {
|
|
27
|
+
console.error('track event error', e);
|
|
28
|
+
}
|
|
29
|
+
}
|
package/dist/utils/ttt.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ApiRequestByAtopParams, ApiRequestByHighwayParams, AudioBody, AuthorizeParams, AuthorizePolicyStatusParams, CanIUseRouterParams, CloseSessionParams, ConnectParams, ConnectStateBody, CreateSessionParams, DeleteRecordListParams, DisconnectParams, EventBody, EventChannelMessageParams, GetAppInfoParams, GetCurrentHomeInfoParams, GetMiniAppConfigParams, GetAccountInfoParams, ImageBody, InsertRecordParams, NavigateToMiniProgramParams, OpenInnerH5Params, OpenMiniWidgetParams, QueryAgentTokenParams, QueryRecordListParams, RecordAmplitudesBody, RegisterChannelParams, RouterParams, SendEventChatBreakParams, SendEventEndParams, SendEventPayloadEndParams, SendEventStartParams, SendImageDataParams, SendTextDataParams, SessionStateBody, StartRecordAndSendAudioDataParams, StopRecordAndSendAudioDataParams, TextBody, UpdateRecordParams, IsConnectedParams, GetNetworkTypeParams, StartPlayAudioParams, InitAudioRecorderParams, StopPlayAudioParams, AudioPlayChangedBody } from '../AIStreamTypes';
|
|
1
|
+
import { ApiRequestByAtopParams, ApiRequestByHighwayParams, AudioBody, AuthorizeParams, AuthorizePolicyStatusParams, CanIUseRouterParams, CloseSessionParams, ConnectParams, ConnectStateBody, CreateSessionParams, DeleteRecordListParams, DisconnectParams, EventBody, EventChannelMessageParams, GetAppInfoParams, GetCurrentHomeInfoParams, GetMiniAppConfigParams, GetAccountInfoParams, ImageBody, InsertRecordParams, NavigateToMiniProgramParams, OpenInnerH5Params, OpenMiniWidgetParams, QueryAgentTokenParams, QueryRecordListParams, RecordAmplitudesBody, RegisterChannelParams, RouterParams, SendEventChatBreakParams, SendEventEndParams, SendEventPayloadEndParams, SendEventStartParams, SendImageDataParams, SendTextDataParams, SessionStateBody, StartRecordAndSendAudioDataParams, StopRecordAndSendAudioDataParams, TextBody, UpdateRecordParams, IsConnectedParams, GetNetworkTypeParams, StartPlayAudioParams, InitAudioRecorderParams, StopPlayAudioParams, AudioPlayChangedBody, EventParams } from '../AIStreamTypes';
|
|
2
2
|
export declare const getMiniAppConfig: (options?: Omit<GetMiniAppConfigParams, "success" | "fail"> | undefined) => Promise<{
|
|
3
3
|
config: any;
|
|
4
4
|
}>;
|
|
@@ -113,3 +113,4 @@ export declare const getNetworkType: (options?: Omit<GetNetworkTypeParams, "succ
|
|
|
113
113
|
networkType: import("../AIStreamTypes").NetworkType;
|
|
114
114
|
signalStrength: number;
|
|
115
115
|
}>;
|
|
116
|
+
export declare const sendEvent: (options?: Omit<EventParams, "success" | "fail"> | undefined) => Promise<null>;
|
package/dist/utils/ttt.js
CHANGED
|
@@ -91,4 +91,5 @@ export const queryRecordList = promisify(ty.aistream.queryRecordList, true);
|
|
|
91
91
|
export const deleteRecordList = promisify(ty.aistream.deleteRecordList, true);
|
|
92
92
|
export const updateRecord = promisify(ty.aistream.updateRecord, true);
|
|
93
93
|
export const insertRecord = promisify(ty.aistream.insertRecord, true);
|
|
94
|
-
export const getNetworkType = promisify(ty.getNetworkType, true);
|
|
94
|
+
export const getNetworkType = promisify(ty.getNetworkType, true);
|
|
95
|
+
export const sendEvent = promisify(ty.event);
|
package/dist/withAIStream.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ChatAgent, ChatCardObject, ChatMessage, ChatMessageStatus, ChatTile, GetChatPluginHandler, InputBlock } from '@ray-js/t-agent';
|
|
1
|
+
import { AbortSignalObject, ChatAgent, ChatCardObject, ChatMessage, ChatMessageStatus, ChatTile, GetChatPluginHandler, InputBlock } from '@ray-js/t-agent';
|
|
2
2
|
import { TTTAction } from './utils';
|
|
3
3
|
import { AIStreamUserData, ConnectClientType, ReceivedTextSkillPacketBody } from './AIStreamTypes';
|
|
4
4
|
import { ChatHistoryStore, StoredMessageObject } from './ChatHistoryStore';
|
|
@@ -67,11 +67,11 @@ export interface AIStreamHooks {
|
|
|
67
67
|
export declare function withAIStream(options?: AIStreamOptions): (agent: ChatAgent) => {
|
|
68
68
|
hooks: import("hookable").Hookable<AIStreamHooks, import("hookable").HookKeys<AIStreamHooks>>;
|
|
69
69
|
aiStream: {
|
|
70
|
-
send: (blocks: InputBlock[], signal?:
|
|
70
|
+
send: (blocks: InputBlock[], signal?: AbortSignalObject, eventUserData?: AIStreamUserData) => {
|
|
71
71
|
response: import("@ray-js/t-agent").StreamResponse;
|
|
72
72
|
metaPromise: Promise<Record<string, any>>;
|
|
73
73
|
};
|
|
74
|
-
chat: (blocks: InputBlock[], signal?:
|
|
74
|
+
chat: (blocks: InputBlock[], signal?: AbortSignalObject, options?: {
|
|
75
75
|
sendBy?: string | undefined;
|
|
76
76
|
responseBy?: string | undefined;
|
|
77
77
|
userData?: AIStreamUserData | undefined;
|
package/dist/withAIStream.js
CHANGED
|
@@ -15,6 +15,7 @@ import { DEFAULT_TOKEN_API, DEFAULT_TOKEN_API_VERSION, globalAIStreamClient } fr
|
|
|
15
15
|
import logger from './utils/logger';
|
|
16
16
|
import { ChatHistoryLocalStore } from './ChatHistoryLocalStore';
|
|
17
17
|
import { deepMerge } from './utils/object';
|
|
18
|
+
import { trackEvent } from './utils/track';
|
|
18
19
|
export function withAIStream() {
|
|
19
20
|
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
20
21
|
const hooks = createHooks();
|
|
@@ -61,6 +62,10 @@ export function withAIStream() {
|
|
|
61
62
|
if (clientType === ConnectClientType.APP && deviceId) {
|
|
62
63
|
throw new Error('deviceId is not allowed when clientType is app');
|
|
63
64
|
}
|
|
65
|
+
trackEvent(agentId, 'on_agent_start', {
|
|
66
|
+
clientType,
|
|
67
|
+
tokenApi: "".concat(tokenOptions.api, "_").concat(tokenOptions.version)
|
|
68
|
+
});
|
|
64
69
|
let homeId = options.homeId;
|
|
65
70
|
if (clientType === ConnectClientType.DEVELOPER) {
|
|
66
71
|
homeId = 1;
|
|
@@ -267,7 +272,8 @@ export function withAIStream() {
|
|
|
267
272
|
userData
|
|
268
273
|
} = options || {};
|
|
269
274
|
let audioEmitter = null;
|
|
270
|
-
for (
|
|
275
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
276
|
+
const block = blocks[i];
|
|
271
277
|
if (block.type === 'audio') {
|
|
272
278
|
if (audioEmitter) {
|
|
273
279
|
throw new Error('only one audio emitter is allowed');
|
|
@@ -418,7 +424,8 @@ export function withAIStream() {
|
|
|
418
424
|
if (valid) {
|
|
419
425
|
await message.persist();
|
|
420
426
|
const [_, ...rest] = result.messages;
|
|
421
|
-
for (
|
|
427
|
+
for (let i = 0; i < rest.length; i++) {
|
|
428
|
+
const m = rest[i];
|
|
422
429
|
await m.show();
|
|
423
430
|
await m.persist();
|
|
424
431
|
}
|
|
@@ -550,8 +557,9 @@ export function withAIStream() {
|
|
|
550
557
|
};
|
|
551
558
|
onSkillsEnd(async (skills, respMsg, result) => {
|
|
552
559
|
const cards = [];
|
|
553
|
-
for (
|
|
560
|
+
for (let i = 0; i < skills.length; i++) {
|
|
554
561
|
var _skill$custom, _skill$general;
|
|
562
|
+
const skill = skills[i];
|
|
555
563
|
if ((_skill$custom = skill.custom) !== null && _skill$custom !== void 0 && (_skill$custom = _skill$custom.data) !== null && _skill$custom !== void 0 && _skill$custom.aiCards) {
|
|
556
564
|
for (const card of skill.custom.data.aiCards) {
|
|
557
565
|
cards.push(card);
|
|
@@ -567,7 +575,8 @@ export function withAIStream() {
|
|
|
567
575
|
cards: cards
|
|
568
576
|
};
|
|
569
577
|
await hooks.callHook('onCardsReceived', skills, r);
|
|
570
|
-
for (
|
|
578
|
+
for (let i = 0; i < cards.length; i++) {
|
|
579
|
+
const card = cards[i];
|
|
571
580
|
const m = createMessage({
|
|
572
581
|
role: respMsg.role,
|
|
573
582
|
status: ChatMessageStatus.FINISH,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/t-agent-plugin-aistream",
|
|
3
|
-
"version": "0.2.7-beta.
|
|
3
|
+
"version": "0.2.7-beta.11",
|
|
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": "10d81d0352c88b6b2eff7168ed046a065416dc4f"
|
|
39
39
|
}
|