@blueking/chat-helper 0.0.11 → 0.0.12-beta.10
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/agent/use-agent.d.ts +1276 -4
- package/dist/agent/use-agent.ts.js +136 -19
- package/dist/event/ag-ui.d.ts +14 -2
- package/dist/event/ag-ui.ts.js +75 -7
- package/dist/event/type.d.ts +90 -24
- package/dist/event/type.ts.js +44 -16
- package/dist/http/fetch/fetch.d.ts +20 -0
- package/dist/http/fetch/fetch.ts.js +67 -18
- package/dist/http/index.d.ts +14 -3
- package/dist/http/index.ts.js +2 -1
- package/dist/http/module/index.d.ts +12 -3
- package/dist/http/module/message.d.ts +6 -3
- package/dist/http/module/message.ts.js +18 -1
- package/dist/http/module/session.d.ts +8 -2
- package/dist/http/module/session.ts.js +47 -8
- package/dist/http/transform/message.ts.js +10 -0
- package/dist/index.d.ts +8867 -18
- package/dist/index.ts.js +2 -1
- package/dist/message/type.d.ts +26 -5
- package/dist/message/type.ts.js +8 -0
- package/dist/message/use-message.d.ts +2524 -4
- package/dist/session/type.d.ts +19 -0
- package/dist/session/use-session.d.ts +5056 -10
- package/dist/session/use-session.ts.js +78 -8
- package/package.json +4 -1
|
@@ -80,8 +80,8 @@ function _object_spread(target) {
|
|
|
80
80
|
return target;
|
|
81
81
|
}
|
|
82
82
|
import { ref } from 'vue';
|
|
83
|
-
import { AGUIProtocol } from '../event/index.ts.js';
|
|
84
|
-
import { MessageRole, MessageStatus } from '../message/index.ts.js';
|
|
83
|
+
import { AGUIProtocol, ApprovalInterruptTicketStatus, ResumeStatus, RunFinishedOutcomeType } from '../event/index.ts.js';
|
|
84
|
+
import { MessageRole, MessageStatus, UserOperation } from '../message/index.ts.js';
|
|
85
85
|
import { SessionStatus } from '../session/type.ts.js';
|
|
86
86
|
/**
|
|
87
87
|
* Agent 模块
|
|
@@ -92,7 +92,9 @@ import { SessionStatus } from '../session/type.ts.js';
|
|
|
92
92
|
const isInfoLoading = ref(false);
|
|
93
93
|
const isChatting = ref(false);
|
|
94
94
|
let usedProtocol = protocol || new AGUIProtocol();
|
|
95
|
-
let
|
|
95
|
+
let chatAbortController = null;
|
|
96
|
+
let resumeAbortController = null;
|
|
97
|
+
let longPollTimer = null;
|
|
96
98
|
const getAgentInfo = ()=>{
|
|
97
99
|
var _mediator_http;
|
|
98
100
|
isInfoLoading.value = true;
|
|
@@ -121,19 +123,54 @@ import { SessionStatus } from '../session/type.ts.js';
|
|
|
121
123
|
});
|
|
122
124
|
}
|
|
123
125
|
};
|
|
126
|
+
const userOperationStreamRequest = (sessionCode, operation, payload, config)=>{
|
|
127
|
+
var _mediator_http;
|
|
128
|
+
return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.message.userOperation(sessionCode, operation, payload, config).then(()=>{
|
|
129
|
+
if (operation !== UserOperation.ApprovalCancel) {
|
|
130
|
+
streamRequest({
|
|
131
|
+
sessionCode,
|
|
132
|
+
config
|
|
133
|
+
});
|
|
134
|
+
} else {
|
|
135
|
+
clearLongPollTimer();
|
|
136
|
+
pollResumeSession(sessionCode);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
};
|
|
124
140
|
const streamRequest = function() {
|
|
125
|
-
var _ref = _async_to_generator(function*(sessionCode, url, config) {
|
|
126
|
-
var
|
|
127
|
-
_mediator_http;
|
|
141
|
+
var _ref = _async_to_generator(function*({ sessionCode, url, config, resume, input, lastMessageId }) {
|
|
142
|
+
var _mediator_http;
|
|
128
143
|
// ag-ui 协议需要注入消息模块
|
|
129
144
|
if (usedProtocol instanceof AGUIProtocol) {
|
|
130
145
|
usedProtocol.injectMessageModule(mediator.message);
|
|
131
146
|
}
|
|
147
|
+
if (input) {
|
|
148
|
+
var // 列表新增一个用户消息
|
|
149
|
+
_mediator_message;
|
|
150
|
+
(_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : _mediator_message.list.value.push({
|
|
151
|
+
role: MessageRole.User,
|
|
152
|
+
content: input,
|
|
153
|
+
status: MessageStatus.Complete,
|
|
154
|
+
sessionCode
|
|
155
|
+
});
|
|
156
|
+
}
|
|
132
157
|
// 事件代理
|
|
133
158
|
const onDone = ()=>{
|
|
134
159
|
var _usedProtocol_onDone;
|
|
135
160
|
isChatting.value = false;
|
|
136
161
|
(_usedProtocol_onDone = usedProtocol.onDone) === null || _usedProtocol_onDone === void 0 ? void 0 : _usedProtocol_onDone.call(usedProtocol);
|
|
162
|
+
if (input) {
|
|
163
|
+
var // 刷新列表,获取前端 mock message 的 id,并更新到列表中
|
|
164
|
+
_mediator_http_message, _mediator_http;
|
|
165
|
+
(_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : (_mediator_http_message = _mediator_http.message) === null || _mediator_http_message === void 0 ? void 0 : _mediator_http_message.getMessages(sessionCode).then((res)=>{
|
|
166
|
+
var _mediator_message;
|
|
167
|
+
const lastUserMessage = (_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : _mediator_message.list.value.findLast((item)=>item.role === MessageRole.User);
|
|
168
|
+
const lastApiUserMessage = res.findLast((item)=>item.role === MessageRole.User);
|
|
169
|
+
lastUserMessage.id = lastApiUserMessage.id;
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
// 轮询接口,判断是否可以继续聊天
|
|
173
|
+
pollResumeSession(sessionCode);
|
|
137
174
|
};
|
|
138
175
|
const onError = (error)=>{
|
|
139
176
|
var _usedProtocol_onError;
|
|
@@ -150,24 +187,31 @@ import { SessionStatus } from '../session/type.ts.js';
|
|
|
150
187
|
(_usedProtocol_onStart = usedProtocol.onStart) === null || _usedProtocol_onStart === void 0 ? void 0 : _usedProtocol_onStart.call(usedProtocol);
|
|
151
188
|
};
|
|
152
189
|
// 创建 AbortController
|
|
153
|
-
|
|
154
|
-
|
|
190
|
+
chatAbortController = new AbortController();
|
|
191
|
+
// 发起聊天
|
|
192
|
+
void ((_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.fetchClient.streamRequest(_object_spread({
|
|
155
193
|
url: url || 'chat_completion/',
|
|
156
194
|
method: 'POST',
|
|
157
195
|
data: {
|
|
158
196
|
session_code: sessionCode,
|
|
197
|
+
input,
|
|
159
198
|
execute_kwargs: {
|
|
160
|
-
stream: true
|
|
199
|
+
stream: true,
|
|
200
|
+
persist_input: !!input,
|
|
201
|
+
last_message_id: lastMessageId,
|
|
202
|
+
resume
|
|
161
203
|
}
|
|
162
204
|
},
|
|
163
|
-
controller:
|
|
205
|
+
controller: chatAbortController,
|
|
164
206
|
onDone,
|
|
165
207
|
onError,
|
|
166
208
|
onMessage,
|
|
167
209
|
onStart
|
|
168
|
-
}, config))
|
|
210
|
+
}, config)).catch(()=>{
|
|
211
|
+
// 非 abort 错误已通过 onError 回调处理;abort 为正常结束
|
|
212
|
+
}));
|
|
169
213
|
});
|
|
170
|
-
return function streamRequest(
|
|
214
|
+
return function streamRequest(_) {
|
|
171
215
|
return _ref.apply(this, arguments);
|
|
172
216
|
};
|
|
173
217
|
}();
|
|
@@ -191,7 +235,11 @@ import { SessionStatus } from '../session/type.ts.js';
|
|
|
191
235
|
property
|
|
192
236
|
}));
|
|
193
237
|
// 发起聊天
|
|
194
|
-
streamRequest(
|
|
238
|
+
streamRequest({
|
|
239
|
+
sessionCode,
|
|
240
|
+
url,
|
|
241
|
+
config
|
|
242
|
+
});
|
|
195
243
|
});
|
|
196
244
|
return function chat(userInput, sessionCode, url, config, property) {
|
|
197
245
|
return _ref.apply(this, arguments);
|
|
@@ -206,15 +254,76 @@ import { SessionStatus } from '../session/type.ts.js';
|
|
|
206
254
|
*/ const resumeStreamingChat = (sessionCode, url, config)=>{
|
|
207
255
|
var _mediator_session_current_value, _mediator_session;
|
|
208
256
|
if (((_mediator_session = mediator.session) === null || _mediator_session === void 0 ? void 0 : (_mediator_session_current_value = _mediator_session.current.value) === null || _mediator_session_current_value === void 0 ? void 0 : _mediator_session_current_value.status) === SessionStatus.Running) {
|
|
209
|
-
|
|
257
|
+
var _mediator_message_list_value_at, _mediator_message;
|
|
258
|
+
const lastMessageId = (_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : (_mediator_message_list_value_at = _mediator_message.list.value.at(-1)) === null || _mediator_message_list_value_at === void 0 ? void 0 : _mediator_message_list_value_at.id;
|
|
259
|
+
streamRequest({
|
|
260
|
+
sessionCode,
|
|
261
|
+
url,
|
|
262
|
+
config,
|
|
263
|
+
lastMessageId
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
/**
|
|
268
|
+
* 轮询接口,判断是否可以继续聊天
|
|
269
|
+
* @param sessionCode - 会话编码
|
|
270
|
+
* @returns 是否可以继续聊天
|
|
271
|
+
*/ const pollResumeSession = (sessionCode)=>{
|
|
272
|
+
var _mediator_message, _lastMessage_content_outcome, _lastMessage_content;
|
|
273
|
+
const lastMessage = (_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : _mediator_message.list.value.at(-1);
|
|
274
|
+
const pendingApprovalInterrupt = (lastMessage === null || lastMessage === void 0 ? void 0 : (_lastMessage_content = lastMessage.content) === null || _lastMessage_content === void 0 ? void 0 : (_lastMessage_content_outcome = _lastMessage_content.outcome) === null || _lastMessage_content_outcome === void 0 ? void 0 : _lastMessage_content_outcome.type) === RunFinishedOutcomeType.Interrupt ? lastMessage.content.outcome.interrupts.find((interrupt)=>{
|
|
275
|
+
var _interrupt_metadata_ticket, _interrupt_metadata;
|
|
276
|
+
return [
|
|
277
|
+
ApprovalInterruptTicketStatus.Pending,
|
|
278
|
+
ApprovalInterruptTicketStatus.Draft
|
|
279
|
+
].includes((_interrupt_metadata = interrupt.metadata) === null || _interrupt_metadata === void 0 ? void 0 : (_interrupt_metadata_ticket = _interrupt_metadata.ticket) === null || _interrupt_metadata_ticket === void 0 ? void 0 : _interrupt_metadata_ticket.status);
|
|
280
|
+
}) : undefined;
|
|
281
|
+
const getIsTicketLoading = ()=>{
|
|
282
|
+
const isInterruptMessage = (lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.role) === MessageRole.Interrupt;
|
|
283
|
+
return isInterruptMessage && !!pendingApprovalInterrupt;
|
|
284
|
+
};
|
|
285
|
+
if (getIsTicketLoading()) {
|
|
286
|
+
var // 轮询接口,判断是否可以继续聊天
|
|
287
|
+
_mediator_http;
|
|
288
|
+
// 清除轮询定时器和中断轮询控制器
|
|
289
|
+
clearLongPollTimer();
|
|
290
|
+
resumeAbortController = new AbortController();
|
|
291
|
+
(_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.session.isResumeSession(sessionCode, {
|
|
292
|
+
controller: resumeAbortController
|
|
293
|
+
}).then((res)=>{
|
|
294
|
+
if (res) {
|
|
295
|
+
// 可以继续聊天,重新发起聊天(携带 execute_kwargs.resume 通知后端恢复中断)
|
|
296
|
+
streamRequest({
|
|
297
|
+
sessionCode,
|
|
298
|
+
resume: {
|
|
299
|
+
interruptId: pendingApprovalInterrupt.id,
|
|
300
|
+
status: ResumeStatus.Resolved
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
} else {
|
|
304
|
+
longPollTimer = setTimeout(()=>{
|
|
305
|
+
var _mediator_session_current_value, _mediator_session_current, _mediator_session;
|
|
306
|
+
// 如果会话不匹配,则不继续轮询
|
|
307
|
+
if (sessionCode !== ((_mediator_session = mediator.session) === null || _mediator_session === void 0 ? void 0 : (_mediator_session_current = _mediator_session.current) === null || _mediator_session_current === void 0 ? void 0 : (_mediator_session_current_value = _mediator_session_current.value) === null || _mediator_session_current_value === void 0 ? void 0 : _mediator_session_current_value.sessionCode)) return;
|
|
308
|
+
pollResumeSession(sessionCode);
|
|
309
|
+
}, 10000);
|
|
310
|
+
}
|
|
311
|
+
});
|
|
210
312
|
}
|
|
211
313
|
};
|
|
314
|
+
const clearLongPollTimer = ()=>{
|
|
315
|
+
var _resumeAbortController_abort;
|
|
316
|
+
clearTimeout(longPollTimer);
|
|
317
|
+
longPollTimer = null;
|
|
318
|
+
resumeAbortController === null || resumeAbortController === void 0 ? void 0 : (_resumeAbortController_abort = resumeAbortController.abort) === null || _resumeAbortController_abort === void 0 ? void 0 : _resumeAbortController_abort.call(resumeAbortController);
|
|
319
|
+
resumeAbortController = null;
|
|
320
|
+
};
|
|
212
321
|
/**
|
|
213
322
|
* 中止聊天(纯前端中止,后端继续处理)
|
|
214
323
|
*/ const abortChat = ()=>{
|
|
215
|
-
var
|
|
216
|
-
|
|
217
|
-
|
|
324
|
+
var _chatAbortController_abort;
|
|
325
|
+
chatAbortController === null || chatAbortController === void 0 ? void 0 : (_chatAbortController_abort = chatAbortController.abort) === null || _chatAbortController_abort === void 0 ? void 0 : _chatAbortController_abort.call(chatAbortController);
|
|
326
|
+
chatAbortController = null;
|
|
218
327
|
};
|
|
219
328
|
/**
|
|
220
329
|
* 停止会话,后端中止
|
|
@@ -269,7 +378,11 @@ import { SessionStatus } from '../session/type.ts.js';
|
|
|
269
378
|
property: originalProperty
|
|
270
379
|
}));
|
|
271
380
|
// 6. 立即发起流式请求(不等待删除和创建的 API 完成)
|
|
272
|
-
streamRequest(
|
|
381
|
+
streamRequest({
|
|
382
|
+
sessionCode,
|
|
383
|
+
url,
|
|
384
|
+
config
|
|
385
|
+
});
|
|
273
386
|
// 7. 在后台等待 API 完成,处理可能的错误
|
|
274
387
|
Promise.all([
|
|
275
388
|
deletePromise,
|
|
@@ -299,6 +412,10 @@ import { SessionStatus } from '../session/type.ts.js';
|
|
|
299
412
|
abortChat,
|
|
300
413
|
stopChat,
|
|
301
414
|
getAgentInfo,
|
|
302
|
-
reset
|
|
415
|
+
reset,
|
|
416
|
+
pollResumeSession,
|
|
417
|
+
clearLongPollTimer,
|
|
418
|
+
userOperationStreamRequest,
|
|
419
|
+
streamRequest
|
|
303
420
|
};
|
|
304
421
|
};
|
package/dist/event/ag-ui.d.ts
CHANGED
|
@@ -26,7 +26,7 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
26
26
|
* 处理活动快照事件
|
|
27
27
|
* 记录活动的完整状态
|
|
28
28
|
*/
|
|
29
|
-
handleActivitySnapshotEvent(
|
|
29
|
+
handleActivitySnapshotEvent(event: IActivitySnapshotEvent): void;
|
|
30
30
|
/**
|
|
31
31
|
* 处理自定义事件
|
|
32
32
|
*/
|
|
@@ -35,6 +35,10 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
35
35
|
* 自定义事件 处理流程编排任务结束
|
|
36
36
|
*/
|
|
37
37
|
handleFlowAgentEndCustomEvent(event: ICustomEvent): void;
|
|
38
|
+
/**
|
|
39
|
+
* 自定义事件 处理流程编排任务重启
|
|
40
|
+
*/
|
|
41
|
+
handleFlowAgentRestartCustomEvent(event: ICustomEvent): void;
|
|
38
42
|
/**
|
|
39
43
|
* 自定义事件 处理流程编排任务结果(增量更新节点状态)
|
|
40
44
|
*/
|
|
@@ -43,6 +47,10 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
43
47
|
* 自定义事件 处理流程编排任务开始
|
|
44
48
|
*/
|
|
45
49
|
handleFlowAgentStartCustomEvent(event: ICustomEvent): void;
|
|
50
|
+
/**
|
|
51
|
+
* 自定义事件 处理流程编排任务更新
|
|
52
|
+
*/
|
|
53
|
+
handleFlowAgentUpdateCustomEvent(event: ICustomEvent): void;
|
|
46
54
|
/**
|
|
47
55
|
* 自定义事件 处理意图识别结束
|
|
48
56
|
*/
|
|
@@ -80,7 +88,7 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
80
88
|
/**
|
|
81
89
|
* 处理运行完成事件
|
|
82
90
|
*/
|
|
83
|
-
handleRunFinishedEvent(
|
|
91
|
+
handleRunFinishedEvent(event: IRunFinishedEvent): void;
|
|
84
92
|
/**
|
|
85
93
|
* 处理运行开始事件
|
|
86
94
|
*/
|
|
@@ -105,6 +113,10 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
105
113
|
* 自定义事件 处理临时消息,转换为 AI 文本消息
|
|
106
114
|
*/
|
|
107
115
|
handleTempMessageCustomEvent(event: ICustomEvent): void;
|
|
116
|
+
/**
|
|
117
|
+
* 自定义事件 处理审批结果
|
|
118
|
+
*/
|
|
119
|
+
handleApprovalResultCustomEvent(event: ICustomEvent): void;
|
|
108
120
|
/**
|
|
109
121
|
* 处理文本消息块事件
|
|
110
122
|
*/
|
package/dist/event/ag-ui.ts.js
CHANGED
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
}
|
|
38
38
|
import { transferMessageApi2Message } from '../http/transform/index.ts.js';
|
|
39
39
|
import { ActivityType, MessageRole, MessageStatus, MessageType } from '../message/index.ts.js';
|
|
40
|
-
import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
|
|
40
|
+
import { CustomEventName, EventType, FlowTaskState, RunFinishedOutcomeType } from './type.ts.js';
|
|
41
41
|
/**
|
|
42
42
|
* AGUI 协议
|
|
43
43
|
* @param message - 消息模块
|
|
@@ -49,7 +49,15 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
|
|
|
49
49
|
/**
|
|
50
50
|
* 处理活动快照事件
|
|
51
51
|
* 记录活动的完整状态
|
|
52
|
-
*/ handleActivitySnapshotEvent(
|
|
52
|
+
*/ handleActivitySnapshotEvent(event) {
|
|
53
|
+
const message = this.messageModule.getCurrentLoadingMessage();
|
|
54
|
+
if (event.activityType === ActivityType.Interrupt) {
|
|
55
|
+
if ((message === null || message === void 0 ? void 0 : message.role) === MessageRole.Interrupt) {
|
|
56
|
+
message.content = event.content;
|
|
57
|
+
message.status = MessageStatus.Complete;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
53
61
|
/**
|
|
54
62
|
* 处理自定义事件
|
|
55
63
|
*/ handleCustomEvent(event) {
|
|
@@ -63,6 +71,9 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
|
|
|
63
71
|
case CustomEventName.FlowAgentEnd:
|
|
64
72
|
this.handleFlowAgentEndCustomEvent(event);
|
|
65
73
|
break;
|
|
74
|
+
case CustomEventName.FlowAgentRestart:
|
|
75
|
+
this.handleFlowAgentRestartCustomEvent(event);
|
|
76
|
+
break;
|
|
66
77
|
case CustomEventName.ReferenceDocument:
|
|
67
78
|
this.handleReferenceDocumentCustomEvent(event);
|
|
68
79
|
break;
|
|
@@ -81,6 +92,12 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
|
|
|
81
92
|
case CustomEventName.TempMessage:
|
|
82
93
|
this.handleTempMessageCustomEvent(event);
|
|
83
94
|
break;
|
|
95
|
+
case CustomEventName.ApprovalResult:
|
|
96
|
+
this.handleApprovalResultCustomEvent(event);
|
|
97
|
+
break;
|
|
98
|
+
case CustomEventName.FlowAgentUpdate:
|
|
99
|
+
this.handleFlowAgentUpdateCustomEvent(event);
|
|
100
|
+
break;
|
|
84
101
|
default:
|
|
85
102
|
break;
|
|
86
103
|
}
|
|
@@ -102,6 +119,20 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
|
|
|
102
119
|
}
|
|
103
120
|
}
|
|
104
121
|
/**
|
|
122
|
+
* 自定义事件 处理流程编排任务重启
|
|
123
|
+
*/ handleFlowAgentRestartCustomEvent(event) {
|
|
124
|
+
const value = event.value;
|
|
125
|
+
this.messageModule.list.value.forEach((item)=>{
|
|
126
|
+
if (item.role === MessageRole.Activity && item.activityType === ActivityType.FlowAgent) {
|
|
127
|
+
const isSameTask = item.content[0].task_id === value[0].task_id;
|
|
128
|
+
// 通过第一个 task_id 判断是否是同一个任务,是的话直接更新消息状态为 streaming
|
|
129
|
+
if (isSameTask) {
|
|
130
|
+
item.status = MessageStatus.Streaming;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
105
136
|
* 自定义事件 处理流程编排任务结果(增量更新节点状态)
|
|
106
137
|
*/ handleFlowAgentResultCustomEvent(event) {
|
|
107
138
|
const message = this.messageModule.getCurrentLoadingMessage();
|
|
@@ -118,7 +149,7 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
|
|
|
118
149
|
activityType: ActivityType.FlowAgent,
|
|
119
150
|
content: value.map((item)=>({
|
|
120
151
|
nodes: {},
|
|
121
|
-
task_id:
|
|
152
|
+
task_id: item.task_id,
|
|
122
153
|
task_name: '',
|
|
123
154
|
task_outputs: [],
|
|
124
155
|
task_state: FlowTaskState.Created
|
|
@@ -127,6 +158,20 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
|
|
|
127
158
|
});
|
|
128
159
|
}
|
|
129
160
|
/**
|
|
161
|
+
* 自定义事件 处理流程编排任务更新
|
|
162
|
+
*/ handleFlowAgentUpdateCustomEvent(event) {
|
|
163
|
+
const value = event.value;
|
|
164
|
+
this.messageModule.list.value.forEach((item)=>{
|
|
165
|
+
if (item.role === MessageRole.Activity && item.activityType === ActivityType.FlowAgent) {
|
|
166
|
+
const isSameTask = item.content[0].task_id === value[0].task_id;
|
|
167
|
+
// 通过第一个 task_id 判断是否是同一个任务,是的话直接更新消息内容
|
|
168
|
+
if (isSameTask) {
|
|
169
|
+
item.content = value;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
130
175
|
* 自定义事件 处理意图识别结束
|
|
131
176
|
*/ handleKnowledgeRagEndCustomEvent(_event) {
|
|
132
177
|
const message = this.messageModule.getCurrentLoadingMessage();
|
|
@@ -206,11 +251,24 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
|
|
|
206
251
|
}
|
|
207
252
|
/**
|
|
208
253
|
* 处理运行完成事件
|
|
209
|
-
*/ handleRunFinishedEvent(
|
|
254
|
+
*/ handleRunFinishedEvent(event) {
|
|
255
|
+
var _event_outcome, _event_outcome1;
|
|
210
256
|
const message = this.messageModule.getCurrentLoadingMessage();
|
|
211
257
|
if (message) {
|
|
212
258
|
message.status = MessageStatus.Complete;
|
|
213
259
|
}
|
|
260
|
+
// 如果是中断消息,则创建一个中断消息
|
|
261
|
+
if (((_event_outcome = event.outcome) === null || _event_outcome === void 0 ? void 0 : _event_outcome.type) === RunFinishedOutcomeType.Interrupt) {
|
|
262
|
+
this.messageModule.plusMessage({
|
|
263
|
+
role: MessageRole.Interrupt,
|
|
264
|
+
content: event,
|
|
265
|
+
status: MessageStatus.Pending
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
// 二次返回,直接更新消息内容
|
|
269
|
+
if (((_event_outcome1 = event.outcome) === null || _event_outcome1 === void 0 ? void 0 : _event_outcome1.type) === RunFinishedOutcomeType.Success && (message === null || message === void 0 ? void 0 : message.role) === MessageRole.Interrupt) {
|
|
270
|
+
message.content = event;
|
|
271
|
+
}
|
|
214
272
|
}
|
|
215
273
|
/**
|
|
216
274
|
* 处理运行开始事件
|
|
@@ -249,6 +307,16 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
|
|
|
249
307
|
});
|
|
250
308
|
}
|
|
251
309
|
/**
|
|
310
|
+
* 自定义事件 处理审批结果
|
|
311
|
+
*/ handleApprovalResultCustomEvent(event) {
|
|
312
|
+
const value = event.value;
|
|
313
|
+
const message = this.messageModule.getCurrentLoadingMessage();
|
|
314
|
+
if (message) {
|
|
315
|
+
message.content = value;
|
|
316
|
+
message.status = MessageStatus.Complete;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
252
320
|
* 处理文本消息块事件
|
|
253
321
|
*/ handleTextMessageChunkEvent(event) {
|
|
254
322
|
const message = this.messageModule.getCurrentLoadingMessage();
|
|
@@ -397,10 +465,10 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
|
|
|
397
465
|
(_this_onDoneCallback = (_this = this).onDoneCallback) === null || _this_onDoneCallback === void 0 ? void 0 : _this_onDoneCallback.call(_this);
|
|
398
466
|
}
|
|
399
467
|
onError(error) {
|
|
400
|
-
var //
|
|
468
|
+
var // 在聊天区域显示错误消息
|
|
469
|
+
_this_messageModule, // 回调给上层
|
|
401
470
|
_this_onErrorCallback, _this;
|
|
402
|
-
|
|
403
|
-
this.messageModule.plusMessage({
|
|
471
|
+
(_this_messageModule = this.messageModule) === null || _this_messageModule === void 0 ? void 0 : _this_messageModule.plusMessage({
|
|
404
472
|
role: MessageRole.Assistant,
|
|
405
473
|
content: error.message,
|
|
406
474
|
status: MessageStatus.Error
|
package/dist/event/type.d.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { type IMessageApi, type MessageRole, type MessageStatus } from '../message/type';
|
|
2
|
+
import type { JSONSchema4 } from 'json-schema';
|
|
2
3
|
export declare enum CustomEventName {
|
|
3
4
|
FlowAgentEnd = "flow_agent_end",
|
|
4
5
|
FlowAgentResult = "flow_agent_result",
|
|
5
6
|
FlowAgentStart = "flow_agent_start",
|
|
7
|
+
FlowAgentUpdate = "flow_agent_update",
|
|
8
|
+
FlowAgentRestart = "flow_agent_restart",
|
|
6
9
|
KnowledgeRagEnd = "knowledge_rag_end",
|
|
7
10
|
KnowledgeRagResult = "knowledge_rag_result",
|
|
8
11
|
KnowledgeRagStart = "knowledge_rag_start",
|
|
9
12
|
KnowledgeRagTextContent = "knowledge_rag_text_content",
|
|
10
13
|
ReferenceDocument = "reference_document",
|
|
11
|
-
TempMessage = "temp_message"
|
|
14
|
+
TempMessage = "temp_message",
|
|
15
|
+
ApprovalResult = "approval_result"
|
|
12
16
|
}
|
|
13
17
|
export declare enum EventType {
|
|
14
18
|
ActivityDelta = "ACTIVITY_DELTA",
|
|
@@ -40,42 +44,51 @@ export declare enum EventType {
|
|
|
40
44
|
}
|
|
41
45
|
/** BKFlow 收敛后的节点状态 - task.node 的状态枚举 */
|
|
42
46
|
export declare enum FlowNodeState {
|
|
43
|
-
/** 失败 */
|
|
44
47
|
Failed = "failed",
|
|
45
|
-
/** 执行中 */
|
|
46
48
|
Running = "running",
|
|
47
|
-
/** 成功 */
|
|
48
49
|
Success = "success",
|
|
49
|
-
/** 挂起 */
|
|
50
50
|
Suspended = "suspended"
|
|
51
51
|
}
|
|
52
52
|
/** BKFLOW 原始状态 - task 的状态枚举 */
|
|
53
53
|
export declare enum FlowTaskState {
|
|
54
|
-
/** 执行中 */
|
|
55
54
|
Blocked = "BLOCKED",
|
|
56
|
-
/** 执行中 */
|
|
57
55
|
Created = "CREATED",
|
|
58
|
-
/** 失败 */
|
|
59
56
|
Failed = "FAILED",
|
|
60
|
-
/** 成功 */
|
|
61
57
|
Finished = "FINISHED",
|
|
62
|
-
/** 执行中 */
|
|
63
58
|
LoopReady = "LOOP_READY",
|
|
64
|
-
/** 执行中 */
|
|
65
59
|
Ready = "READY",
|
|
66
|
-
/** 失败 */
|
|
67
60
|
Revoked = "REVOKED",
|
|
68
|
-
/** 失败 */
|
|
69
61
|
RollBackFailed = "ROLL_BACK_FAILED",
|
|
70
|
-
/** 执行中 */
|
|
71
62
|
RollBackSuccess = "ROLL_BACK_SUCCESS",
|
|
72
|
-
/** 执行中 */
|
|
73
63
|
RollingBack = "ROLLING_BACK",
|
|
74
|
-
/** 执行中 */
|
|
75
64
|
Running = "RUNNING",
|
|
76
|
-
/** 挂起 */
|
|
77
65
|
Suspended = "SUSPENDED"
|
|
78
66
|
}
|
|
67
|
+
/** 运行完成结果 */
|
|
68
|
+
export declare enum RunFinishedOutcomeType {
|
|
69
|
+
Success = "success",
|
|
70
|
+
Interrupt = "interrupt"
|
|
71
|
+
}
|
|
72
|
+
/** 审批状态 */
|
|
73
|
+
export declare enum ApprovalInterruptTicketStatus {
|
|
74
|
+
Abandoned = "abandoned",
|
|
75
|
+
Approved = "approved",
|
|
76
|
+
Cancelled = "cancelled",
|
|
77
|
+
Draft = "draft",
|
|
78
|
+
Expired = "expired",
|
|
79
|
+
Rejected = "rejected",
|
|
80
|
+
Pending = "pending"
|
|
81
|
+
}
|
|
82
|
+
/** 中断原因 */
|
|
83
|
+
export declare enum InterruptReason {
|
|
84
|
+
AIDevToolApproval = "aidev:tool_approval",
|
|
85
|
+
UserQuestion = "aidev:user_question"
|
|
86
|
+
}
|
|
87
|
+
/** 恢复状态 */
|
|
88
|
+
export declare enum ResumeStatus {
|
|
89
|
+
Resolved = "resolved",
|
|
90
|
+
Cancelled = "cancelled"
|
|
91
|
+
}
|
|
79
92
|
/**
|
|
80
93
|
* Activity 代表 Agent 执行过程中的活动/动作/任务,可能包含结构化数据
|
|
81
94
|
* 活动状态的增量更新
|
|
@@ -112,25 +125,25 @@ export interface IBaseEvent {
|
|
|
112
125
|
export interface ICustomEvent extends IBaseEvent {
|
|
113
126
|
name: CustomEventName;
|
|
114
127
|
type: EventType.Custom;
|
|
115
|
-
value: IFlowAgentEndCustomValue | IFlowAgentResultCustomValue | IFlowAgentStartCustomValue | IKnowledgeRagResultCustomValue | IKnowledgeRagTextContentCustomValue | IReferenceDocumentCustomValue | ITempMessageCustomValue;
|
|
128
|
+
value: IFlowAgentEndCustomValue | IFlowAgentResultCustomValue | IFlowAgentStartCustomValue | IFlowAgentRestartCustomValue | IKnowledgeRagResultCustomValue | IKnowledgeRagTextContentCustomValue | IReferenceDocumentCustomValue | ITempMessageCustomValue | IApprovalResultCustomValue;
|
|
116
129
|
}
|
|
117
130
|
export type IEvent = IActivityDeltaEvent | IActivitySnapshotEvent | ICustomEvent | IMessagesSnapshotEvent | IRawEvent | IRunErrorEvent | IRunFinishedEvent | IRunStartedEvent | IStateDeltaEvent | IStateSnapshotEvent | IStepFinishedEvent | IStepStartedEvent | ITextMessageChunkEvent | ITextMessageContentEvent | ITextMessageEndEvent | ITextMessageStartEvent | IThinkingEndEvent | IThinkingStartEvent | IThinkingTextMessageContentEvent | IThinkingTextMessageEndEvent | IThinkingTextMessageStartEvent | IToolCallArgsEvent | IToolCallChunkEvent | IToolCallEndEvent | IToolCallResultEvent | IToolCallStartEvent;
|
|
118
131
|
export type IFlowAgentEndCustomValue = {
|
|
119
132
|
task_outputs: string[];
|
|
120
133
|
}[];
|
|
121
134
|
export interface IFlowAgentNode {
|
|
122
|
-
/** 耗时(s) */
|
|
123
135
|
elapsed_time: number;
|
|
124
|
-
/** 节点id */
|
|
125
136
|
id: string;
|
|
126
|
-
/** 节点名称 */
|
|
127
137
|
name: string;
|
|
128
|
-
/** 节点状态 */
|
|
129
138
|
state: FlowNodeState;
|
|
139
|
+
retry: number;
|
|
140
|
+
skip: boolean;
|
|
141
|
+
retryable: boolean;
|
|
142
|
+
skippable: boolean;
|
|
130
143
|
}
|
|
131
144
|
export type IFlowAgentResultCustomValue = {
|
|
132
145
|
nodes: Record<string, IFlowAgentNode>;
|
|
133
|
-
task_id:
|
|
146
|
+
task_id: string;
|
|
134
147
|
task_name: string;
|
|
135
148
|
task_outputs: string[];
|
|
136
149
|
task_state: FlowTaskState;
|
|
@@ -138,6 +151,7 @@ export type IFlowAgentResultCustomValue = {
|
|
|
138
151
|
export type IFlowAgentStartCustomValue = {
|
|
139
152
|
task_id: string;
|
|
140
153
|
}[];
|
|
154
|
+
export type IFlowAgentRestartCustomValue = IFlowAgentStartCustomValue;
|
|
141
155
|
export type IKnowledgeRagResultCustomValue = IReferenceDocumentCustomValue;
|
|
142
156
|
export interface IKnowledgeRagTextContentCustomValue {
|
|
143
157
|
cover: boolean;
|
|
@@ -173,15 +187,66 @@ export interface IRunErrorEvent extends IBaseEvent {
|
|
|
173
187
|
message: string;
|
|
174
188
|
type: EventType.RunError;
|
|
175
189
|
}
|
|
190
|
+
export interface IInterrupt<T extends InterruptReason, P extends Record<string, unknown>> {
|
|
191
|
+
id: string;
|
|
192
|
+
reason: T;
|
|
193
|
+
message?: string;
|
|
194
|
+
toolCallId?: string;
|
|
195
|
+
responseSchema?: JSONSchema4;
|
|
196
|
+
expiresAt?: string;
|
|
197
|
+
metadata?: P;
|
|
198
|
+
}
|
|
199
|
+
export type IApprovalInterrupt = IInterrupt<InterruptReason.AIDevToolApproval, {
|
|
200
|
+
ticket: {
|
|
201
|
+
approvers: string[];
|
|
202
|
+
sn: string;
|
|
203
|
+
status: ApprovalInterruptTicketStatus;
|
|
204
|
+
submit_time: string;
|
|
205
|
+
title: string;
|
|
206
|
+
url: string;
|
|
207
|
+
};
|
|
208
|
+
}>;
|
|
209
|
+
export type IUserQuestionInterrupt = IInterrupt<InterruptReason.UserQuestion, {
|
|
210
|
+
questions: {
|
|
211
|
+
header: string;
|
|
212
|
+
multiSelect?: boolean;
|
|
213
|
+
options?: {
|
|
214
|
+
description: string;
|
|
215
|
+
label: string;
|
|
216
|
+
}[];
|
|
217
|
+
question: string;
|
|
218
|
+
}[];
|
|
219
|
+
}>;
|
|
220
|
+
export interface IResume {
|
|
221
|
+
interruptId: string;
|
|
222
|
+
status: ResumeStatus;
|
|
223
|
+
payload?: {
|
|
224
|
+
answers: {
|
|
225
|
+
answer: {
|
|
226
|
+
description: string;
|
|
227
|
+
label: string;
|
|
228
|
+
}[];
|
|
229
|
+
multiSelect?: boolean;
|
|
230
|
+
question: string;
|
|
231
|
+
}[];
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
export type IRunFinishedOutcome = {
|
|
235
|
+
type: RunFinishedOutcomeType.Success;
|
|
236
|
+
} | {
|
|
237
|
+
type: RunFinishedOutcomeType.Interrupt;
|
|
238
|
+
interrupts: Array<IApprovalInterrupt | IUserQuestionInterrupt>;
|
|
239
|
+
};
|
|
176
240
|
/**
|
|
177
241
|
* 标记 Agent 运行正常结束
|
|
178
242
|
* 返回最终结果 result
|
|
179
243
|
*/
|
|
180
244
|
export interface IRunFinishedEvent extends IBaseEvent {
|
|
181
|
-
result?:
|
|
245
|
+
result?: IResume;
|
|
182
246
|
runId: number;
|
|
183
247
|
threadId: string;
|
|
184
248
|
type: EventType.RunFinished;
|
|
249
|
+
outcome?: IRunFinishedOutcome;
|
|
185
250
|
}
|
|
186
251
|
/**
|
|
187
252
|
* 标记一次完整的 Agent 执行开始
|
|
@@ -234,6 +299,7 @@ export interface ITempMessageCustomValue {
|
|
|
234
299
|
message: string;
|
|
235
300
|
status: MessageStatus;
|
|
236
301
|
}
|
|
302
|
+
export type IApprovalResultCustomValue = IRunFinishedEvent;
|
|
237
303
|
export interface ITextMessageChunkEvent extends IBaseEvent {
|
|
238
304
|
delta?: string;
|
|
239
305
|
messageId: string;
|