MCPcopy Create free account
hub / github.com/admineral/OpenAI-Assistant-API-Chat / sendMessage

Method sendMessage

app/services/ChatManager.ts:220–274  ·  view source on GitHub ↗
(input: string, files: File[], fileDetails: any[])

Source from the content-addressed store, hash-verified

218
219// Method to send a message
220async sendMessage(input: string, files: File[], fileDetails: any[]): Promise<void> { // Add a new parameter for the files
221 console.log('Sending message...');
222 this.state.setProgress(0);
223 this.state.isSending = true;
224 const newUserMessage = { role: 'user', content: input, fileDetails: fileDetails };
225 this.state.messages = [...this.state.messages, newUserMessage];
226 this.state.setChatMessages(this.state.messages);
227
228 try {
229 if (this.state.threadId && this.state.assistantId) {
230
231 // Upload the files if there are any
232 let ChatFileIds: string[] = [];
233 if (files.length > 0) {
234 this.state.setStatusMessage('Starting upload...');
235 console.log('Files in Chat:', files);
236 ChatFileIds = await Promise.all(files.map(file => prepareUploadFile(file, this.state.setStatusMessage)));
237 console.log('File IDs during Chat:', ChatFileIds);
238 if (ChatFileIds.map(String).includes('null')) {
239 throw new Error('One or more file IDs are null');
240 }
241 this.state.setStatusMessage('Upload complete..');
242 }
243 console.log('File IDs during Chat:', ChatFileIds);
244 // Submit the user's message
245 await submitUserMessage(input, this.state.threadId, this.state.setStatusMessage, ChatFileIds); // Pass the file IDs here
246 console.log('User message submitted. Running assistant...');
247
248 // Run the assistant
249 this.state.runId = await runChatAssistant(this.state.assistantId as string, this.state.threadId as string);
250 console.log('Assistant run successfully. Fetching assistant response...');
251
252 // Fetch the assistant's response
253 const response = await fetchAssistantResponse(this.state.runId as string, this.state.threadId as string, this.state.setStatusMessage, this.state.setProgress,0);
254 console.log('Assistant response fetched. Adding to chat state...');
255
256
257 // Add the assistant's response to the messages
258 const newAssistantMessage = { role: 'assistant', content: response };
259 this.state.messages = [...this.state.messages, newAssistantMessage];
260 this.state.setChatMessages(this.state.messages);
261
262 } else {
263 console.error('ThreadId or AssistantId is null');
264 }
265 } catch (error) {
266 // Handle any errors
267 this.state.error = error as Error;
268 console.error('Error in sending message:', error);
269 } finally {
270 // Finalize the operation
271 this.state.setProgress(0);
272 this.state.isSending = false;
273 }
274}
275
276 // Method to get the current chat state
277 getChatState(): ChatState {

Callers 1

handleFormSubmitFunction · 0.80

Calls 4

prepareUploadFileFunction · 0.90
submitUserMessageFunction · 0.90
runChatAssistantFunction · 0.90
fetchAssistantResponseFunction · 0.90

Tested by

no test coverage detected