File size: 2,133 Bytes
bc7e9cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
export type MessageRole = "user" | "assistant" | "system";

export type MessageSegmentType =
  | "text"
  | "tool-invocation"
  | "tool-result"
  | "reasoning";

export type ToolStatus = "pending" | "running" | "completed" | "error";

export type StreamingStatus = "idle" | "thinking" | "streaming" | "completing";

export interface MessageSegment {
  readonly id: string;
  readonly type: MessageSegmentType;
  readonly content: string;
  readonly toolName?: string;
  readonly toolArgs?: Record<string, unknown>;
  readonly toolStatus?: ToolStatus;
  readonly toolOutput?: string;
  readonly toolResult?: string;
  readonly toolError?: string;
  readonly startTime?: number;
  readonly endTime?: number;
  readonly consoleOutput?: string[];
  readonly expanded?: boolean;
  readonly streaming?: boolean;
}

export interface ChatMessage {
  readonly id: string;
  readonly role: MessageRole;
  readonly content: string;
  readonly timestamp: number;
  readonly streaming?: boolean;
  readonly reasoning?: string;
  readonly showReasoning?: boolean;
  readonly segments?: ReadonlyArray<MessageSegment>;
}

export interface ChatState {
  readonly messages: ReadonlyArray<ChatMessage>;
  readonly connected: boolean;
  readonly processing: boolean;
  readonly error: string | null;
  readonly streamingContent: string;
  readonly streamingStatus: StreamingStatus;
  readonly thinkingStartTime: number | null;
}

export function createMessage(
  role: MessageRole,
  content: string,
  id?: string,
): ChatMessage {
  return {
    id: id || `${role}_${Date.now()}`,
    role,
    content,
    timestamp: Date.now(),
    streaming: false,
  };
}

export function createSegment(
  type: MessageSegmentType,
  content: string = "",
  id?: string,
): MessageSegment {
  return {
    id: id || `seg_${Date.now()}`,
    type,
    content,
    streaming: type === "text",
    startTime: Date.now(),
  };
}

export function createInitialChatState(): ChatState {
  return {
    messages: [],
    connected: false,
    processing: false,
    error: null,
    streamingContent: "",
    streamingStatus: "idle",
    thinkingStartTime: null,
  };
}