Commit 39fed4a2 authored by ligaowei's avatar ligaowei

Add shared type definitions and utility functions for timeline events

parent 998f1d1d
// 统一的时间轴事件类型定义
export interface BaseTimelineEvent {
type: string;
title: string;
timestamp: number;
metadata?: Record<string, any>;
}
export interface ThoughtEvent extends BaseTimelineEvent {
content: string;
thinkingType?: string;
}
export interface ToolCallEvent extends BaseTimelineEvent {
toolName: string;
toolAction?: string;
toolInput?: any;
toolStatus: string;
}
export interface ToolResultEvent extends BaseTimelineEvent {
toolName: string;
toolAction?: string;
toolOutput?: any;
toolStatus: string;
executionTime?: number;
}
export interface ToolErrorEvent extends BaseTimelineEvent {
toolName: string;
errorMessage: string;
errorCode?: string;
}
export interface EmbedEvent extends BaseTimelineEvent {
embedUrl: string;
embedType?: string;
embedTitle: string;
embedHtmlContent?: string;
}
export type TimelineEvent =
| ThoughtEvent
| ToolCallEvent
| ToolResultEvent
| ToolErrorEvent
| EmbedEvent
| BaseTimelineEvent;
// 事件类型标签映射
export const eventTypeLabels: Record<string, string> = {
thought: '🧠 思考',
tool_call: '🔧 工具调用',
tool_result: '✅ 工具结果',
tool_error: '❌ 工具错误',
embed: '🌐 网页预览',
log: '📝 日志',
result: '🎯 最终答案',
observation: '🔍 观察'
};
\ No newline at end of file
// 时间轴工具函数库
/**
* 检查是否为工具事件类型
* @param type 事件类型
* @returns 是否为工具事件类型
*/
export function isToolEventType(type: string): boolean {
return ['tool_call', 'tool_result', 'tool_error'].includes(type);
}
/**
* 检查工具输入是否有效
* @param event 事件对象
* @returns 工具输入是否有效
*/
export function hasValidToolInput(event: any): boolean {
return event.type === 'tool_call' && event.toolInput !== null && event.toolInput !== undefined;
}
/**
* 检查工具输出是否有效
* @param event 事件对象
* @returns 工具输出是否有效
*/
export function hasValidToolOutput(event: any): boolean {
return event.type === 'tool_result' && event.toolOutput !== null && event.toolOutput !== undefined;
}
/**
* 截断标题
* @param title 标题
* @param maxLength 最大长度
* @returns 截断后的标题
*/
export function truncateTitle(title: string, maxLength: number = 30): string {
if (!title) return '';
return title.length > maxLength ? title.substring(0, maxLength) + '...' : title;
}
\ No newline at end of file
// 类型守卫函数
import type {
TimelineEvent,
ThoughtEvent,
ToolCallEvent,
ToolResultEvent,
ToolErrorEvent,
EmbedEvent
} from '../types/timeline';
/**
* 检查是否为思考事件
* @param event 事件对象
* @returns 是否为思考事件
*/
export function isThoughtEvent(event: TimelineEvent): event is ThoughtEvent {
return event.type === 'thought';
}
/**
* 检查是否为工具调用事件
* @param event 事件对象
* @returns 是否为工具调用事件
*/
export function isToolCallEvent(event: TimelineEvent): event is ToolCallEvent {
return event.type === 'tool_call';
}
/**
* 检查是否为工具结果事件
* @param event 事件对象
* @returns 是否为工具结果事件
*/
export function isToolResultEvent(event: TimelineEvent): event is ToolResultEvent {
return event.type === 'tool_result';
}
/**
* 检查是否为工具错误事件
* @param event 事件对象
* @returns 是否为工具错误事件
*/
export function isToolErrorEvent(event: TimelineEvent): event is ToolErrorEvent {
return event.type === 'tool_error';
}
/**
* 检查是否为嵌入事件
* @param event 事件对象
* @returns 是否为嵌入事件
*/
export function isEmbedEvent(event: TimelineEvent): event is EmbedEvent {
return event.type === 'embed';
}
\ 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