From 7a80e607851fea369c7f8155a78959e44baebe2f Mon Sep 17 00:00:00 2001 From: silentoplayz Date: Sun, 3 Aug 2025 22:36:08 -0400 Subject: [PATCH 1/2] 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. --- src/lib/apis/index.ts | 8 ++++---- src/lib/apis/ollama/index.ts | 2 +- src/lib/apis/users/index.ts | 2 +- src/lib/components/chat/MessageInput.svelte | 7 ++++++- src/lib/components/common/RichTextInput.svelte | 3 +++ src/lib/i18n/index.ts | 4 ++-- src/lib/utils/characters/index.ts | 10 +++++----- src/lib/utils/marked/katex-extension.ts | 12 ++++++------ src/lib/workers/pyodide.worker.ts | 2 +- 9 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/lib/apis/index.ts b/src/lib/apis/index.ts index ca5bad0061..86f4165c30 100644 --- a/src/lib/apis/index.ts +++ b/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; }; diff --git a/src/lib/apis/ollama/index.ts b/src/lib/apis/ollama/index.ts index 85e08ad4e1..e1d488cc68 100644 --- a/src/lib/apis/ollama/index.ts +++ b/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`, { diff --git a/src/lib/apis/users/index.ts b/src/lib/apis/users/index.ts index 68b5e58d82..282b5bdca8 100644 --- a/src/lib/apis/users/index.ts +++ b/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}`); diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 55a64fc2de..a442f5e158 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/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} diff --git a/src/lib/components/common/RichTextInput.svelte b/src/lib/components/common/RichTextInput.svelte index e14b09add4..7494074bc8 100644 --- a/src/lib/components/common/RichTextInput.svelte +++ b/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(() => { diff --git a/src/lib/i18n/index.ts b/src/lib/i18n/index.ts index 4156e68e91..e5c2d183b2 100644 --- a/src/lib/i18n/index.ts +++ b/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`); diff --git a/src/lib/utils/characters/index.ts b/src/lib/utils/characters/index.ts index af3436693a..dff08e66fc 100644 --- a/src/lib/utils/characters/index.ts +++ b/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) | diff --git a/src/lib/utils/marked/katex-extension.ts b/src/lib/utils/marked/katex-extension.ts index 9e864ca185..8d7d0dc87d 100644 --- a/src/lib/utils/marked/katex-extension.ts +++ b/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); diff --git a/src/lib/workers/pyodide.worker.ts b/src/lib/workers/pyodide.worker.ts index 09fb0e1f34..221effca5e 100644 --- a/src/lib/workers/pyodide.worker.ts +++ b/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); From 55d3c07d85f23fc6a2291adc2699667ff7b1579f Mon Sep 17 00:00:00 2001 From: silentoplayz Date: Mon, 4 Aug 2025 08:42:48 -0400 Subject: [PATCH 2/2] revert: Reverts requested --- src/lib/components/chat/MessageInput.svelte | 7 +------ src/lib/components/common/RichTextInput.svelte | 3 --- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index a442f5e158..55a64fc2de 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -283,7 +283,7 @@ if (chatInput) { if ($settings?.richTextInput ?? true) { - word = getWordAtDocPosFromRichText(); + word = chatInputElement?.getWordAtDocPos(); } else { const cursor = chatInput ? chatInput.selectionStart : prompt.length; word = getWordAtCursor(prompt, cursor); @@ -384,8 +384,6 @@ let chatInputContainerElement; let chatInputElement; - let getWordAtDocPosFromRichText = () => ''; - let filesInputElement; let commandsElement; @@ -1094,9 +1092,6 @@ prompt = e.md; command = getCommand(); }} - getWordAtDocPosCallback={(func) => { - getWordAtDocPosFromRichText = func; - }} json={true} messageInput={true} showFormattingButtons={false} diff --git a/src/lib/components/common/RichTextInput.svelte b/src/lib/components/common/RichTextInput.svelte index 7494074bc8..e14b09add4 100644 --- a/src/lib/components/common/RichTextInput.svelte +++ b/src/lib/components/common/RichTextInput.svelte @@ -168,7 +168,6 @@ }); }; - export let getWordAtDocPosCallback = (func) => {}; export let onSelectionUpdate = (e) => {}; export let id = ''; @@ -1209,8 +1208,6 @@ if (messageInput) { selectTemplate(); } - - getWordAtDocPosCallback(getWordAtDocPos); }); onDestroy(() => {