Parcourir la source

fix(frontend): Attempt to resolve TypeError in RichTextInput.svelte

Fixes an issue where `ue.getWordAtDocPos is not a function` would be thrown in `MessageInput.svelte`.

The error was caused by a timing issue where the `getWordAtDocPos` method on the `RichTextInput` component was not available when called from an event handler within the same component.

This change refactors the code to pass the `getWordAtDocPos` function as a callback prop from `RichTextInput` to `MessageInput`, ensuring it's available when needed.
silentoplayz il y a 2 mois
Parent
commit
7a80e60785

+ 4 - 4
src/lib/apis/index.ts

@@ -465,7 +465,7 @@ export const executeToolServer = async (
 			...(token && { authorization: `Bearer ${token}` })
 		};
 
-		let requestOptions: RequestInit = {
+		const requestOptions: RequestInit = {
 			method: httpMethod.toUpperCase(),
 			headers
 		};
@@ -818,7 +818,7 @@ export const generateQueries = async (
 	model: string,
 	messages: object[],
 	prompt: string,
-	type?: string = 'web_search'
+	type: string = 'web_search'
 ) => {
 	let error = null;
 
@@ -1014,7 +1014,7 @@ export const getPipelinesList = async (token: string = '') => {
 		throw error;
 	}
 
-	let pipelines = res?.data ?? [];
+	const pipelines = res?.data ?? [];
 	return pipelines;
 };
 
@@ -1157,7 +1157,7 @@ export const getPipelines = async (token: string, urlIdx?: string) => {
 		throw error;
 	}
 
-	let pipelines = res?.data ?? [];
+	const pipelines = res?.data ?? [];
 	return pipelines;
 };
 

+ 1 - 1
src/lib/apis/ollama/index.ts

@@ -331,7 +331,7 @@ export const generateTextCompletion = async (token: string = '', model: string,
 };
 
 export const generateChatCompletion = async (token: string = '', body: object) => {
-	let controller = new AbortController();
+	const controller = new AbortController();
 	let error = null;
 
 	const res = await fetch(`${OLLAMA_API_BASE_URL}/api/chat`, {

+ 1 - 1
src/lib/apis/users/index.ts

@@ -126,7 +126,7 @@ export const getUsers = async (
 	let error = null;
 	let res = null;
 
-	let searchParams = new URLSearchParams();
+	const searchParams = new URLSearchParams();
 
 	searchParams.set('page', `${page}`);
 

+ 6 - 1
src/lib/components/chat/MessageInput.svelte

@@ -283,7 +283,7 @@
 
 		if (chatInput) {
 			if ($settings?.richTextInput ?? true) {
-				word = chatInputElement?.getWordAtDocPos();
+				word = getWordAtDocPosFromRichText();
 			} else {
 				const cursor = chatInput ? chatInput.selectionStart : prompt.length;
 				word = getWordAtCursor(prompt, cursor);
@@ -384,6 +384,8 @@
 	let chatInputContainerElement;
 	let chatInputElement;
 
+	let getWordAtDocPosFromRichText = () => '';
+
 	let filesInputElement;
 	let commandsElement;
 
@@ -1092,6 +1094,9 @@
 													prompt = e.md;
 													command = getCommand();
 												}}
+												getWordAtDocPosCallback={(func) => {
+													getWordAtDocPosFromRichText = func;
+												}}
 												json={true}
 												messageInput={true}
 												showFormattingButtons={false}

+ 3 - 0
src/lib/components/common/RichTextInput.svelte

@@ -168,6 +168,7 @@
 		});
 	};
 
+	export let getWordAtDocPosCallback = (func) => {};
 	export let onSelectionUpdate = (e) => {};
 
 	export let id = '';
@@ -1208,6 +1209,8 @@
 		if (messageInput) {
 			selectTemplate();
 		}
+
+		getWordAtDocPosCallback(getWordAtDocPos);
 	});
 
 	onDestroy(() => {

+ 2 - 2
src/lib/i18n/index.ts

@@ -38,10 +38,10 @@ const createIsLoadingStore = (i18n: i18nType) => {
 };
 
 export const initI18n = (defaultLocale?: string | undefined) => {
-	let detectionOrder = defaultLocale
+	const detectionOrder = defaultLocale
 		? ['querystring', 'localStorage']
 		: ['querystring', 'localStorage', 'navigator'];
-	let fallbackDefaultLocale = defaultLocale ? [defaultLocale] : ['en-US'];
+	const fallbackDefaultLocale = defaultLocale ? [defaultLocale] : ['en-US'];
 
 	const loadResource = (language: string, namespace: string) =>
 		import(`./locales/${language}/${namespace}.json`);

+ 5 - 5
src/lib/utils/characters/index.ts

@@ -74,15 +74,15 @@ const readPngChunks = (data) => {
 
 	if (!isValidPng) throw new Error('Invalid PNG file');
 
-	let chunks = [];
+	const chunks = [];
 	let offset = 8; // Skip PNG signature
 
 	while (offset < data.length) {
-		let length =
+		const length =
 			(data[offset] << 24) | (data[offset + 1] << 16) | (data[offset + 2] << 8) | data[offset + 3];
-		let type = String.fromCharCode.apply(null, data.slice(offset + 4, offset + 8));
-		let chunkData = data.slice(offset + 8, offset + 8 + length);
-		let crc =
+		const type = String.fromCharCode.apply(null, data.slice(offset + 4, offset + 8));
+		const chunkData = data.slice(offset + 8, offset + 8 + length);
+		const crc =
 			(data[offset + 8 + length] << 24) |
 			(data[offset + 8 + length + 1] << 16) |
 			(data[offset + 8 + length + 2] << 8) |

+ 6 - 6
src/lib/utils/marked/katex-extension.ts

@@ -23,8 +23,8 @@ const ALLOWED_SURROUNDING_CHARS =
 // const inlineRule = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])*?(?:\\.|[^\\\n\$]))\1(?=[\s?!\.,:?!。,:]|$)/;
 // const blockRule = /^(\${1,2})\n((?:\\[^]|[^\\])+?)\n\1(?:\n|$)/;
 
-let inlinePatterns = [];
-let blockPatterns = [];
+const inlinePatterns = [];
+const blockPatterns = [];
 
 function escapeRegex(string) {
 	return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
@@ -69,7 +69,7 @@ export default function (options = {}) {
 }
 
 function katexStart(src, displayMode: boolean) {
-	let ruleReg = displayMode ? blockRule : inlineRule;
+	const ruleReg = displayMode ? blockRule : inlineRule;
 
 	let indexSrc = src;
 
@@ -78,7 +78,7 @@ function katexStart(src, displayMode: boolean) {
 		let startIndex = -1;
 		let startDelimiter = '';
 		let endDelimiter = '';
-		for (let delimiter of DELIMITER_LIST) {
+		for (const delimiter of DELIMITER_LIST) {
 			if (delimiter.display !== displayMode) {
 				continue;
 			}
@@ -115,8 +115,8 @@ function katexStart(src, displayMode: boolean) {
 }
 
 function katexTokenizer(src, tokens, displayMode: boolean) {
-	let ruleReg = displayMode ? blockRule : inlineRule;
-	let type = displayMode ? 'blockKatex' : 'inlineKatex';
+	const ruleReg = displayMode ? blockRule : inlineRule;
+	const type = displayMode ? 'blockKatex' : 'inlineKatex';
 
 	const match = src.match(ruleReg);
 

+ 1 - 1
src/lib/workers/pyodide.worker.ts

@@ -40,7 +40,7 @@ async function loadPyodideAndPackages(packages: string[] = []) {
 		packages: ['micropip']
 	});
 
-	let mountDir = '/mnt';
+	const mountDir = '/mnt';
 	self.pyodide.FS.mkdirTree(mountDir);
 	// self.pyodide.FS.mount(self.pyodide.FS.filesystems.IDBFS, {}, mountDir);