fix: embedding params escape
This commit is contained in:
@@ -32,24 +32,60 @@ export function escapeDollarNumber(text: string) {
|
|||||||
return escapedText;
|
return escapedText;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function escapeBrackets(text: string) {
|
export function escapeBrackets(text: string): string {
|
||||||
const pattern =
|
const codeRegex = /```[\s\S]*?```|`[^`\n]+`/g;
|
||||||
/(```[\S\s]*?```|`.*?`)|\\\[([\S\s]*?[^\\])\\]|\\\((.*?)\\\)/g;
|
|
||||||
return text.replaceAll(
|
const codeBlocks: string[] = [];
|
||||||
pattern,
|
const placeholder = '%%CODE_BLOCK%%';
|
||||||
(match, codeBlock, squareBracket, roundBracket) => {
|
|
||||||
if (codeBlock) {
|
// 1. replace code blocks with a placeholder
|
||||||
return codeBlock;
|
// and store them in an array
|
||||||
} else if (squareBracket) {
|
const temp = text.replace(codeRegex, (match) => {
|
||||||
return `$$${squareBracket}$$`;
|
codeBlocks.push(match);
|
||||||
} else if (roundBracket) {
|
return placeholder;
|
||||||
return `$${roundBracket}$`;
|
});
|
||||||
}
|
|
||||||
return match;
|
// 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{');
|
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(/(?<!\\)\\ce\{/g, '\\\\ce{')
|
||||||
|
.replace(/(?<!\\)\\pu\{/g, '\\\\pu{');
|
||||||
|
return `$${fixed}$`;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 3. Restore code blocks
|
||||||
|
let i = 0;
|
||||||
|
return escaped.replace(
|
||||||
|
new RegExp(placeholder, 'g'),
|
||||||
|
() => codeBlocks[i++] || ''
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -276,10 +276,8 @@ const AddModal: FC<AddModalProps> = (props) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleOnSelectModelAfterEvaluate = (item: any) => {
|
const handleOnSelectModelAfterEvaluate = (item: any) => {
|
||||||
console.log('handleOnSelectModelAfterEvaluate', item);
|
setIsGGUF(item.isGGUF);
|
||||||
if (item.isGGUF) {
|
setSelectedModel(item);
|
||||||
return;
|
|
||||||
}
|
|
||||||
const modelInfo = onSelectModel(item, props.source);
|
const modelInfo = onSelectModel(item, props.source);
|
||||||
if (
|
if (
|
||||||
evaluateStateRef.current.state === EvaluateProccess.model &&
|
evaluateStateRef.current.state === EvaluateProccess.model &&
|
||||||
|
|||||||
@@ -292,7 +292,8 @@ const ModelCard: React.FC<{
|
|||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!props.selectedModel) return;
|
console.log('ModelCard selectedModel changed', props.selectedModel);
|
||||||
|
if (!props.selectedModel.name) return;
|
||||||
|
|
||||||
getModelCardData();
|
getModelCardData();
|
||||||
setIsGGUFModel(props.selectedModel.isGGUF);
|
setIsGGUFModel(props.selectedModel.isGGUF);
|
||||||
|
|||||||
@@ -343,7 +343,8 @@ const SearchModel: React.FC<SearchInputProps> = (props) => {
|
|||||||
(item) => item.id === currentRef.current
|
(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);
|
onSelectModelAfterEvaluate(currentItem);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -305,7 +305,6 @@ const MessageInput: React.FC<MessageInputProps> = forwardRef(
|
|||||||
uid: updateUidCount(),
|
uid: updateUidCount(),
|
||||||
format: audioTypeMap[data.file.type] as AudioFormat,
|
format: audioTypeMap[data.file.type] as AudioFormat,
|
||||||
base64: base64Audio.split(',')[1],
|
base64: base64Audio.split(',')[1],
|
||||||
name: audioData.name,
|
|
||||||
data: _.pick(audioData, ['url', 'name', 'duration'])
|
data: _.pick(audioData, ['url', 'name', 'duration'])
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -519,8 +518,8 @@ const MessageInput: React.FC<MessageInputProps> = forwardRef(
|
|||||||
<AudioWrapper>
|
<AudioWrapper>
|
||||||
<SimpleAudio
|
<SimpleAudio
|
||||||
url={message.audio?.[0].data?.url}
|
url={message.audio?.[0].data?.url}
|
||||||
name={message.audio?.[0].name}
|
name={message.audio?.[0].data?.name}
|
||||||
height={44}
|
height={54}
|
||||||
onDelete={handleDeleteAudio}
|
onDelete={handleDeleteAudio}
|
||||||
></SimpleAudio>
|
></SimpleAudio>
|
||||||
</AudioWrapper>
|
</AudioWrapper>
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ const MessageBody: React.FC<MessageBodyProps> = forwardRef(
|
|||||||
url={data.audio?.[0]?.data.url}
|
url={data.audio?.[0]?.data.url}
|
||||||
name={data.audio?.[0]?.data.name}
|
name={data.audio?.[0]?.data.name}
|
||||||
actions={[]}
|
actions={[]}
|
||||||
height={44}
|
height={54}
|
||||||
></SimpleAudio>
|
></SimpleAudio>
|
||||||
</AudioWrapper>
|
</AudioWrapper>
|
||||||
)}
|
)}
|
||||||
@@ -250,7 +250,7 @@ const MessageBody: React.FC<MessageBodyProps> = forwardRef(
|
|||||||
url={data.audio?.[0]?.data.url}
|
url={data.audio?.[0]?.data.url}
|
||||||
name={data.audio?.[0]?.data.name}
|
name={data.audio?.[0]?.data.name}
|
||||||
onDelete={handleDeleteAudio}
|
onDelete={handleDeleteAudio}
|
||||||
height={44}
|
height={54}
|
||||||
></SimpleAudio>
|
></SimpleAudio>
|
||||||
</AudioWrapper>
|
</AudioWrapper>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ export interface AudioData {
|
|||||||
uid: string | number;
|
uid: string | number;
|
||||||
base64: string;
|
base64: string;
|
||||||
format: AudioFormat;
|
format: AudioFormat;
|
||||||
name?: string;
|
|
||||||
data: {
|
data: {
|
||||||
url: string;
|
url: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
|||||||
@@ -1,5 +1,15 @@
|
|||||||
import _ from 'lodash';
|
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
|
// curl format
|
||||||
export const formatCurlArgs = (
|
export const formatCurlArgs = (
|
||||||
parameters: Record<string, any>,
|
parameters: Record<string, any>,
|
||||||
@@ -9,11 +19,13 @@ export const formatCurlArgs = (
|
|||||||
return _.keys(parameters).reduce((acc: string, key: string) => {
|
return _.keys(parameters).reduce((acc: string, key: string) => {
|
||||||
const val = parameters[key];
|
const val = parameters[key];
|
||||||
const value =
|
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 acc + `-F ${key}="${value}" \\\n`;
|
||||||
}, '');
|
}, '');
|
||||||
}
|
}
|
||||||
return `-d '${JSON.stringify(parameters, null, 2)}'`;
|
return `-d '${toShellSafeMultilineJson(parameters)}'`;
|
||||||
};
|
};
|
||||||
|
|
||||||
// python format
|
// python format
|
||||||
|
|||||||
Reference in New Issue
Block a user