Commit b5a64d8a authored by ligaowei's avatar ligaowei

Refactor event processing services to use dedicated EventDeduplicationService...

Refactor event processing services to use dedicated EventDeduplicationService and remove duplicate logic
parent fbbf5f18
// 统一的事件处理服务
import type { TimelineEvent } from '../types/timeline';
export class EventProcessingService {
/**
* 标准化事件对象
* @param event 原始事件数据
* @returns 标准化的事件对象
*/
normalizeEvent(event: any): TimelineEvent {
// 确保时间戳存在
const timestamp = event.timestamp || Date.now();
// 根据事件类型创建相应类型的事件对象
switch (event.type) {
case 'thought':
return {
type: 'thought',
title: event.title || '思考事件',
content: event.content || '',
thinkingType: event.thinkingType,
metadata: event.metadata,
timestamp
};
case 'tool_call':
return {
type: 'tool_call',
title: event.title || '工具调用',
toolName: event.toolName || '',
toolAction: event.toolAction || '',
toolInput: event.toolInput,
toolStatus: event.toolStatus,
metadata: event.metadata,
timestamp
};
case 'tool_result':
return {
type: 'tool_result',
title: event.title || '工具结果',
toolName: event.toolName || '',
toolAction: event.toolAction || '',
toolOutput: event.toolOutput,
toolStatus: event.toolStatus,
executionTime: event.executionTime,
metadata: event.metadata,
timestamp
};
case 'tool_error':
return {
type: 'tool_error',
title: event.title || '工具错误',
toolName: event.toolName || '',
errorMessage: event.errorMessage || '',
errorCode: event.errorCode,
metadata: event.metadata,
timestamp
};
case 'embed':
return {
type: 'embed',
title: event.title || '嵌入内容',
embedUrl: event.embedUrl || '',
embedType: event.embedType,
embedTitle: event.embedTitle,
embedHtmlContent: event.embedHtmlContent,
metadata: event.metadata,
timestamp
};
default:
return {
type: event.type || 'thought',
title: event.title || '未命名事件',
metadata: event.metadata,
timestamp
};
}
}
/**
* 处理事件类型转换
* @param event 事件对象
* @returns 处理后的事件对象
*/
processEventType(event: any): any {
// 处理thinking类型的事件,如果是final_answer则转换为result类型
const processedEvent = { ...event };
if (processedEvent.type === 'thought' && processedEvent.title === '最终答案') {
processedEvent.type = 'result';
}
return processedEvent;
}
}
\ No newline at end of file
// 统一事件处理器
import type { TimelineEvent } from '../types/timeline'
import { EventProcessingService } from './EventProcessingService'
import { EventDeduplicationService } from './EventDeduplicationService'
export class UnifiedEventProcessor {
private eventProcessingService: EventProcessingService
private eventDeduplicationService: EventDeduplicationService
private eventHandlers: Array<(event: TimelineEvent) => void> = []
private processedEvents: TimelineEvent[] = []
constructor() {
this.eventProcessingService = new EventProcessingService()
this.eventDeduplicationService = new EventDeduplicationService()
}
/**
* 处理接收到的原始事件数据
* @param rawData 原始事件数据
* @returns 处理后的标准化事件对象
*/
processRawEvent(rawData: any): TimelineEvent | null {
try {
// 验证数据
if (!rawData || typeof rawData !== 'object') {
console.warn('[UnifiedEventProcessor] 无效的事件数据:', rawData)
return null
}
// 检查是否为重复事件
if (this.eventDeduplicationService.isDuplicateEvent(rawData)) {
console.log('[UnifiedEventProcessor] 跳过重复事件:', rawData.type, rawData.title)
return null
}
// 处理事件类型转换
const processedEvent = this.eventProcessingService.processEventType(rawData)
// 标准化事件对象
const normalizedEvent = this.eventProcessingService.normalizeEvent(processedEvent)
// 添加到已处理事件列表
this.processedEvents.push(normalizedEvent)
// 限制已处理事件列表大小以避免内存泄漏
if (this.processedEvents.length > 1000) {
this.processedEvents.shift()
}
return normalizedEvent
} catch (error) {
console.error('[UnifiedEventProcessor] 处理事件数据时发生错误:', error, '原始数据:', rawData)
return null
}
}
/**
* 注册事件处理器
* @param handler 事件处理器函数
*/
registerHandler(handler: (event: TimelineEvent) => void): void {
this.eventHandlers.push(handler)
}
/**
* 分发事件给所有注册的处理器
* @param event 事件对象
*/
dispatchEvent(event: TimelineEvent): void {
this.eventHandlers.forEach(handler => {
try {
handler(event)
} catch (error) {
console.error('[UnifiedEventProcessor] 事件处理器执行错误:', error)
}
})
}
/**
* 处理并分发事件
* @param rawData 原始事件数据
*/
processAndDispatch(rawData: any): void {
const event = this.processRawEvent(rawData)
if (event) {
this.dispatchEvent(event)
}
}
/**
* 清除已处理事件列表
*/
clearProcessedEvents(): void {
this.processedEvents = []
this.eventDeduplicationService.clearEventHashSet()
}
}
\ No newline at end of file
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