mirror of
https://github.com/ikawrakow/ik_llama.cpp.git
synced 2026-02-06 14:30:09 +00:00
webui: add system message in export conversation, support upload conversation with system message Webui: show upload only when in new conversation Webui: Add model name webui: increase height of chat message window when clicking editing Webui: autoclose settings dialog dropdown and maximze screen width when zoom in webui: fix date issues and add more dates webui: change error to toast.error. server: add n_past and slot_id in props_simple webui: add cache tokens, context and prompt speed in chat webui: modernize ui webui: change welcome message webui: change speed display webui: change run python icon webui: add config to use server defaults for sampler webui: put speed on left and context on right webui: recognize AsciiDoc files as valid text files (#16850) * webui: recognize AsciiDoc files as valid text files * webui: add an updated static webui build * webui: add the updated dependency list * webui: re-add an updated static webui build Add a setting to display message generation statistics (#16901) * feat: Add setting to display message generation statistics * chore: build static webui output webui: add HTML/JS preview support to MarkdownContent with sandboxed iframe (#16757) * webui: add HTML/JS preview support to MarkdownContent with sandboxed iframe dialog Extended MarkdownContent to flag previewable code languages, add a preview button alongside copy controls, manage preview dialog state, and share styling for the new button group Introduced CodePreviewDialog.svelte, a sandboxed iframe modal for rendering HTML/JS previews with consistent dialog controls * webui: fullscreen HTML preview dialog using bits-ui * Update tools/server/webui/src/lib/components/app/misc/CodePreviewDialog.svelte Co-authored-by: Aleksander Grygier <aleksander.grygier@gmail.com> * Update tools/server/webui/src/lib/components/app/misc/MarkdownContent.svelte Co-authored-by: Aleksander Grygier <aleksander.grygier@gmail.com> * webui: pedantic style tweak for CodePreviewDialog close button * webui: remove overengineered preview language logic * chore: update webui static build --------- Co-authored-by: Aleksander Grygier <aleksander.grygier@gmail.com> webui: auto-refresh /props on inference start to resync model metadata (#16784) * webui: auto-refresh /props on inference start to resync model metadata - Add no-cache headers to /props and /slots - Throttle slot checks to 30s - Prevent concurrent fetches with promise guard - Trigger refresh from chat streaming for legacy and ModelSelector - Show dynamic serverWarning when using cached data * fix: restore proper legacy behavior in webui by using unified /props refresh Updated assistant message bubbles to show each message's stored model when available, falling back to the current server model only when the per-message value is missing When the model selector is disabled, now fetches /props and prioritizes that model name over chunk metadata, then persists it with the streamed message so legacy mode properly reflects the backend configuration * fix: detect first valid SSE chunk and refresh server props once * fix: removed the slots availability throttle constant and state * webui: purge ai-generated cruft * chore: update webui static build feat(webui): improve LaTeX rendering with currency detection (#16508) * webui : Revised LaTeX formula recognition * webui : Further examples containg amounts * webui : vitest for maskInlineLaTeX * webui: Moved preprocessLaTeX to lib/utils * webui: LaTeX in table-cells * chore: update webui build output (use theirs) * webui: backslash in LaTeX-preprocessing * chore: update webui build output * webui: look-behind backslash-check * chore: update webui build output * Apply suggestions from code review Code maintenance (variable names, code formatting, string handling) Co-authored-by: Aleksander Grygier <aleksander.grygier@gmail.com> * webui: Moved constants to lib/constants. * webui: package woff2 inside base64 data * webui: LaTeX-line-break in display formula * chore: update webui build output * webui: Bugfix (font embedding) * webui: Bugfix (font embedding) * webui: vite embeds assets * webui: don't suppress 404 (fonts) * refactor: KaTeX integration with SCSS Moves KaTeX styling to SCSS for better customization and font embedding. This change includes: - Adding `sass` as a dev dependency. - Introducing a custom SCSS file to override KaTeX variables and disable TTF/WOFF fonts, relying solely on WOFF2 for embedding. - Adjusting the Vite configuration to resolve `katex-fonts` alias and inject SCSS variables. * fix: LaTeX processing within blockquotes * webui: update webui build output --------- Co-authored-by: Aleksander Grygier <aleksander.grygier@gmail.com> server : add props.model_alias (#16943) * server : add props.model_alias webui: fix keyboard shortcuts for new chat & edit chat title (#17007) Better UX for handling multiple attachments in WebUI (#17246) webui: add OAI-Compat Harmony tool-call streaming visualization and persistence in chat UI (#16618) * webui: add OAI-Compat Harmony tool-call live streaming visualization and persistence in chat UI - Purely visual and diagnostic change, no effect on model context, prompt construction, or inference behavior - Captured assistant tool call payloads during streaming and non-streaming completions, and persisted them in chat state and storage for downstream use - Exposed parsed tool call labels beneath the assistant's model info line with graceful fallback when parsing fails - Added tool call badges beneath assistant responses that expose JSON tooltips and copy their payloads when clicked, matching the existing model badge styling - Added a user-facing setting to toggle tool call visibility to the Developer settings section directly under the model selector option * webui: remove scroll listener causing unnecessary layout updates (model selector) * Update tools/server/webui/src/lib/components/app/chat/ChatMessages/ChatMessageAssistant.svelte Co-authored-by: Aleksander Grygier <aleksander.grygier@gmail.com> * Update tools/server/webui/src/lib/components/app/chat/ChatMessages/ChatMessageAssistant.svelte Co-authored-by: Aleksander Grygier <aleksander.grygier@gmail.com> * chore: npm run format & update webui build output * chore: update webui build output --------- Co-authored-by: Aleksander Grygier <aleksander.grygier@gmail.com> webui: Fix clickability around chat processing statistics UI (#17278) * fix: Better pointer events handling in chat processing info elements * chore: update webui build output Fix merge error webui: Add a "Continue" Action for Assistant Message (#16971) * feat: Add "Continue" action for assistant messages * feat: Continuation logic & prompt improvements * chore: update webui build output * feat: Improve logic for continuing the assistant message * chore: update webui build output * chore: Linting * chore: update webui build output * fix: Remove synthetic prompt logic, use the prefill feature by sending the conversation payload ending with assistant message * chore: update webui build output * feat: Enable "Continue" button based on config & non-reasoning model type * chore: update webui build output * chore: Update packages with `npm audit fix` * fix: Remove redundant error * chore: update webui build output * chore: Update `.gitignore` * fix: Add missing change * feat: Add auto-resizing for Edit Assistant/User Message textareas * chore: update webui build output Improved file naming & structure for UI components (#17405) * refactor: Component iles naming & structure * chore: update webui build output * refactor: Dialog titles + components namig * chore: update webui build output * refactor: Imports * chore: update webui build output webui: hide border of button webui: update webui: update webui: update add vision webui: minor settings reorganization and add disable autoscroll option (#17452) * webui: added a dedicated 'Display' settings section that groups visualization options * webui: added a Display setting to toggle automatic chat scrolling * chore: update webui build output Co-authored-by: firecoperana <firecoperana>
315 lines
9.2 KiB
TypeScript
315 lines
9.2 KiB
TypeScript
import React, { useMemo, useState } from 'react';
|
|
import Markdown, { ExtraProps } from 'react-markdown';
|
|
import remarkGfm from 'remark-gfm';
|
|
import rehypeHightlight from 'rehype-highlight';
|
|
import rehypeKatex from 'rehype-katex';
|
|
import remarkMath from 'remark-math';
|
|
import remarkBreaks from 'remark-breaks';
|
|
import 'katex/dist/katex.min.css';
|
|
import { classNames, copyStr } from '../utils/misc';
|
|
import { ElementContent, Root } from 'hast';
|
|
import { visit } from 'unist-util-visit';
|
|
import { useAppContext } from '../utils/app.context';
|
|
import { CanvasType } from '../utils/types';
|
|
import { DocumentDuplicateIcon, PlayIcon } from '@heroicons/react/24/outline';
|
|
|
|
export default function MarkdownDisplay({
|
|
content,
|
|
isGenerating,
|
|
}: {
|
|
content: string;
|
|
isGenerating?: boolean;
|
|
}) {
|
|
const preprocessedContent = useMemo(
|
|
() => preprocessLaTeX(content),
|
|
[content]
|
|
);
|
|
return (
|
|
<Markdown
|
|
remarkPlugins={[remarkGfm, remarkMath, remarkBreaks]}
|
|
rehypePlugins={[rehypeHightlight, rehypeKatex, rehypeCustomCopyButton]}
|
|
components={{
|
|
button: (props) => (
|
|
<CodeBlockButtons
|
|
{...props}
|
|
isGenerating={isGenerating}
|
|
origContent={preprocessedContent}
|
|
/>
|
|
),
|
|
// note: do not use "pre", "p" or other basic html elements here, it will cause the node to re-render when the message is being generated (this should be a bug with react-markdown, not sure how to fix it)
|
|
}}
|
|
>
|
|
{preprocessedContent}
|
|
</Markdown>
|
|
);
|
|
}
|
|
|
|
const CodeBlockButtons: React.ElementType<
|
|
React.ClassAttributes<HTMLButtonElement> &
|
|
React.HTMLAttributes<HTMLButtonElement> &
|
|
ExtraProps & { origContent: string; isGenerating?: boolean }
|
|
> = ({ node, origContent, isGenerating }) => {
|
|
const { config } = useAppContext();
|
|
const startOffset = node?.position?.start.offset ?? 0;
|
|
const endOffset = node?.position?.end.offset ?? 0;
|
|
|
|
const copiedContent = useMemo(
|
|
() =>
|
|
origContent
|
|
.substring(startOffset, endOffset)
|
|
.replace(/^```[^\n]+\n/g, '')
|
|
.replace(/```$/g, ''),
|
|
[origContent, startOffset, endOffset]
|
|
);
|
|
|
|
const codeLanguage = useMemo(
|
|
() =>
|
|
origContent
|
|
.substring(startOffset, startOffset + 10)
|
|
.match(/^```([^\n]+)\n/)?.[1] ?? '',
|
|
[origContent, startOffset]
|
|
);
|
|
|
|
const canRunCode =
|
|
!isGenerating &&
|
|
config.pyIntepreterEnabled &&
|
|
codeLanguage.startsWith('py');
|
|
|
|
return (
|
|
<div
|
|
className={classNames({
|
|
'text-right sticky top-[7em] mb-2 mr-2 h-0': true,
|
|
'display-none': !node?.position,
|
|
})}
|
|
>
|
|
<CopyButton className="badge btn-mini" content={copiedContent} />
|
|
{canRunCode && (
|
|
<RunPyCodeButton
|
|
className="badge btn-mini ml-2"
|
|
content={copiedContent}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
|
|
export const CopyButton = ({
|
|
content,
|
|
className,
|
|
}: {
|
|
content: string;
|
|
className?: string;
|
|
}) => {
|
|
const [copied, setCopied] = useState(false);
|
|
return (
|
|
<button
|
|
className={className}
|
|
onClick={() => {
|
|
copyStr(content);
|
|
setCopied(true);
|
|
}}
|
|
onMouseLeave={() => setCopied(false)}
|
|
>
|
|
<DocumentDuplicateIcon className="h-4 w-4" />
|
|
{copied ? 'Copied!' : 'Copy'}
|
|
</button>
|
|
);
|
|
};
|
|
|
|
export const RunPyCodeButton = ({
|
|
content,
|
|
className,
|
|
}: {
|
|
content: string;
|
|
className?: string;
|
|
}) => {
|
|
const { setCanvasData } = useAppContext();
|
|
return (
|
|
<>
|
|
<button
|
|
className={className}
|
|
onClick={() =>
|
|
setCanvasData({
|
|
type: CanvasType.PY_INTERPRETER,
|
|
content,
|
|
})
|
|
}
|
|
>
|
|
<PlayIcon className="h-4 w-4" />
|
|
{"Run"}
|
|
</button>
|
|
</>
|
|
);
|
|
};
|
|
|
|
/**
|
|
* This injects the "button" element before each "pre" element.
|
|
* The actual button will be replaced with a react component in the MarkdownDisplay.
|
|
* We don't replace "pre" node directly because it will cause the node to re-render, which causes this bug: https://github.com/ggerganov/llama.cpp/issues/9608
|
|
*/
|
|
function rehypeCustomCopyButton() {
|
|
return function (tree: Root) {
|
|
visit(tree, 'element', function (node) {
|
|
if (node.tagName === 'pre' && !node.properties.visited) {
|
|
const preNode = { ...node };
|
|
// replace current node
|
|
preNode.properties.visited = 'true';
|
|
node.tagName = 'div';
|
|
node.properties = {};
|
|
// add node for button
|
|
const btnNode: ElementContent = {
|
|
type: 'element',
|
|
tagName: 'button',
|
|
properties: {},
|
|
children: [],
|
|
position: node.position,
|
|
};
|
|
node.children = [btnNode, preNode];
|
|
}
|
|
});
|
|
};
|
|
}
|
|
|
|
/**
|
|
* The part below is copied and adapted from:
|
|
* https://github.com/danny-avila/LibreChat/blob/main/client/src/utils/latex.ts
|
|
* (MIT License)
|
|
*/
|
|
|
|
// Regex to check if the processed content contains any potential LaTeX patterns
|
|
const containsLatexRegex =
|
|
/\\\(.*?\\\)|\\\[.*?\\\]|\$.*?\$|\\begin\{equation\}.*?\\end\{equation\}/;
|
|
|
|
// Regex for inline and block LaTeX expressions
|
|
const inlineLatex = new RegExp(/\\\((.+?)\\\)/, 'g');
|
|
const blockLatex = new RegExp(/\\\[(.*?[^\\])\\\]/, 'gs');
|
|
|
|
// Function to restore code blocks
|
|
const restoreCodeBlocks = (content: string, codeBlocks: string[]) => {
|
|
return content.replace(
|
|
/<<CODE_BLOCK_(\d+)>>/g,
|
|
(_, index) => codeBlocks[index]
|
|
);
|
|
};
|
|
|
|
// Regex to identify code blocks and inline code
|
|
const codeBlockRegex = /(```[\s\S]*?```|`.*?`)/g;
|
|
|
|
export const processLaTeX = (_content: string) => {
|
|
let content = _content;
|
|
// Temporarily replace code blocks and inline code with placeholders
|
|
const codeBlocks: string[] = [];
|
|
let index = 0;
|
|
content = content.replace(codeBlockRegex, (match) => {
|
|
codeBlocks[index] = match;
|
|
return `<<CODE_BLOCK_${index++}>>`;
|
|
});
|
|
|
|
// Escape dollar signs followed by a digit or space and digit
|
|
let processedContent = content.replace(/(\$)(?=\s?\d)/g, '\\$');
|
|
|
|
// If no LaTeX patterns are found, restore code blocks and return the processed content
|
|
if (!containsLatexRegex.test(processedContent)) {
|
|
return restoreCodeBlocks(processedContent, codeBlocks);
|
|
}
|
|
|
|
// Convert LaTeX expressions to a markdown compatible format
|
|
processedContent = processedContent
|
|
.replace(inlineLatex, (_: string, equation: string) => `$${equation}$`) // Convert inline LaTeX
|
|
.replace(blockLatex, (_: string, equation: string) => `$$${equation}$$`); // Convert block LaTeX
|
|
|
|
// Restore code blocks
|
|
return restoreCodeBlocks(processedContent, codeBlocks);
|
|
};
|
|
|
|
/**
|
|
* Preprocesses LaTeX content by replacing delimiters and escaping certain characters.
|
|
*
|
|
* @param content The input string containing LaTeX expressions.
|
|
* @returns The processed string with replaced delimiters and escaped characters.
|
|
*/
|
|
export function preprocessLaTeX(content: string): string {
|
|
// Step 1: Protect code blocks
|
|
const codeBlocks: string[] = [];
|
|
content = content.replace(/(```[\s\S]*?```|`[^`\n]+`)/g, (_, code) => {
|
|
codeBlocks.push(code);
|
|
return `<<CODE_BLOCK_${codeBlocks.length - 1}>>`;
|
|
});
|
|
|
|
// Step 2: Protect existing LaTeX expressions
|
|
const latexExpressions: string[] = [];
|
|
|
|
// Protect block math ($$...$$), \[...\], and \(...\) as before.
|
|
content = content.replace(
|
|
/(\$\$[\s\S]*?\$\$|\\\[[\s\S]*?\\\]|\\\(.*?\\\))/g,
|
|
(match) => {
|
|
latexExpressions.push(match);
|
|
return `<<LATEX_${latexExpressions.length - 1}>>`;
|
|
}
|
|
);
|
|
|
|
// Protect inline math ($...$) only if it does NOT match a currency pattern.
|
|
// We assume a currency pattern is one where the inner content is purely numeric (with optional decimals).
|
|
content = content.replace(/\$([^$]+)\$/g, (match, inner) => {
|
|
if (/^\s*\d+(?:\.\d+)?\s*$/.test(inner)) {
|
|
// This looks like a currency value (e.g. "$123" or "$12.34"),
|
|
// so don't protect it.
|
|
return match;
|
|
} else {
|
|
// Otherwise, treat it as a LaTeX expression.
|
|
latexExpressions.push(match);
|
|
return `<<LATEX_${latexExpressions.length - 1}>>`;
|
|
}
|
|
});
|
|
|
|
// Step 3: Escape dollar signs that are likely currency indicators.
|
|
// (Now that inline math is protected, this will only escape dollars not already protected)
|
|
content = content.replace(/\$(?=\d)/g, '\\$');
|
|
|
|
// Step 4: Restore LaTeX expressions
|
|
content = content.replace(
|
|
/<<LATEX_(\d+)>>/g,
|
|
(_, index) => latexExpressions[parseInt(index)]
|
|
);
|
|
|
|
// Step 5: Restore code blocks
|
|
content = content.replace(
|
|
/<<CODE_BLOCK_(\d+)>>/g,
|
|
(_, index) => codeBlocks[parseInt(index)]
|
|
);
|
|
|
|
// Step 6: Apply additional escaping functions
|
|
content = escapeBrackets(content);
|
|
content = escapeMhchem(content);
|
|
|
|
return content;
|
|
}
|
|
|
|
export function escapeBrackets(text: string): string {
|
|
const pattern =
|
|
/(```[\S\s]*?```|`.*?`)|\\\[([\S\s]*?[^\\])\\]|\\\((.*?)\\\)/g;
|
|
return text.replace(
|
|
pattern,
|
|
(
|
|
match: string,
|
|
codeBlock: string | undefined,
|
|
squareBracket: string | undefined,
|
|
roundBracket: string | undefined
|
|
): string => {
|
|
if (codeBlock != null) {
|
|
return codeBlock;
|
|
} else if (squareBracket != null) {
|
|
return `$$${squareBracket}$$`;
|
|
} else if (roundBracket != null) {
|
|
return `$${roundBracket}$`;
|
|
}
|
|
return match;
|
|
}
|
|
);
|
|
}
|
|
|
|
export function escapeMhchem(text: string) {
|
|
return text.replaceAll('$\\ce{', '$\\\\ce{').replaceAll('$\\pu{', '$\\\\pu{');
|
|
}
|