-
Notifications
You must be signed in to change notification settings - Fork 479
/
aichatRedux.ts
345 lines (317 loc) · 11 KB
/
aichatRedux.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import moment from 'moment';
import {createSlice, PayloadAction, createAsyncThunk} from '@reduxjs/toolkit';
import {LabState} from '@cdo/apps/lab2/lab2Redux';
import Lab2Registry from '@cdo/apps/lab2/Lab2Registry';
const registerReducers = require('@cdo/apps/redux').registerReducers;
import {
DEFAULT_VISIBILITIES,
EMPTY_AI_CUSTOMIZATIONS,
AI_CUSTOMIZATIONS_LABELS,
} from '../views/modelCustomization/constants';
import {initialChatMessages} from '../constants';
import {getChatCompletionMessage} from '../chatApi';
import {
ChatCompletionMessage,
AichatLevelProperties,
Role,
AITutorInteractionStatus as Status,
AITutorInteractionStatusType,
AiCustomizations,
ModelCardInfo,
Visibility,
LevelAichatSettings,
} from '../types';
import {RootState} from '@cdo/apps/types/redux';
const haveDifferentValues = (
value1: AiCustomizations[keyof AiCustomizations],
value2: AiCustomizations[keyof AiCustomizations]
): boolean => {
if (typeof value1 === 'object' && typeof value2 === 'object') {
return JSON.stringify(value1) !== JSON.stringify(value2);
}
return value1 !== value2;
};
const findChangedProperties = (
previous: AiCustomizations | undefined,
next: AiCustomizations
) => {
if (!previous) {
return Object.keys(next);
}
const changedProperties: string[] = [];
Object.keys(next).forEach(key => {
const typedKey = key as keyof AiCustomizations;
if (haveDifferentValues(previous[typedKey], next[typedKey])) {
changedProperties.push(key);
}
});
return changedProperties;
};
const getCurrentTimestamp = () => moment(Date.now()).format('YYYY-MM-DD HH:mm');
const getCurrentTime = () => moment(Date.now()).format('LT');
export interface AichatState {
// All user and assistant chat messages - includes too personal and inappropriate user messages.
// Messages will be logged and stored.
chatMessages: ChatCompletionMessage[];
// Denotes whether we are waiting for a chat completion response from the backend
isWaitingForChatResponse: boolean;
// Denotes whether we should show the warning modal
showWarningModal: boolean;
// Denotes if there is an error with the chat completion response
chatMessageError: boolean;
currentAiCustomizations: AiCustomizations;
previouslySavedAiCustomizations?: AiCustomizations;
fieldVisibilities: {[key in keyof AiCustomizations]: Visibility};
}
const initialState: AichatState = {
chatMessages: initialChatMessages,
isWaitingForChatResponse: false,
showWarningModal: true,
chatMessageError: false,
currentAiCustomizations: EMPTY_AI_CUSTOMIZATIONS,
fieldVisibilities: DEFAULT_VISIBILITIES,
};
// THUNKS
// This thunk saves a student's AI customizations using the Project Manager (ie, to S3 typically),
// then does a comparison between the previous and current saved customizations in order to
// output a message to the chat window with the list of customizations that were updated.
export const updateAiCustomization = createAsyncThunk(
'aichat/updateAiCustomization',
async (_, thunkAPI) => {
const state = thunkAPI.getState() as RootState;
const {currentAiCustomizations, previouslySavedAiCustomizations} =
state.aichat;
// Remove any empty example topics on save
const trimmedExampleTopics =
currentAiCustomizations.modelCardInfo.exampleTopics.filter(
topic => topic.length
);
thunkAPI.dispatch(
setModelCardProperty({
property: 'exampleTopics',
value: trimmedExampleTopics,
})
);
const trimmedCurrentAiCustomizations = {
...currentAiCustomizations,
modelCardInfo: {
...currentAiCustomizations.modelCardInfo,
exampleTopics: trimmedExampleTopics,
},
};
await Lab2Registry.getInstance()
.getProjectManager()
?.save({source: JSON.stringify(trimmedCurrentAiCustomizations)}, true);
thunkAPI.dispatch(
setPreviouslySavedAiCustomizations(trimmedCurrentAiCustomizations)
);
const changedProperties = findChangedProperties(
previouslySavedAiCustomizations,
trimmedCurrentAiCustomizations
);
changedProperties.forEach(property => {
thunkAPI.dispatch(
addChatMessage({
id: 0,
role: Role.MODEL_UPDATE,
chatMessageText:
AI_CUSTOMIZATIONS_LABELS[property as keyof AiCustomizations],
status: Status.OK,
timestamp: getCurrentTime(),
})
);
});
}
);
// This thunk's callback function submits a user chat message to the chat completion endpoint,
// waits for a chat completion response, and updates the user message state.
export const submitChatMessage = createAsyncThunk(
'aichat/submitChatMessage',
async (message: string, thunkAPI) => {
const state = thunkAPI.getState() as {lab: LabState; aichat: AichatState};
const systemPrompt = (state.lab.levelProperties as AichatLevelProperties)
?.systemPrompt;
// TODO: move a check for undefined systemPrompt to AIchatView and throw an error dialog
// there if systemPrompt is undefined.
if (systemPrompt === undefined) {
throw new Error('systemPrompt is undefined');
}
const storedMessages = state.aichat.chatMessages;
const newMessageId =
storedMessages.length === 0
? 1
: storedMessages[storedMessages.length - 1].id + 1;
const appropriateChatMessages = storedMessages.filter(
msg => msg.status === Status.OK
);
// Create the new user ChatCompleteMessage and add to chatMessages.
const newMessage: ChatCompletionMessage = {
id: newMessageId,
role: Role.USER,
status: Status.UNKNOWN,
chatMessageText: message,
timestamp: getCurrentTimestamp(),
};
thunkAPI.dispatch(addChatMessage(newMessage));
// Send user message to backend and retrieve assistant response.
const chatApiResponse = await getChatCompletionMessage(
systemPrompt,
newMessageId,
message,
appropriateChatMessages
);
// Find message in chatMessages and update status.
thunkAPI.dispatch(
updateChatMessageStatus({
id: chatApiResponse.id,
status: chatApiResponse.status,
})
);
// Add assistant chat messages to chatMessages.
if (chatApiResponse.assistantResponse) {
const assistantChatMessage: ChatCompletionMessage = {
id: chatApiResponse.id + 1,
role: Role.ASSISTANT,
status: Status.OK,
chatMessageText: chatApiResponse.assistantResponse,
// The accuracy of this timestamp is debatable since it's not when our backend
// issued the message, but it's good enough for user testing.
timestamp: getCurrentTimestamp(),
};
thunkAPI.dispatch(addChatMessage(assistantChatMessage));
}
}
);
const aichatSlice = createSlice({
name: 'aichat',
initialState,
reducers: {
addChatMessage: (state, action: PayloadAction<ChatCompletionMessage>) => {
const newMessageId =
state.chatMessages[state.chatMessages.length - 1].id + 1;
const newMessage = {
...action.payload,
id: newMessageId,
};
state.chatMessages.push(newMessage);
},
removeChatMessage: (state, action: PayloadAction<number>) => {
const updatedMessages = state.chatMessages.filter(
message => message.id !== action.payload
);
if (updatedMessages.length !== state.chatMessages.length) {
state.chatMessages = updatedMessages;
}
},
clearChatMessages: state => {
state.chatMessages = initialChatMessages;
},
setIsWaitingForChatResponse: (state, action: PayloadAction<boolean>) => {
state.isWaitingForChatResponse = action.payload;
},
setShowWarningModal: (state, action: PayloadAction<boolean>) => {
state.showWarningModal = action.payload;
},
updateChatMessageStatus: (
state,
action: PayloadAction<{id: number; status: AITutorInteractionStatusType}>
) => {
const {id, status} = action.payload;
const chatMessage = state.chatMessages.find(msg => msg.id === id);
if (chatMessage) {
chatMessage.status = status;
}
},
setStartingAiCustomizations: (
state,
action: PayloadAction<{
levelAichatSettings?: LevelAichatSettings;
studentAiCustomizations: AiCustomizations;
}>
) => {
const {levelAichatSettings, studentAiCustomizations} = action.payload;
let reconciledAiCustomizations: AiCustomizations = {
...(levelAichatSettings?.initialCustomizations ||
EMPTY_AI_CUSTOMIZATIONS),
};
for (const customizationUntyped in reconciledAiCustomizations) {
const customization = customizationUntyped as keyof AiCustomizations;
if (
(levelAichatSettings?.visibilities || DEFAULT_VISIBILITIES)[
customization
] === Visibility.EDITABLE &&
studentAiCustomizations[customization]
) {
reconciledAiCustomizations = {
...reconciledAiCustomizations,
[customization]: studentAiCustomizations[customization],
};
}
}
state.previouslySavedAiCustomizations = reconciledAiCustomizations;
state.currentAiCustomizations = reconciledAiCustomizations;
state.fieldVisibilities =
levelAichatSettings?.visibilities || DEFAULT_VISIBILITIES;
},
setPreviouslySavedAiCustomizations: (
state,
action: PayloadAction<AiCustomizations>
) => {
state.previouslySavedAiCustomizations = action.payload;
},
setAiCustomizationProperty: (
state,
action: PayloadAction<{
property: keyof AiCustomizations;
value: AiCustomizations[typeof property];
}>
) => {
const {property, value} = action.payload;
const updatedAiCustomizations = {
...state.currentAiCustomizations,
[property]: value,
};
state.currentAiCustomizations = updatedAiCustomizations;
},
setModelCardProperty: (
state,
action: PayloadAction<{
property: keyof ModelCardInfo;
value: ModelCardInfo[typeof property];
}>
) => {
const {property, value} = action.payload;
const updatedModelCardInfo: ModelCardInfo = {
...state.currentAiCustomizations.modelCardInfo,
[property]: value,
};
state.currentAiCustomizations.modelCardInfo = updatedModelCardInfo;
},
},
extraReducers: builder => {
builder.addCase(submitChatMessage.fulfilled, state => {
state.isWaitingForChatResponse = false;
});
builder.addCase(submitChatMessage.rejected, (state, action) => {
state.isWaitingForChatResponse = false;
state.chatMessageError = true;
console.error(action.error);
});
builder.addCase(submitChatMessage.pending, state => {
state.isWaitingForChatResponse = true;
});
},
});
registerReducers({aichat: aichatSlice.reducer});
export const {
addChatMessage,
removeChatMessage,
clearChatMessages,
setIsWaitingForChatResponse,
setShowWarningModal,
updateChatMessageStatus,
setStartingAiCustomizations,
setPreviouslySavedAiCustomizations,
setAiCustomizationProperty,
setModelCardProperty,
} = aichatSlice.actions;