From 3e76d7c321eca17f9e2d43dca2296ba85474b0d1 Mon Sep 17 00:00:00 2001 From: jialin Date: Wed, 18 Jun 2025 15:02:16 +0800 Subject: [PATCH] fix: embedding params escape --- src/components/markdown-viewer/utils.ts | 68 ++++++++++++++----- .../llmodels/components/deploy-modal.tsx | 6 +- src/pages/llmodels/components/model-card.tsx | 3 +- .../llmodels/components/search-model.tsx | 3 +- .../playground/components/message-input.tsx | 5 +- .../components/multiple-chat/message-body.tsx | 4 +- src/pages/playground/config/types.ts | 1 - src/pages/playground/view-code/utils.ts | 16 ++++- 8 files changed, 76 insertions(+), 30 deletions(-) diff --git a/src/components/markdown-viewer/utils.ts b/src/components/markdown-viewer/utils.ts index a9b367a6..d21d8d99 100644 --- a/src/components/markdown-viewer/utils.ts +++ b/src/components/markdown-viewer/utils.ts @@ -32,24 +32,60 @@ export function escapeDollarNumber(text: string) { return escapedText; } -export function escapeBrackets(text: string) { - const pattern = - /(```[\S\s]*?```|`.*?`)|\\\[([\S\s]*?[^\\])\\]|\\\((.*?)\\\)/g; - return text.replaceAll( - pattern, - (match, codeBlock, squareBracket, roundBracket) => { - if (codeBlock) { - return codeBlock; - } else if (squareBracket) { - return `$$${squareBracket}$$`; - } else if (roundBracket) { - return `$${roundBracket}$`; - } - return match; - } +export function escapeBrackets(text: string): string { + const codeRegex = /```[\s\S]*?```|`[^`\n]+`/g; + + const codeBlocks: string[] = []; + const placeholder = '%%CODE_BLOCK%%'; + + // 1. replace code blocks with a placeholder + // and store them in an array + const temp = text.replace(codeRegex, (match) => { + codeBlocks.push(match); + return placeholder; + }); + + // 2. repace \[...\] with $$...$$,and replace \(...\) with $...$ + const replaced = temp + .replace(/\\\[([\s\S]*?)\\\]/g, (_, inner) => `$$${inner}$$`) + .replace(/\\\(([\s\S]*?)\\\)/g, (_, inner) => `$${inner}$`); + + // 3. restore code blocks from the placeholder + let i = 0; + return replaced.replace( + new RegExp(placeholder, 'g'), + () => codeBlocks[i++] || '' ); } -export function escapeMhchem(text: string) { +export function escapeMhchem_2(text: string) { return text.replaceAll('$\\ce{', '$\\\\ce{').replaceAll('$\\pu{', '$\\\\pu{'); } + +export function escapeMhchem(text: string): string { + const codeRegex = /```[\s\S]*?```|`[^`\n]+`/g; + const codeBlocks: string[] = []; + const placeholder = '%%CODE_BLOCK%%'; + + // 1. Replace code blocks with a placeholder + const temp = text.replace(codeRegex, (match) => { + codeBlocks.push(match); + return placeholder; + }); + + // 2. Only perform \ce{} / \pu{} escaping inside $...$ (to avoid incorrect escaping outside math environments) + const mathRegex = /\$(.+?)\$/gs; + const escaped = temp.replace(mathRegex, (_, content) => { + const fixed = content + .replace(/(? codeBlocks[i++] || '' + ); +} diff --git a/src/pages/llmodels/components/deploy-modal.tsx b/src/pages/llmodels/components/deploy-modal.tsx index 0705de97..62870948 100644 --- a/src/pages/llmodels/components/deploy-modal.tsx +++ b/src/pages/llmodels/components/deploy-modal.tsx @@ -276,10 +276,8 @@ const AddModal: FC = (props) => { }; const handleOnSelectModelAfterEvaluate = (item: any) => { - console.log('handleOnSelectModelAfterEvaluate', item); - if (item.isGGUF) { - return; - } + setIsGGUF(item.isGGUF); + setSelectedModel(item); const modelInfo = onSelectModel(item, props.source); if ( evaluateStateRef.current.state === EvaluateProccess.model && diff --git a/src/pages/llmodels/components/model-card.tsx b/src/pages/llmodels/components/model-card.tsx index fb22ffe2..b294e75c 100644 --- a/src/pages/llmodels/components/model-card.tsx +++ b/src/pages/llmodels/components/model-card.tsx @@ -292,7 +292,8 @@ const ModelCard: React.FC<{ ); useEffect(() => { - if (!props.selectedModel) return; + console.log('ModelCard selectedModel changed', props.selectedModel); + if (!props.selectedModel.name) return; getModelCardData(); setIsGGUFModel(props.selectedModel.isGGUF); diff --git a/src/pages/llmodels/components/search-model.tsx b/src/pages/llmodels/components/search-model.tsx index 19ada758..98a308c8 100644 --- a/src/pages/llmodels/components/search-model.tsx +++ b/src/pages/llmodels/components/search-model.tsx @@ -343,7 +343,8 @@ const SearchModel: React.FC = (props) => { (item) => item.id === currentRef.current ); - if (currentItem) { + // if it is gguf, would trigger a evaluation after select a model file + if (currentItem && !currentItem.isGGUF) { onSelectModelAfterEvaluate(currentItem); } } catch (error) { diff --git a/src/pages/playground/components/message-input.tsx b/src/pages/playground/components/message-input.tsx index 2cbeb242..a810a156 100644 --- a/src/pages/playground/components/message-input.tsx +++ b/src/pages/playground/components/message-input.tsx @@ -305,7 +305,6 @@ const MessageInput: React.FC = forwardRef( uid: updateUidCount(), format: audioTypeMap[data.file.type] as AudioFormat, base64: base64Audio.split(',')[1], - name: audioData.name, data: _.pick(audioData, ['url', 'name', 'duration']) } ] @@ -519,8 +518,8 @@ const MessageInput: React.FC = forwardRef( diff --git a/src/pages/playground/components/multiple-chat/message-body.tsx b/src/pages/playground/components/multiple-chat/message-body.tsx index e70fd1f4..c1e7b2d2 100644 --- a/src/pages/playground/components/multiple-chat/message-body.tsx +++ b/src/pages/playground/components/multiple-chat/message-body.tsx @@ -219,7 +219,7 @@ const MessageBody: React.FC = forwardRef( url={data.audio?.[0]?.data.url} name={data.audio?.[0]?.data.name} actions={[]} - height={44} + height={54} > )} @@ -250,7 +250,7 @@ const MessageBody: React.FC = forwardRef( url={data.audio?.[0]?.data.url} name={data.audio?.[0]?.data.name} onDelete={handleDeleteAudio} - height={44} + height={54} > )} diff --git a/src/pages/playground/config/types.ts b/src/pages/playground/config/types.ts index fa36b3e6..bc86b594 100644 --- a/src/pages/playground/config/types.ts +++ b/src/pages/playground/config/types.ts @@ -19,7 +19,6 @@ export interface AudioData { uid: string | number; base64: string; format: AudioFormat; - name?: string; data: { url: string; name: string; diff --git a/src/pages/playground/view-code/utils.ts b/src/pages/playground/view-code/utils.ts index aace8adb..8c484836 100644 --- a/src/pages/playground/view-code/utils.ts +++ b/src/pages/playground/view-code/utils.ts @@ -1,5 +1,15 @@ import _ from 'lodash'; +function toShellSafeMultilineJson(value: any): string { + const json = JSON.stringify(value, null, 2); + const escaped = json.replace(/'/g, `'\\''`); + return `${escaped}`; +} + +function toShellSafeJson(value: any): string { + return JSON.stringify(value, null, 2).replace(/'/g, `'\\''`); +} + // curl format export const formatCurlArgs = ( parameters: Record, @@ -9,11 +19,13 @@ export const formatCurlArgs = ( return _.keys(parameters).reduce((acc: string, key: string) => { const val = parameters[key]; const value = - typeof val === 'object' ? JSON.stringify(val, null, 2) : `${val}`; + typeof val === 'object' + ? toShellSafeMultilineJson(val) + : `${toShellSafeJson(val)}`; return acc + `-F ${key}="${value}" \\\n`; }, ''); } - return `-d '${JSON.stringify(parameters, null, 2)}'`; + return `-d '${toShellSafeMultilineJson(parameters)}'`; }; // python format