Repository navigation
Expand file tree
/
Copy pathappState.ts
More file actions
132 lines (121 loc) · 4.32 KB
/
Copy pathappState.ts
File metadata and controls
132 lines (121 loc) · 4.32 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import type { AccountInfo } from '@azure/msal-browser';
import type { IChatItem, IUsageInfo, IAnnotation, IMcpApprovalRequest, IFileAttachment } from './chat';
import type { AppError } from './errors';
// Re-export types for convenience
export type { IChatItem, IUsageInfo, IAnnotation, IMcpApprovalRequest, IFileAttachment };
export interface ConversationSummary {
id: string;
title: string | null;
createdAt: number;
}
export interface ConversationMessageInfo {
role: string;
content: string;
}
/**
* Central application state structure
* All application state flows through this single source of truth
*/
export interface AppState {
// Authentication state
auth: {
status: 'initializing' | 'authenticated' | 'unauthenticated' | 'error';
user: AccountInfo | null;
error: string | null;
};
// Chat operations state
chat: {
status: 'idle' | 'sending' | 'streaming' | 'error';
messages: IChatItem[];
currentConversationId: string | null;
error: AppError | null;
streamingMessageId?: string;
recoveredInput?: string;
recoveredAttachments?: IFileAttachment[];
editSnapshot?: IChatItem[]; // messages removed during edit, for undo
regenerateText?: string;// auto-resend text for regenerate/edit flows
pendingMessages: Array<{ text: string; files?: File[] }>;
};
// Conversation history state
conversations: {
list: ConversationSummary[];
isLoading: boolean;
sidebarOpen: boolean;
hasMore: boolean;
};
// UI coordination state
ui: {
chatInputEnabled: boolean; // Disable during streaming/errors
};
}
/**
* All possible actions that can modify application state
* Use discriminated unions for type safety
*/
export type AppAction =
// Auth actions
| { type: 'AUTH_INITIALIZED'; user: AccountInfo }
| { type: 'AUTH_TOKEN_EXPIRED' }
// Chat actions
| { type: 'CHAT_SEND_MESSAGE'; message: IChatItem }
| { type: 'CHAT_LOAD_MESSAGES'; messages: IChatItem[] }
| { type: 'CHAT_START_STREAM'; conversationId?: string; messageId: string }
| { type: 'CHAT_STREAM_CHUNK'; messageId: string; content: string }
| { type: 'CHAT_STREAM_ANNOTATIONS'; messageId: string; annotations: IAnnotation[] }
| { type: 'CHAT_STREAM_TOOL_USE'; messageId: string; toolName: string }
| { type: 'CHAT_MCP_APPROVAL_REQUEST'; messageId: string; approvalRequest: IMcpApprovalRequest; previousResponseId: string | null }
| { type: 'CHAT_MCP_APPROVAL_RESOLVED'; approvalRequestId: string; resolved?: 'approved' | 'rejected' }
| { type: 'CHAT_STREAM_COMPLETE'; usage: IUsageInfo }
| { type: 'CHAT_CANCEL_STREAM' }
| { type: 'CHAT_ERROR'; error: AppError } // Enhanced error object
| { type: 'CHAT_CLEAR_ERROR' } // Clear error state
| { type: 'CHAT_CLEAR' }
| { type: 'CHAT_ADD_ASSISTANT_MESSAGE'; messageId: string }
| { type: 'CHAT_LOAD_CONVERSATION'; conversationId: string; messages: IChatItem[] }
| { type: 'CHAT_STREAM_RETRY'; messageId: string; attempt: number; maxRetries: number }
| { type: 'CHAT_RECOVER_MESSAGE'; messageText: string; error: AppError; retryCount: number }
| { type: 'CHAT_CONSUMED_RECOVERED_INPUT' }
| { type: 'CHAT_QUEUE_MESSAGE'; text: string; files?: File[] }
| { type: 'CHAT_DEQUEUE_MESSAGE'; index: number }
| { type: 'CHAT_CLEAR_QUEUE' }
| { type: 'CHAT_REGENERATE' }
| { type: 'CHAT_EDIT_MESSAGE'; messageId: string; newText: string }
| { type: 'CHAT_CANCEL_EDIT' }
| { type: 'CHAT_CONSUMED_REGENERATE' }
// Conversation history actions
| { type: 'CONVERSATIONS_SET_LIST'; conversations: ConversationSummary[]; hasMore: boolean; append?: boolean }
| { type: 'CONVERSATIONS_LOADING' }
| { type: 'CONVERSATIONS_LOADING_DONE' }
| { type: 'CONVERSATIONS_TOGGLE_SIDEBAR' }
| { type: 'CONVERSATIONS_REMOVE'; conversationId: string };
/**
* Initial state for the application
*/
export const initialAppState: AppState = {
auth: {
status: 'initializing',
user: null,
error: null,
},
chat: {
status: 'idle',
messages: [],
currentConversationId: null,
error: null,
streamingMessageId: undefined,
recoveredInput: undefined,
recoveredAttachments: undefined,
editSnapshot: undefined,
regenerateText: undefined,
pendingMessages: [],
},
conversations: {
list: [],
isLoading: false,
sidebarOpen: false,
hasMore: false,
},
ui: {
chatInputEnabled: true,
},
};