Commit 55b91d26 authored by ligaowei's avatar ligaowei

feat(react): 优化事件处理逻辑并添加事件内容字段

- 在WorkPanelEvent中添加content字段用于存储事件内容
- 简化sendWorkPanelEvent方法调用,移除冗余参数
- 完善DefaultReactCallback中的事件发送逻辑,增加错误处理
- 调整EventSplitter的endStream方法签名以支持回调
- 修复代码格式问题
parent 5538d955
package pangea.hiagent.agent.react; package pangea.hiagent.agent.react;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import pangea.hiagent.agent.service.UserSseService; import pangea.hiagent.agent.service.UserSseService;
import pangea.hiagent.workpanel.IWorkPanelDataCollector; import pangea.hiagent.common.utils.UserUtils;
import pangea.hiagent.web.dto.WorkPanelEvent;
/** /**
* 简化的ReAct回调类 * 简化的ReAct回调类
...@@ -18,18 +21,23 @@ public class DefaultReactCallback implements ReactCallback { ...@@ -18,18 +21,23 @@ public class DefaultReactCallback implements ReactCallback {
@Override @Override
public void onStep(ReactStep reactStep) { public void onStep(ReactStep reactStep) {
log.info("ReAct步骤触发: 类型={}, 内容摘要={}",
reactStep.getStepType(),
reactStep.getContent() != null ?
reactStep.getContent().substring(0, Math.min(50, reactStep.getContent().length())) : "null");
String reactStepName = reactStep.getStepType().name(); String reactStepName = reactStep.getStepType().name();
try {
userSseService.sendWorkPanelEvent(WorkPanelEvent.builder() userSseService.sendWorkPanelEvent(WorkPanelEvent.builder()
.eventType(reactStepName) .type(reactStepName)
.content(reactStep.getContent()) .content(reactStep.getContent())
.userId(reactStep.getUserId()) .userId(UserUtils.getCurrentUserIdStatic())
.build()); .build());
} catch (IOException e) {
log.error("发送ReAct步骤到WorkPanel失败: 类型={}, 内容摘要={}",
reactStep.getStepType(),
reactStep.getContent() != null
? reactStep.getContent().substring(0, Math.min(50, reactStep.getContent().length()))
: "null",
e);
}
// 记录最终答案到日志 // 记录最终答案到日志
log.info("[WorkPanel] 记录{} {}", reactStepName, log.info("[WorkPanel] 记录{} {}", reactStepName,
......
...@@ -49,7 +49,7 @@ public class EventSplitter { ...@@ -49,7 +49,7 @@ public class EventSplitter {
} }
// 流式结束时,调用此方法输出最后一个事件 // 流式结束时,调用此方法输出最后一个事件
public void endStream() { public void endStream(ReactCallback tokenConsumer) {
if (currentType != null && currentContent.length() > 0) { if (currentType != null && currentContent.length() > 0) {
callback.onStep(new ReactStep(stepNumber++, ReactStepType.fromString(currentType), currentContent.toString())); callback.onStep(new ReactStep(stepNumber++, ReactStepType.fromString(currentType), currentContent.toString()));
} }
......
...@@ -116,7 +116,7 @@ public abstract class BaseTool { ...@@ -116,7 +116,7 @@ public abstract class BaseTool {
.build(); .build();
// 获取用户的SSE发射器 // 获取用户的SSE发射器
userSseService.sendWorkPanelEvent(event, userId); userSseService.sendWorkPanelEvent(event);
log.debug("已发送工具事件: {}#{}, 状态: {}", toolName, methodName, status); log.debug("已发送工具事件: {}#{}, 状态: {}", toolName, methodName, status);
......
...@@ -34,6 +34,10 @@ public class WorkPanelEvent implements Serializable { ...@@ -34,6 +34,10 @@ public class WorkPanelEvent implements Serializable {
*/ */
private Long timestamp; private Long timestamp;
/**
* 事件内容
*/
private String content;
/** /**
* 元数据 * 元数据
*/ */
......
...@@ -384,7 +384,7 @@ public class WorkPanelDataCollector implements IWorkPanelDataCollector { ...@@ -384,7 +384,7 @@ public class WorkPanelDataCollector implements IWorkPanelDataCollector {
// 通过EventService发送事件到所有SSE连接 // 通过EventService发送事件到所有SSE连接
for (SseEmitter emitter : unifiedSseService.getEmitters()) { for (SseEmitter emitter : unifiedSseService.getEmitters()) {
try { try {
unifiedSseService.sendWorkPanelEvent(emitter, event); unifiedSseService.sendWorkPanelEvent(event);
} catch (Exception e) { } catch (Exception e) {
log.debug("通过EventService发送事件失败: {}", e.getMessage()); log.debug("通过EventService发送事件失败: {}", e.getMessage());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment