@blueking/chat-helper 0.0.12-beta.1 → 0.0.12-beta.3

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.
@@ -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 abortController = null;
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
- abortController = new AbortController();
154
- (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.fetchClient.streamRequest(_object_spread({
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: abortController,
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(sessionCode, url, config) {
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(sessionCode, url, config);
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
- streamRequest(sessionCode, url, config);
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
+ }, 15000);
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 _abortController_abort;
216
- abortController === null || abortController === void 0 ? void 0 : (_abortController_abort = abortController.abort) === null || _abortController_abort === void 0 ? void 0 : _abortController_abort.call(abortController);
217
- abortController = null;
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(sessionCode, url, config);
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
  };
@@ -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(_event: IRunFinishedEvent): void;
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
  */
@@ -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 - 消息模块
@@ -63,6 +63,9 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
63
63
  case CustomEventName.FlowAgentEnd:
64
64
  this.handleFlowAgentEndCustomEvent(event);
65
65
  break;
66
+ case CustomEventName.FlowAgentRestart:
67
+ this.handleFlowAgentRestartCustomEvent(event);
68
+ break;
66
69
  case CustomEventName.ReferenceDocument:
67
70
  this.handleReferenceDocumentCustomEvent(event);
68
71
  break;
@@ -81,6 +84,12 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
81
84
  case CustomEventName.TempMessage:
82
85
  this.handleTempMessageCustomEvent(event);
83
86
  break;
87
+ case CustomEventName.ApprovalResult:
88
+ this.handleApprovalResultCustomEvent(event);
89
+ break;
90
+ case CustomEventName.FlowAgentUpdate:
91
+ this.handleFlowAgentUpdateCustomEvent(event);
92
+ break;
84
93
  default:
85
94
  break;
86
95
  }
@@ -102,6 +111,20 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
102
111
  }
103
112
  }
104
113
  /**
114
+ * 自定义事件 处理流程编排任务重启
115
+ */ handleFlowAgentRestartCustomEvent(event) {
116
+ const value = event.value;
117
+ this.messageModule.list.value.forEach((item)=>{
118
+ if (item.role === MessageRole.Activity && item.activityType === ActivityType.FlowAgent) {
119
+ const isSameTask = item.content[0].task_id === value[0].task_id;
120
+ // 通过第一个 task_id 判断是否是同一个任务,是的话直接更新消息状态为 streaming
121
+ if (isSameTask) {
122
+ item.status = MessageStatus.Streaming;
123
+ }
124
+ }
125
+ });
126
+ }
127
+ /**
105
128
  * 自定义事件 处理流程编排任务结果(增量更新节点状态)
106
129
  */ handleFlowAgentResultCustomEvent(event) {
107
130
  const message = this.messageModule.getCurrentLoadingMessage();
@@ -118,7 +141,7 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
118
141
  activityType: ActivityType.FlowAgent,
119
142
  content: value.map((item)=>({
120
143
  nodes: {},
121
- task_id: Number(item.task_id),
144
+ task_id: item.task_id,
122
145
  task_name: '',
123
146
  task_outputs: [],
124
147
  task_state: FlowTaskState.Created
@@ -127,6 +150,20 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
127
150
  });
128
151
  }
129
152
  /**
153
+ * 自定义事件 处理流程编排任务更新
154
+ */ handleFlowAgentUpdateCustomEvent(event) {
155
+ const value = event.value;
156
+ this.messageModule.list.value.forEach((item)=>{
157
+ if (item.role === MessageRole.Activity && item.activityType === ActivityType.FlowAgent) {
158
+ const isSameTask = item.content[0].task_id === value[0].task_id;
159
+ // 通过第一个 task_id 判断是否是同一个任务,是的话直接更新消息内容
160
+ if (isSameTask) {
161
+ item.content = value;
162
+ }
163
+ }
164
+ });
165
+ }
166
+ /**
130
167
  * 自定义事件 处理意图识别结束
131
168
  */ handleKnowledgeRagEndCustomEvent(_event) {
132
169
  const message = this.messageModule.getCurrentLoadingMessage();
@@ -206,11 +243,24 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
206
243
  }
207
244
  /**
208
245
  * 处理运行完成事件
209
- */ handleRunFinishedEvent(_event) {
246
+ */ handleRunFinishedEvent(event) {
247
+ var _event_outcome, _event_outcome1;
210
248
  const message = this.messageModule.getCurrentLoadingMessage();
211
249
  if (message) {
212
250
  message.status = MessageStatus.Complete;
213
251
  }
252
+ // 如果是中断消息,则创建一个中断消息
253
+ if (((_event_outcome = event.outcome) === null || _event_outcome === void 0 ? void 0 : _event_outcome.type) === RunFinishedOutcomeType.Interrupt) {
254
+ this.messageModule.plusMessage({
255
+ role: MessageRole.Interrupt,
256
+ content: event,
257
+ status: MessageStatus.Pending
258
+ });
259
+ }
260
+ // 二次返回,直接更新消息内容
261
+ 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) {
262
+ message.content = event;
263
+ }
214
264
  }
215
265
  /**
216
266
  * 处理运行开始事件
@@ -249,6 +299,16 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
249
299
  });
250
300
  }
251
301
  /**
302
+ * 自定义事件 处理审批结果
303
+ */ handleApprovalResultCustomEvent(event) {
304
+ const value = event.value;
305
+ const message = this.messageModule.getCurrentLoadingMessage();
306
+ if (message) {
307
+ message.content = value;
308
+ message.status = MessageStatus.Complete;
309
+ }
310
+ }
311
+ /**
252
312
  * 处理文本消息块事件
253
313
  */ handleTextMessageChunkEvent(event) {
254
314
  const message = this.messageModule.getCurrentLoadingMessage();
@@ -358,14 +418,23 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
358
418
  /**
359
419
  * 处理工具调用结果事件
360
420
  */ handleToolCallResultEvent(event) {
361
- this.messageModule.plusMessage({
362
- role: MessageRole.Tool,
363
- content: event.content,
364
- duration: event.duration,
365
- status: event.isError ? MessageStatus.Error : MessageStatus.Complete,
366
- toolCallId: event.toolCallId,
367
- messageId: event.messageId
368
- });
421
+ const message = this.messageModule.getCurrentLoadingMessage();
422
+ if ((message === null || message === void 0 ? void 0 : message.role) === MessageRole.Interrupt) {
423
+ // 如果是中断消息,则更新消息内容
424
+ message.content.result = JSON.parse(event.content);
425
+ message.content.outcome.type = RunFinishedOutcomeType.Success;
426
+ message.status = MessageStatus.Complete;
427
+ } else {
428
+ // 否则创建一个工具调用结果消息
429
+ this.messageModule.plusMessage({
430
+ role: MessageRole.Tool,
431
+ content: event.content,
432
+ duration: event.duration,
433
+ status: event.isError ? MessageStatus.Error : MessageStatus.Complete,
434
+ toolCallId: event.toolCallId,
435
+ messageId: event.messageId
436
+ });
437
+ }
369
438
  }
370
439
  /**
371
440
  * 处理工具调用开始事件
@@ -397,10 +466,10 @@ import { CustomEventName, EventType, FlowTaskState } from './type.ts.js';
397
466
  (_this_onDoneCallback = (_this = this).onDoneCallback) === null || _this_onDoneCallback === void 0 ? void 0 : _this_onDoneCallback.call(_this);
398
467
  }
399
468
  onError(error) {
400
- var // 回调给上层
469
+ var // 在聊天区域显示错误消息
470
+ _this_messageModule, // 回调给上层
401
471
  _this_onErrorCallback, _this;
402
- // 创建一个错误消息
403
- this.messageModule.plusMessage({
472
+ (_this_messageModule = this.messageModule) === null || _this_messageModule === void 0 ? void 0 : _this_messageModule.plusMessage({
404
473
  role: MessageRole.Assistant,
405
474
  content: error.message,
406
475
  status: MessageStatus.Error
@@ -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: number;
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?: unknown;
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;