瀏覽代碼

Fix errors for RangeErrors, Fix for accessing undefined objects in Chat.svelte

Vojtěch Šiler 4 月之前
父節點
當前提交
70a6a0d9e8
共有 2 個文件被更改,包括 21 次插入17 次删除
  1. 11 9
      src/lib/components/chat/Chat.svelte
  2. 10 8
      src/lib/components/common/RichTextInput/AutoCompletion.js

+ 11 - 9
src/lib/components/chat/Chat.svelte

@@ -839,13 +839,15 @@
 		if (res !== null && res.messages) {
 		if (res !== null && res.messages) {
 			// Update chat history with the new messages
 			// Update chat history with the new messages
 			for (const message of res.messages) {
 			for (const message of res.messages) {
-				history.messages[message.id] = {
-					...history.messages[message.id],
-					...(history.messages[message.id].content !== message.content
-						? { originalContent: history.messages[message.id].content }
-						: {}),
-					...message
-				};
+				if (message && message.id) {  // Add null check for message and message.id
+					history.messages[message.id] = {
+						...history.messages[message.id],
+						...(history.messages[message.id].content !== message.content
+							? { originalContent: history.messages[message.id].content }
+							: {}),
+						...message
+					};
+				}
 			}
 			}
 		}
 		}
 
 
@@ -1348,7 +1350,7 @@
 				history.currentId = responseMessageId;
 				history.currentId = responseMessageId;
 
 
 				// Append messageId to childrenIds of parent message
 				// Append messageId to childrenIds of parent message
-				if (parentId !== null) {
+				if (parentId !== null && history.messages[parentId]) { // Add null check before accessing childrenIds
 					history.messages[parentId].childrenIds = [
 					history.messages[parentId].childrenIds = [
 						...history.messages[parentId].childrenIds,
 						...history.messages[parentId].childrenIds,
 						responseMessageId
 						responseMessageId
@@ -1526,7 +1528,7 @@
 							: undefined
 							: undefined
 				},
 				},
 
 
-				files: files.length > 0 ? files : undefined,
+				files: (files?.length ?? 0) > 0 ? files : undefined,
 				tool_ids: selectedToolIds.length > 0 ? selectedToolIds : undefined,
 				tool_ids: selectedToolIds.length > 0 ? selectedToolIds : undefined,
 				features: {
 				features: {
 					web_search: webSearchEnabled
 					web_search: webSearchEnabled

+ 10 - 8
src/lib/components/common/RichTextInput/AutoCompletion.js

@@ -158,14 +158,16 @@ export const AIAutocompletion = Extension.create({
 															if (
 															if (
 																view.state.selection.$head.pos === view.state.selection.$head.end()
 																view.state.selection.$head.pos === view.state.selection.$head.end()
 															) {
 															) {
-																view.dispatch(
-																	newState.tr.setNodeMarkup(currentPos, null, {
-																		...newNode.attrs,
-																		class: 'ai-autocompletion',
-																		'data-prompt': prompt,
-																		'data-suggestion': suggestion
-																	})
-																);
+																if (view.state === newState) {
+																	view.dispatch(
+																		newState.tr.setNodeMarkup(currentPos, null, {
+																			...newNode.attrs,
+																			class: 'ai-autocompletion',
+																			'data-prompt': prompt,
+																			'data-suggestion': suggestion
+																		})
+																	);
+																}
 															}
 															}
 														}
 														}
 													})
 													})