elements with a container that includes:
+ * - Language label
+ * - Copy button
+ * - Preview button (for HTML code blocks)
+ */
+export const rehypeEnhanceCodeBlocks: Plugin<[], Root> = () => {
+ return (tree: Root) => {
+ visit(tree, 'element', (node: Element, index, parent) => {
+ if (node.tagName !== 'pre' || !parent || index === undefined) return;
+
+ const codeElement = node.children.find(
+ (child): child is Element => child.type === 'element' && child.tagName === 'code'
+ );
+
+ if (!codeElement) return;
+
+ const language = extractLanguage(codeElement);
+ const codeId = generateBlockId('code', 'idxCodeBlock');
+
+ codeElement.properties = {
+ ...codeElement.properties,
+ 'data-code-id': codeId
+ };
+
+ const actions: Element[] = [createCopyButton(codeId, 'data-code-id', 'Copy code')];
+
+ if (language.toLowerCase() === 'html') {
+ actions.push(createPreviewButton(codeId, 'data-code-id', 'Preview code'));
+ }
+
+ const header = createBlockHeader(language, codeId, 'data-code-id', actions);
+ const wrapper = createWrapper(
+ header,
+ node,
+ CODE_BLOCK_WRAPPER_CLASS,
+ CODE_BLOCK_SCROLL_CONTAINER_CLASS
+ );
+
+ // Replace pre with wrapper in parent
+ (parent.children as ElementContent[])[index] = wrapper;
+ });
+ };
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-links.ts b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-links.ts
new file mode 100644
index 0000000000000000000000000000000000000000..b5fbcbdaae73a2ccaa40b13f01cc7ae5992592b4
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-links.ts
@@ -0,0 +1,33 @@
+/**
+ * Rehype plugin to enhance links with security attributes.
+ *
+ * Adds target="_blank" and rel="noopener noreferrer" to all anchor elements,
+ * ensuring external links open in new tabs safely.
+ */
+
+import type { Plugin } from 'unified';
+import type { Root, Element } from 'hast';
+import { visit } from 'unist-util-visit';
+
+/**
+ * Rehype plugin that adds security attributes to all links.
+ * This plugin ensures external links open in new tabs safely by adding:
+ * - target="_blank"
+ * - rel="noopener noreferrer"
+ */
+export const rehypeEnhanceLinks: Plugin<[], Root> = () => {
+ return (tree: Root) => {
+ visit(tree, 'element', (node: Element) => {
+ if (node.tagName !== 'a') return;
+
+ const props = node.properties ?? {};
+
+ // Only modify if href exists
+ if (!props.href) return;
+
+ props.target = '_blank';
+ props.rel = 'noopener noreferrer';
+ node.properties = props;
+ });
+ };
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-mermaid-blocks.ts b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-mermaid-blocks.ts
new file mode 100644
index 0000000000000000000000000000000000000000..4007c20a19d76ffca05c91cba4c2120723c0dcfe
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-mermaid-blocks.ts
@@ -0,0 +1,106 @@
+/**
+ * Rehype plugin to enhance mermaid diagram blocks with wrapper, header, and action buttons.
+ *
+ * Wraps elements with a container that includes:
+ * - Language label ("mermaid")
+ * - Copy button (copies mermaid syntax to clipboard)
+ * - Preview button (opens fullscreen preview dialog)
+ *
+ * This operates directly on the HAST tree for better performance,
+ * avoiding the need to stringify and re-parse HTML.
+ */
+
+import type { Plugin } from 'unified';
+import type { Root, Element, ElementContent } from 'hast';
+import { visit } from 'unist-util-visit';
+import {
+ MERMAID_WRAPPER_CLASS,
+ MERMAID_SCROLL_CONTAINER_CLASS,
+ MERMAID_BLOCK_CLASS,
+ MERMAID_LANGUAGE,
+ MERMAID_SYNTAX_ATTR,
+ MERMAID_ID_ATTR,
+ DIAGRAM_VIEW_MODE_ATTR,
+ DIAGRAM_VIEW_RENDERED
+} from '$lib/constants';
+import type { DiagramPreData } from './pre-transform';
+import {
+ createBlockHeader,
+ createCopyButton,
+ createPreviewButton,
+ createToggleSourceButton,
+ createSourceView,
+ createWrapper,
+ generateBlockId
+} from './code-block-utils';
+
+declare global {
+ interface Window {
+ idxMermaidBlock?: number;
+ }
+}
+
+/**
+ * Rehype plugin to enhance mermaid diagram blocks with wrapper, header, and action buttons.
+ * This plugin wraps elements with a container that includes:
+ * - Language label ("mermaid")
+ * - Copy button
+ * - Preview button
+ */
+export const rehypeEnhanceMermaidBlocks: Plugin<[], Root> = () => {
+ return (tree: Root) => {
+ visit(tree, 'element', (node: Element, index, parent) => {
+ if (node.tagName !== 'pre' || !parent || index === undefined) return;
+
+ const className = node.properties?.className;
+ if (!Array.isArray(className)) return;
+
+ const isMermaid = className.some(
+ (cls) => typeof cls === 'string' && cls === MERMAID_BLOCK_CLASS
+ );
+
+ if (!isMermaid) return;
+
+ const mermaidId = generateBlockId(MERMAID_LANGUAGE, 'idxMermaidBlock');
+
+ // Extract the mermaid syntax (text content of the pre element)
+ const diagramText = node.children
+ .map((child) => {
+ if (child.type === 'text') return child.value;
+ return '';
+ })
+ .join('');
+
+ // Store the mermaid syntax in data attribute for copy functionality
+ node.properties = {
+ ...node.properties,
+ [MERMAID_SYNTAX_ATTR]: diagramText,
+ [MERMAID_ID_ATTR]: mermaidId
+ };
+
+ const actions = [
+ createCopyButton(mermaidId, MERMAID_ID_ATTR, 'Copy mermaid syntax'),
+ createToggleSourceButton(mermaidId, MERMAID_ID_ATTR, 'Toggle mermaid source'),
+ createPreviewButton(mermaidId, MERMAID_ID_ATTR, 'Preview diagram')
+ ];
+
+ const header = createBlockHeader(MERMAID_LANGUAGE, mermaidId, MERMAID_ID_ATTR, actions);
+ const preservedCode = (node.data as DiagramPreData | undefined)?.sourceCode;
+ const sourceView = createSourceView(preservedCode, diagramText, MERMAID_LANGUAGE);
+ const wrapper = createWrapper(
+ header,
+ node,
+ MERMAID_WRAPPER_CLASS,
+ MERMAID_SCROLL_CONTAINER_CLASS,
+ {
+ [MERMAID_ID_ATTR]: mermaidId,
+ [DIAGRAM_VIEW_MODE_ATTR]: DIAGRAM_VIEW_RENDERED
+ },
+ [sourceView]
+ );
+
+ // Replace pre with wrapper in parent
+ (parent.children as ElementContent[])[index] = wrapper;
+ });
+ };
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-svg-blocks.ts b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-svg-blocks.ts
new file mode 100644
index 0000000000000000000000000000000000000000..55bcb6065fda13d0fd3fad23f905b28eace4b5e4
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-svg-blocks.ts
@@ -0,0 +1,96 @@
+/**
+ * Rehype plugin to enhance svg blocks with wrapper, header, and action buttons.
+ *
+ * Wraps elements with a container that includes:
+ * - Language label ("svg")
+ * - Copy button (copies svg source to clipboard)
+ * - Preview button (opens fullscreen preview dialog)
+ *
+ * Operates directly on the HAST tree and reuses the shared code-block builders.
+ */
+
+import type { Plugin } from 'unified';
+import type { Root, Element, ElementContent } from 'hast';
+import { visit } from 'unist-util-visit';
+import {
+ SVG_WRAPPER_CLASS,
+ SVG_SCROLL_CONTAINER_CLASS,
+ SVG_BLOCK_CLASS,
+ SVG_LANGUAGE,
+ SVG_SOURCE_ATTR,
+ SVG_ID_ATTR,
+ DIAGRAM_VIEW_MODE_ATTR,
+ DIAGRAM_VIEW_RENDERED
+} from '$lib/constants';
+import type { DiagramPreData } from './pre-transform';
+import {
+ createBlockHeader,
+ createCopyButton,
+ createPreviewButton,
+ createToggleSourceButton,
+ createSourceView,
+ createWrapper,
+ generateBlockId
+} from './code-block-utils';
+
+declare global {
+ interface Window {
+ idxSvgBlock?: number;
+ }
+}
+
+export const rehypeEnhanceSvgBlocks: Plugin<[], Root> = () => {
+ return (tree: Root) => {
+ visit(tree, 'element', (node: Element, index, parent) => {
+ if (node.tagName !== 'pre' || !parent || index === undefined) return;
+
+ const className = node.properties?.className;
+ if (!Array.isArray(className)) return;
+
+ const isSvg = className.some((cls) => typeof cls === 'string' && cls === SVG_BLOCK_CLASS);
+
+ if (!isSvg) return;
+
+ const svgId = generateBlockId(SVG_LANGUAGE, 'idxSvgBlock');
+
+ // Extract the svg source (text content of the pre element)
+ const svgSource = node.children
+ .map((child) => {
+ if (child.type === 'text') return child.value;
+ return '';
+ })
+ .join('');
+
+ // Store the svg source in data attribute for copy and render
+ node.properties = {
+ ...node.properties,
+ [SVG_SOURCE_ATTR]: svgSource,
+ [SVG_ID_ATTR]: svgId
+ };
+
+ const actions = [
+ createCopyButton(svgId, SVG_ID_ATTR, 'Copy svg source'),
+ createToggleSourceButton(svgId, SVG_ID_ATTR, 'Toggle svg source'),
+ createPreviewButton(svgId, SVG_ID_ATTR, 'Preview svg')
+ ];
+
+ const header = createBlockHeader(SVG_LANGUAGE, svgId, SVG_ID_ATTR, actions);
+ const preservedCode = (node.data as DiagramPreData | undefined)?.sourceCode;
+ const sourceView = createSourceView(preservedCode, svgSource, SVG_LANGUAGE);
+ const wrapper = createWrapper(
+ header,
+ node,
+ SVG_WRAPPER_CLASS,
+ SVG_SCROLL_CONTAINER_CLASS,
+ {
+ [SVG_ID_ATTR]: svgId,
+ [DIAGRAM_VIEW_MODE_ATTR]: DIAGRAM_VIEW_RENDERED
+ },
+ [sourceView]
+ );
+
+ // Replace pre with wrapper in parent
+ (parent.children as ElementContent[])[index] = wrapper;
+ });
+ };
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/mermaid-pre.ts b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/mermaid-pre.ts
new file mode 100644
index 0000000000000000000000000000000000000000..61322f045cf128fcc245bf6a4fe86553fa653d9e
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/mermaid-pre.ts
@@ -0,0 +1,7 @@
+import { createPreTransform } from './pre-transform';
+import { MERMAID_BLOCK_CLASS, MERMAID_LANGUAGE } from '$lib/constants';
+
+/**
+ * Converts mermaid code blocks to for client-side rendering.
+ */
+export const rehypeMermaidPre = createPreTransform(MERMAID_LANGUAGE, MERMAID_BLOCK_CLASS);
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/pre-transform.ts b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/pre-transform.ts
new file mode 100644
index 0000000000000000000000000000000000000000..7aa967bb81daa98e29688b024abe780dcc43d26a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/pre-transform.ts
@@ -0,0 +1,91 @@
+import type { Plugin } from 'unified';
+import type { Root, Element, ElementContent, Text } from 'hast';
+import { visit } from 'unist-util-visit';
+
+/**
+ * Metadata a diagram pre carries on its unist data field. The source code holds
+ * the highlighted code element captured before the pre became a render target,
+ * which the enhancer reuses to build a matching source view.
+ */
+export interface DiagramPreData {
+ sourceCode: Element;
+}
+
+/**
+ * Recursively extracts all text content from a HAST node.
+ * Handles nested elements (e.g., span wrappers from syntax highlighting).
+ */
+function extractText(node: ElementContent): string {
+ if (node.type === 'text') return node.value;
+ if (node.type === 'element') {
+ return (node.children ?? []).map(extractText).join('');
+ }
+ return '';
+}
+
+/**
+ * Builds a rehype plugin that converts
+ * blocks into elements carrying the raw text.
+ *
+ * Accepts one or more source languages, and an optional contentGuard that
+ * receives the trimmed text and decides whether the block qualifies. The guard
+ * lets a shared fence language be claimed only when its content matches, e.g.
+ * an xml block is converted to svg only when it starts with child, so rehypeEnhanceCodeBlocks skips it. Rendering
+ * happens client-side, so no markup is injected at this stage. Must run BEFORE
+ * rehypeEnhanceCodeBlocks.
+ */
+export function createPreTransform(
+ languages: string | string[],
+ targetClass: string,
+ contentGuard?: (text: string) => boolean
+): Plugin<[], Root> {
+ const codeClasses = (Array.isArray(languages) ? languages : [languages]).map(
+ (language) => `language-${language}`
+ );
+
+ return () => {
+ return (tree: Root) => {
+ visit(tree, 'element', (node: Element, index, parent) => {
+ if (node.tagName !== 'pre' || !parent || index === undefined) return;
+
+ const codeElement = node.children.find(
+ (child): child is Element => child.type === 'element' && child.tagName === 'code'
+ );
+
+ if (!codeElement) return;
+
+ const className = codeElement.properties?.className;
+ if (!Array.isArray(className)) return;
+
+ const matches = className.some(
+ (cls) => typeof cls === 'string' && codeClasses.includes(cls)
+ );
+
+ if (!matches) return;
+
+ // Recursively extract text to handle nested spans from syntax highlighting
+ const text = codeElement.children.map(extractText).join('').trim();
+
+ if (!text) return;
+
+ if (contentGuard && !contentGuard(text)) return;
+
+ const pre: Element = {
+ type: 'element',
+ tagName: 'pre',
+ properties: {
+ className: [targetClass]
+ },
+ children: [{ type: 'text', value: text } as Text],
+ // Keep the highlighted code element so the block can offer a source
+ // view that matches the app code blocks without re highlighting.
+ data: { sourceCode: codeElement } satisfies DiagramPreData
+ };
+
+ (parent.children as ElementContent[])[index] = pre;
+ });
+ };
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/rehype-rtl-support.ts b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/rehype-rtl-support.ts
new file mode 100644
index 0000000000000000000000000000000000000000..0a8b93ad547207d6ed71e840feee0391f7792f86
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/rehype-rtl-support.ts
@@ -0,0 +1,28 @@
+/**
+ * Rehype plugin to provide comprehensive RTL support by adding dir="auto"
+ * to all text-containing elements.
+ *
+ * This operates directly on the HAST tree, ensuring that all elements
+ * (including those not in a predefined list) receive the attribute.
+ */
+
+import type { Plugin } from 'unified';
+import type { Root, Element } from 'hast';
+import { visit } from 'unist-util-visit';
+
+/**
+ * Rehype plugin to add dir="auto" to all elements that have children.
+ * This provides bidirectional text support for mixed RTL/LTR content.
+ */
+export const rehypeRtlSupport: Plugin<[], Root> = () => {
+ return (tree: Root) => {
+ visit(tree, 'element', (node: Element) => {
+ if (node.children && node.children.length > 0) {
+ node.properties = {
+ ...node.properties,
+ dir: 'auto'
+ };
+ }
+ });
+ };
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/resolve-attachment-images.ts b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/resolve-attachment-images.ts
new file mode 100644
index 0000000000000000000000000000000000000000..36e7a3192bc1ac6ffed6ad81a96a14d80b26bf47
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/resolve-attachment-images.ts
@@ -0,0 +1,34 @@
+import type { Root as HastRoot } from 'hast';
+import { visit } from 'unist-util-visit';
+import type { DatabaseMessageExtra, DatabaseMessageExtraImageFile } from '$lib/types/database';
+import { AttachmentType, UrlProtocol } from '$lib/enums';
+
+/**
+ * Rehype plugin to resolve attachment image sources.
+ * Converts attachment names (e.g., "mcp-attachment-xxx.png") to base64 data URLs.
+ */
+export function rehypeResolveAttachmentImages(options: { attachments?: DatabaseMessageExtra[] }) {
+ return (tree: HastRoot) => {
+ visit(tree, 'element', (node) => {
+ if (node.tagName === 'img' && node.properties?.src) {
+ const src = String(node.properties.src);
+
+ // Skip data URLs and external URLs
+ if (src.startsWith(UrlProtocol.DATA) || src.startsWith(UrlProtocol.HTTP)) {
+ return;
+ }
+
+ // Find matching attachment
+ const attachment = options.attachments?.find(
+ (a): a is DatabaseMessageExtraImageFile =>
+ a.type === AttachmentType.IMAGE && a.name === src
+ );
+
+ // Replace with base64 URL if found
+ if (attachment?.base64Url) {
+ node.properties.src = attachment.base64Url;
+ }
+ }
+ });
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/svg-pre.ts b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/svg-pre.ts
new file mode 100644
index 0000000000000000000000000000000000000000..eb0e2c699bf5d5fcfe4623563b73e17eb6a41c20
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/svg-pre.ts
@@ -0,0 +1,13 @@
+import { createPreTransform } from './pre-transform';
+import { SVG_BLOCK_CLASS, SVG_LANGUAGE, XML_LANGUAGE, SVG_TAG_PREFIX } from '$lib/constants';
+
+/**
+ * Converts svg code blocks to for client-side rendering.
+ * Also claims xml blocks whose content starts with text.startsWith(SVG_TAG_PREFIX)
+);
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/table-html-restorer.ts b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/table-html-restorer.ts
new file mode 100644
index 0000000000000000000000000000000000000000..bc5d034653f20586a61878991242e833fb4258d4
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/rehype/table-html-restorer.ts
@@ -0,0 +1,181 @@
+/**
+ * Rehype plugin to restore limited HTML elements inside Markdown table cells.
+ *
+ * ## Problem
+ * The remark/rehype pipeline neutralizes inline HTML as literal text
+ * (remarkLiteralHtml) so that XML/HTML snippets in LLM responses display
+ * as-is instead of being rendered. This causes and markup in
+ * table cells to show as plain text.
+ *
+ * ## Solution
+ * This plugin traverses the HAST post-conversion, parses whitelisted HTML
+ * patterns from text nodes, and replaces them with actual HAST element nodes
+ * that will be rendered as real HTML.
+ *
+ * ## Supported HTML
+ * - ` ` / ` ` / ` ` - Line breaks (inline)
+ * - `` - Unordered lists (block)
+ *
+ * ## Key Implementation Details
+ *
+ * ### 1. Sibling Combination (Critical)
+ * The Markdown pipeline may fragment content across multiple text nodes and ` `
+ * elements. For example, `` might arrive as:
+ * - Text: `""`
+ * - Element: ` `
+ * - Text: `"a "`
+ *
+ * We must combine consecutive text nodes and ` ` elements into a single string
+ * before attempting to parse list markup. Without this, list detection fails.
+ *
+ * ### 2. visitParents for Deep Traversal
+ * Table cell content may be wrapped in intermediate elements (e.g., `` tags).
+ * Using `visitParents` instead of direct child iteration ensures we find text
+ * nodes at any depth within the cell.
+ *
+ * ### 3. Reference Comparison for No-Op Detection
+ * When checking if ` ` expansion changed anything, we compare:
+ * `expanded.length !== 1 || expanded[0] !== textNode`
+ *
+ * This catches both cases:
+ * - Multiple nodes created (text was split)
+ * - Single NEW node created (original had only ` `, now it's an element)
+ *
+ * A simple `length > 1` check would miss the single ` ` case.
+ *
+ * ### 4. Strict List Validation
+ * `parseList()` rejects malformed markup by checking for garbage text between
+ * `
` elements. This prevents creating broken DOM from partial matches like
+ * ``.
+ *
+ * ### 5. Newline Substitution for ` ` in Combined String
+ * When combining siblings, existing ` ` elements become `\n` in the combined
+ * string. This allows list content to span visual lines while still being parsed
+ * as a single unit.
+ *
+ * @example
+ * // Input Markdown:
+ * // | Feature | Notes |
+ * // |---------|-------|
+ * // | Multi-line | First Second |
+ * // | List | |
+ * //
+ * // Without this plugin: and render as literal text
+ * // With this plugin: becomes line break, becomes actual list
+ */
+
+import type { Plugin } from 'unified';
+import type { Element, ElementContent, Root, Text } from 'hast';
+import { visit } from 'unist-util-visit';
+import { visitParents } from 'unist-util-visit-parents';
+import { BR_PATTERN, LIST_PATTERN, LI_PATTERN } from '$lib/constants';
+
+/**
+ * Expands text containing ` ` tags into an array of text nodes and br elements.
+ */
+function expandBrTags(value: string): ElementContent[] {
+ const matches = [...value.matchAll(BR_PATTERN)];
+ if (!matches.length) return [{ type: 'text', value } as Text];
+
+ const result: ElementContent[] = [];
+ let cursor = 0;
+
+ for (const m of matches) {
+ if (m.index! > cursor) {
+ result.push({ type: 'text', value: value.slice(cursor, m.index) } as Text);
+ }
+ result.push({ type: 'element', tagName: 'br', properties: {}, children: [] } as Element);
+ cursor = m.index! + m[0].length;
+ }
+
+ if (cursor < value.length) {
+ result.push({ type: 'text', value: value.slice(cursor) } as Text);
+ }
+
+ return result;
+}
+
+/**
+ * Parses a `` string into a HAST element.
+ * Returns null if the markup is malformed or contains unexpected content.
+ */
+function parseList(value: string): Element | null {
+ const match = value.trim().match(LIST_PATTERN);
+ if (!match) return null;
+
+ const body = match[1];
+ const items: ElementContent[] = [];
+ let cursor = 0;
+
+ for (const liMatch of body.matchAll(LI_PATTERN)) {
+ // Reject if there's non-whitespace between list items
+ if (body.slice(cursor, liMatch.index!).trim()) return null;
+
+ items.push({
+ type: 'element',
+ tagName: 'li',
+ properties: {},
+ children: expandBrTags(liMatch[1] ?? '')
+ } as Element);
+
+ cursor = liMatch.index! + liMatch[0].length;
+ }
+
+ // Reject if no items found or trailing garbage exists
+ if (!items.length || body.slice(cursor).trim()) return null;
+
+ return { type: 'element', tagName: 'ul', properties: {}, children: items } as Element;
+}
+
+/**
+ * Processes a single table cell, restoring HTML elements from text content.
+ */
+function processCell(cell: Element) {
+ visitParents(cell, 'text', (textNode: Text, ancestors) => {
+ const parent = ancestors[ancestors.length - 1];
+ if (!parent || parent.type !== 'element') return;
+
+ const parentEl = parent as Element;
+ const siblings = parentEl.children as ElementContent[];
+ const startIndex = siblings.indexOf(textNode as ElementContent);
+ if (startIndex === -1) return;
+
+ // Combine consecutive text nodes and elements into one string
+ let combined = '';
+ let endIndex = startIndex;
+
+ for (let i = startIndex; i < siblings.length; i++) {
+ const sib = siblings[i];
+ if (sib.type === 'text') {
+ combined += (sib as Text).value;
+ endIndex = i;
+ } else if (sib.type === 'element' && (sib as Element).tagName === 'br') {
+ combined += '\n';
+ endIndex = i;
+ } else {
+ break;
+ }
+ }
+
+ // Try parsing as list first (replaces entire combined range)
+ const list = parseList(combined);
+ if (list) {
+ siblings.splice(startIndex, endIndex - startIndex + 1, list);
+ return;
+ }
+
+ // Otherwise, just expand tags in this text node
+ const expanded = expandBrTags(textNode.value);
+ if (expanded.length !== 1 || expanded[0] !== textNode) {
+ siblings.splice(startIndex, 1, ...expanded);
+ }
+ });
+}
+
+export const rehypeRestoreTableHtml: Plugin<[], Root> = () => (tree) => {
+ visit(tree, 'element', (node: Element) => {
+ if (node.tagName === 'td' || node.tagName === 'th') {
+ processCell(node);
+ }
+ });
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/remark/literal-html.ts b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/remark/literal-html.ts
new file mode 100644
index 0000000000000000000000000000000000000000..c974d8b1893ed5bfa44568e3791c0ef9a73177b2
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/MarkdownContent/plugins/remark/literal-html.ts
@@ -0,0 +1,121 @@
+import type { Plugin } from 'unified';
+import { visit } from 'unist-util-visit';
+import type { Break, Content, Paragraph, PhrasingContent, Root, Text } from 'mdast';
+import { LINE_BREAK, NBSP, PHRASE_PARENTS, TAB_AS_SPACES } from '$lib/constants';
+
+/**
+ * remark plugin that rewrites raw HTML nodes into plain-text equivalents.
+ *
+ * remark parses inline HTML into `html` nodes even when we do not want to render
+ * them. We turn each of those nodes into regular text (plus ` ` break markers)
+ * so the downstream rehype pipeline escapes the characters instead of executing
+ * them. Leading spaces and tab characters are converted to non‑breaking spaces to
+ * keep indentation identical to the original author input.
+ */
+
+function preserveIndent(line: string): string {
+ let index = 0;
+ let output = '';
+
+ while (index < line.length) {
+ const char = line[index];
+
+ if (char === ' ') {
+ output += NBSP;
+ index += 1;
+ continue;
+ }
+
+ if (char === '\t') {
+ output += TAB_AS_SPACES;
+ index += 1;
+ continue;
+ }
+
+ break;
+ }
+
+ return output + line.slice(index);
+}
+
+function createLiteralChildren(value: string): PhrasingContent[] {
+ const lines = value.split(LINE_BREAK);
+ const nodes: PhrasingContent[] = [];
+
+ for (const [lineIndex, rawLine] of lines.entries()) {
+ if (lineIndex > 0) {
+ nodes.push({ type: 'break' } as Break as unknown as PhrasingContent);
+ }
+
+ nodes.push({
+ type: 'text',
+ value: preserveIndent(rawLine)
+ } as Text as unknown as PhrasingContent);
+ }
+
+ if (!nodes.length) {
+ nodes.push({ type: 'text', value: '' } as Text as unknown as PhrasingContent);
+ }
+
+ return nodes;
+}
+
+export const remarkLiteralHtml: Plugin<[], Root> = () => {
+ return (tree) => {
+ visit(tree, 'html', (node, index, parent) => {
+ if (!parent || typeof index !== 'number') {
+ return;
+ }
+
+ const replacement = createLiteralChildren(node.value);
+
+ if (!PHRASE_PARENTS.has(parent.type as string)) {
+ const paragraph: Paragraph = {
+ type: 'paragraph',
+ children: replacement as Paragraph['children'],
+ data: { literalHtml: true }
+ };
+
+ const siblings = parent.children as unknown as Content[];
+ siblings.splice(index, 1, paragraph as unknown as Content);
+
+ if (index > 0) {
+ const previous = siblings[index - 1] as Paragraph | undefined;
+
+ if (
+ previous?.type === 'paragraph' &&
+ (previous.data as { literalHtml?: boolean } | undefined)?.literalHtml
+ ) {
+ const prevChildren = previous.children as unknown as PhrasingContent[];
+
+ if (prevChildren.length) {
+ const lastChild = prevChildren[prevChildren.length - 1];
+
+ if (lastChild.type !== 'break') {
+ prevChildren.push({
+ type: 'break'
+ } as Break as unknown as PhrasingContent);
+ }
+ }
+
+ prevChildren.push(...(paragraph.children as unknown as PhrasingContent[]));
+
+ siblings.splice(index, 1);
+
+ return index;
+ }
+ }
+
+ return index + 1;
+ }
+
+ (parent.children as unknown as PhrasingContent[]).splice(
+ index,
+ 1,
+ ...(replacement as unknown as PhrasingContent[])
+ );
+
+ return index + replacement.length;
+ });
+ };
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/MermaidPreview.svelte b/llama.cpp/tools/ui/src/lib/components/app/content/MermaidPreview.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..a30f585b93cb7c2df273246a30642592c13ca8f4
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/MermaidPreview.svelte
@@ -0,0 +1,121 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/MermaidPreviewControls.svelte b/llama.cpp/tools/ui/src/lib/components/app/content/MermaidPreviewControls.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..bb3185f40edac7ce13ac44172db415137e3df019
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/MermaidPreviewControls.svelte
@@ -0,0 +1,74 @@
+
+
+
+
+
+
+
+
{Math.round(scale * 100)}%
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/content/SyntaxHighlightedCode.svelte b/llama.cpp/tools/ui/src/lib/components/app/content/SyntaxHighlightedCode.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..c4d1706bfe1b406afe8df97627573890cdfde32e
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/content/SyntaxHighlightedCode.svelte
@@ -0,0 +1,90 @@
+
+
+
+
+
{@html highlightedHtml}
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogChatAttachmentsPreview.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogChatAttachmentsPreview.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..533301dfdad3a76f2404dc6b7a56d3f4de37517f
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogChatAttachmentsPreview.svelte
@@ -0,0 +1,88 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogChatError.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogChatError.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..ff1005313e5ce742301ad709f865e3bd293637df
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogChatError.svelte
@@ -0,0 +1,80 @@
+
+
+
+
+
+
+ {#if isTimeout}
+
+ {:else}
+
+ {/if}
+
+ {title}
+
+
+
+ {description}
+
+
+
+
+
{message}
+
+ {#if contextInfo}
+
+
+ Prompt tokens:
+
+ {contextInfo.n_prompt_tokens.toLocaleString()}
+
+
+ {#if contextInfo.n_ctx}
+
+ Context size:
+
+ {contextInfo.n_ctx.toLocaleString()}
+
+ {/if}
+
+ {/if}
+
+
+
+ handleOpenChange(false)}>Close
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogCodePreview.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogCodePreview.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..fe5d9b504b88a8c988b2b4fc159e4a9064cf3492
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogCodePreview.svelte
@@ -0,0 +1,95 @@
+
+
+
+
+
+
+
+
+
+
+
+
+ Close preview
+
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogConfirmation.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogConfirmation.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..becc658d3c5738ed15fd0ceb185716417bd0d607
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogConfirmation.svelte
@@ -0,0 +1,81 @@
+
+
+
+
+
+
+ {#if icon}
+ {@const IconComponent = icon}
+
+
+ {/if}
+ {title}
+
+
+
+ {description}
+
+
+
+ {#if children}
+ {@render children()}
+ {/if}
+
+
+ {cancelText}
+
+ {confirmText}
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogConversationSelection.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogConversationSelection.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..7373250850ccf8882c541e18a16cab79e709128b
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogConversationSelection.svelte
@@ -0,0 +1,69 @@
+
+
+
+
+
+
+
+
+
+ Select Conversations to {mode === 'export' ? 'Export' : 'Import'}
+
+
+
+ {#if mode === 'export'}
+ Choose which conversations you want to export. Selected conversations will be downloaded
+ as a JSON file.
+ {:else}
+ Choose which conversations you want to import. Selected conversations will be merged
+ with your existing conversations.
+ {/if}
+
+
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogConversationTitleUpdate.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogConversationTitleUpdate.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..4a9eccef789bb5c23abe52bf6b02bc99b7845407
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogConversationTitleUpdate.svelte
@@ -0,0 +1,46 @@
+
+
+
+
+
+ Update Conversation Title?
+
+
+ Do you want to update the conversation title to match the first message content?
+
+
+
+
+
+
Current title:
+
+
{currentTitle}
+
+
+
+
New title would be:
+
+
{newTitle}
+
+
+
+
+ Keep Current Title
+
+ Update Title
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogEmptyFileAlert.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogEmptyFileAlert.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..f875b0abaeba33eb85ce179a15d16f55d0616e5d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogEmptyFileAlert.svelte
@@ -0,0 +1,61 @@
+
+
+
+
+
+
+
+
+ Empty Files Detected
+
+
+
+ The following files are empty and have been removed from your attachments:
+
+
+
+
+
+
Empty Files:
+
+
+ {#each emptyFiles as fileName (fileName)}
+ {fileName}
+ {/each}
+
+
+
+
+
What happened:
+
+
+ Empty files cannot be processed or sent to the AI model
+
+ These files have been automatically removed from your attachments
+
+ You can try uploading files with content instead
+
+
+
+
+
+ handleOpenChange(false)}>Got it
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogExportSettings.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogExportSettings.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..c112bde9f6828e3a99157eba16c336ffe149958d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogExportSettings.svelte
@@ -0,0 +1,83 @@
+
+
+
+
+
+
+ {#if includeSensitiveData}
+
+ {:else}
+
+ {/if}
+ Export Settings
+
+
+
+ {#if includeSensitiveData}
+
+ Warning: This export will include sensitive data such as API keys and MCP server custom
+ headers (e.g., authorization tokens). Do not share this file with anyone you don't
+ trust.
+
+ {:else}
+
+ Sensitive data (API keys, MCP server custom headers) will not be included in the export
+ to protect your credentials.
+
+ {/if}
+
+
+
+
+
+
+
+ {#if includeSensitiveData}
+ Include sensitive data (not recommended)
+ {:else}
+ Include sensitive data
+ {/if}
+
+
+
+
+ Cancel
+
+ {#if includeSensitiveData}
+ Export Anyway
+ {:else}
+ Export Without Sensitive Data
+ {/if}
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogFileUploadError.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogFileUploadError.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..3bb2d357f537045e027dec0144d31aa27733b8e9
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogFileUploadError.svelte
@@ -0,0 +1,88 @@
+
+
+
+
+
+
+
+
+ File Upload Error
+
+
+ Some files cannot be uploaded with the current model.
+
+
+
+
+ {#if fileErrorData.generallyUnsupported.length > 0}
+
+
Unsupported File Types
+
+
+ {#each fileErrorData.generallyUnsupported as file (file.name)}
+
+
+ {file.name}
+
+
+
File type not supported
+
+ {/each}
+
+
+ {/if}
+
+ {#if fileErrorData.modalityUnsupported.length > 0}
+
+
+ {#each fileErrorData.modalityUnsupported as file (file.name)}
+
+
+ {file.name}
+
+
+
+ {fileErrorData.modalityReasons[file.name] || 'Not supported by current model'}
+
+
+ {/each}
+
+
+ {/if}
+
+
+
+
This model supports:
+
+
+ {fileErrorData.supportedTypes.join(', ')}
+
+
+
+
+ handleOpenChange(false)}>Got it
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMcpResourcePreview.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMcpResourcePreview.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..7bf284089c97d0a8eba79829e303ede81380215e
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMcpResourcePreview.svelte
@@ -0,0 +1,122 @@
+
+
+
+
+
+ {extra.name}
+
+
+
+
{extra.uri}
+
+ {#if serverName}
+
+ ·
+ {#if favicon}
+ {
+ (e.currentTarget as HTMLImageElement).style.display = 'none';
+ }}
+ />
+ {/if}
+ {serverName}
+
+ {/if}
+
+ {#if extra.mimeType}
+
{extra.mimeType}
+ {/if}
+
+
+
+
+
+
+
+ {#if isImageResource(extra.mimeType, extra.uri) && extra.content}
+
+
+
+ {:else if isCodeResource(extra.mimeType, extra.uri) && extra.content}
+
+ {:else if extra.content}
+
{extra.content}
+ {:else}
+
No content available
+ {/if}
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMcpResourcesBrowser.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMcpResourcesBrowser.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..eb162a5572921b9202c0582b2a4babfa83f9de96
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMcpResourcesBrowser.svelte
@@ -0,0 +1,394 @@
+
+
+
+
+
+
+
+
+ MCP Resources
+
+ {#if totalCount > 0}
+ ({totalCount})
+ {/if}
+
+
+
+ Browse and attach resources from connected MCP servers to your chat context.
+
+
+
+
+
+
+
+
+
+ {#if selectedTemplate && !templatePreviewContent}
+
+
+
+
+
+ {selectedTemplate.title || selectedTemplate.name}
+
+
+
+ {#if selectedTemplate.description}
+
+ {selectedTemplate.description}
+
+ {/if}
+
+
+
+ {selectedTemplate.uriTemplate}
+
+
+
+ {#if templatePreviewLoading}
+
+
+
+ {:else if templatePreviewError}
+
+ {templatePreviewError}
+
+ {
+ templatePreviewError = null;
+ }}
+ >
+ Try again
+
+
+ {:else}
+
+ {/if}
+
+ {:else if hasTemplateResult}
+
+
+ {:else if selectedResources.size === 1}
+ {@const allResources = getAllResourcesFlatInTreeOrder()}
+ {@const selectedResource = allResources.find((r) => selectedResources.has(r.uri))}
+
+
+ {:else if selectedResources.size > 1}
+
+ {#each getAllResourcesFlatInTreeOrder() as resource (resource.uri)}
+ {#if selectedResources.has(resource.uri)}
+
+ {/if}
+ {/each}
+
+ {:else}
+
+ Select a resource to preview
+
+ {/if}
+
+
+
+
+ handleOpenChange(false)}>Cancel
+
+ {#if hasTemplateResult}
+
+ {#if isAttaching}
+
+ {:else}
+
+ {/if}
+
+ Attach Resource
+
+ {:else}
+
+ {#if isAttaching}
+
+ {:else}
+
+ {/if}
+
+ Attach {selectedResources.size > 0 ? `(${selectedResources.size})` : 'Resource'}
+
+ {/if}
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMcpServerAddNew.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMcpServerAddNew.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..ee99e6b5e33d456949d7cb2e7bd0e65eb8117c9d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMcpServerAddNew.svelte
@@ -0,0 +1,95 @@
+
+
+
+
+
+ Add New Server
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMcpServerRecommendations.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMcpServerRecommendations.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..9b4489b8287d93b40e6708a30ec2dad1ab16d8e0
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMcpServerRecommendations.svelte
@@ -0,0 +1,180 @@
+
+
+
+
+
+ Do more with MCP
+
+ Power-up your experience by adding tools, resources and more capabilities provided by MCP
+ servers.
+
+
+
+
+
Quickly get started with
+
+ {#each RECOMMENDED_MCP_SERVERS as server (server.id)}
+
(selected[server.id] = enabled)}
+ />
+ {/each}
+
+ {#if addedServers.length > 0}
+ {#each addedServers as server (server.id)}
+
+ {/each}
+ {/if}
+
+ {#if showAddForm}
+
+ (newServerUrl = v)}
+ onHeadersChange={(v) => (newServerHeaders = v)}
+ urlError={newServerUrl ? newServerUrlError : null}
+ id="recommendation-new-server"
+ />
+
+
+ Cancel
+
+
+ Add
+
+
+
+ {:else}
+
+ (showAddForm = true)}
+ aria-label="Add your own MCP server"
+ >
+
+ Add your own server
+
+
+ {/if}
+
+
+
+ handleOpenChange(false)}>Not now
+
+ Add selected
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMermaidPreview.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMermaidPreview.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..9cbeebc36af2d7209cdd63db704e6840df911a24
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogMermaidPreview.svelte
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogModelInformation.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogModelInformation.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..5a10859a080929512ce10ae3cb40661e8831d27d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogModelInformation.svelte
@@ -0,0 +1,270 @@
+
+
+
+
+
+
+
+ Model Information
+
+ Current model details and capabilities
+
+
+
+ {#if isLoadingModels || isLoadingRouterProps}
+
+
Loading model information...
+
+ {:else if firstModel}
+ {@const modelMeta = firstModel.meta}
+
+ {#if serverProps}
+
+
+
+ Model
+
+
+
+
+
+
+
+
+
+ File Path
+
+
+
+ {serverProps.model_path}
+
+
+
+
+
+
+
+ {#if serverProps?.default_generation_settings?.n_ctx}
+
+ Context Size
+
+ {formatNumber(serverProps.default_generation_settings.n_ctx)} tokens
+
+ {:else}
+
+ Context Size
+
+ Not available
+
+ {/if}
+
+
+ {#if modelMeta?.n_ctx_train}
+
+ Training Context
+
+ {formatNumber(modelMeta.n_ctx_train)} tokens
+
+ {/if}
+
+
+ {#if modelMeta?.size}
+
+ Model Size
+
+ {formatFileSize(modelMeta.size)}
+
+ {/if}
+
+
+ {#if modelMeta?.n_params}
+
+ Parameters
+
+ {formatParameters(modelMeta.n_params)}
+
+ {/if}
+
+
+ {#if modelMeta?.n_embd}
+
+ Embedding Size
+
+ {formatNumber(modelMeta.n_embd)}
+
+ {/if}
+
+
+ {#if modelMeta?.n_vocab}
+
+ Vocabulary Size
+
+ {formatNumber(modelMeta.n_vocab)} tokens
+
+ {/if}
+
+
+ {#if modelMeta?.vocab_type}
+
+ Vocabulary Type
+ {modelMeta.vocab_type}
+
+ {/if}
+
+
+
+ Parallel Slots
+
+ {serverProps.total_slots}
+
+
+
+ {#if modalities.length > 0}
+
+ Modalities
+
+
+
+
+
+
+
+ {/if}
+
+
+
+ Build Info
+
+ {serverProps.build_info}
+
+
+
+ {#if serverProps.chat_template}
+
+ Chat Template
+
+
+
+
{serverProps.chat_template}
+
+
+
+ {/if}
+
+
+ {/if}
+ {:else if !isLoadingModels}
+
+
No model information available
+
+ {/if}
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogModelNotAvailable.svelte b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogModelNotAvailable.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..a6c20291fa0a6151ad442270b0057520790fa1f1
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/DialogModelNotAvailable.svelte
@@ -0,0 +1,76 @@
+
+
+
+
+
+
+
+ Model Not Available
+
+
+
+ The requested model could not be found. Select an available model to continue.
+
+
+
+
+
+
+ Requested: {modelName}
+
+
+
+ {#if availableModels.length > 0}
+
+
Select an available model:
+
+ {#each availableModels as model (model)}
+
handleSelectModel(model)}
+ >
+ {model}
+
+
+ {/each}
+
+
+ {/if}
+
+
+
+ handleOpenChange(false)}>Cancel
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/dialogs/index.ts b/llama.cpp/tools/ui/src/lib/components/app/dialogs/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..73f22c565141d139b2b9b376b80e0bcc8db7bf95
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/dialogs/index.ts
@@ -0,0 +1,517 @@
+/**
+ *
+ * DIALOGS
+ *
+ * Modal dialog components for the chat application.
+ *
+ * All dialogs use ShadCN Dialog or AlertDialog components for consistent
+ * styling, accessibility, and animation. They integrate with application
+ * stores for state management and data access.
+ *
+ */
+
+/**
+ * **DialogMcpServerAddNew** - Add new MCP server dialog
+ *
+ * Modal dialog for adding a new MCP server with URL and optional headers.
+ * Validates URL format and integrates with mcpStore and conversationsStore.
+ */
+export { default as DialogMcpServerAddNew } from './DialogMcpServerAddNew.svelte';
+
+/**
+ * **DialogMcpServerRecommendations** - Suggested MCP servers opt-in dialog
+ *
+ * Prompts the user to enable pre-defined recommended MCP servers on first launch.
+ * Shows one switch per suggested server and persists the choice as a per-chat
+ * override so the selected servers become available in conversations.
+ */
+export { default as DialogMcpServerRecommendations } from './DialogMcpServerRecommendations.svelte';
+
+/**
+ * **DialogExportSettings** - Settings export dialog with sensitive data warning
+ *
+ * Dialog for exporting settings with an option to include or exclude
+ * sensitive data (API keys, MCP server custom headers). Defaults to excluding
+ * sensitive data for security. User must explicitly opt-in to include them.
+ *
+ * **Architecture:**
+ * - Uses ShadCN AlertDialog
+ * - Checkbox to toggle sensitive data inclusion (defaults to false)
+ * - Warning icon and message when sensitive data is included
+ * - Destructive variant for the action button when exporting with sensitive data
+ *
+ * **Features:**
+ * - Secure default: sensitive data excluded by default
+ * - User must explicitly opt-in to include sensitive data
+ * - Visual warning (ShieldOff icon) when sensitive data is included
+ * - Different action text based on sensitive data state
+ *
+ * @example
+ * ```svelte
+ * showExportSettings = false}
+ * />
+ * ```
+ */
+export { default as DialogExportSettings } from './DialogExportSettings.svelte';
+
+/**
+ *
+ * CONFIRMATION DIALOGS
+ *
+ * Dialogs for user action confirmations. Use AlertDialog for blocking
+ * confirmations that require explicit user decision before proceeding.
+ *
+ */
+
+/**
+ * **DialogConfirmation** - Generic confirmation dialog
+ *
+ * Reusable confirmation dialog with customizable title, description,
+ * and action buttons. Supports destructive action styling and custom icons.
+ * Used for delete confirmations, irreversible actions, and important decisions.
+ *
+ * **Architecture:**
+ * - Uses ShadCN AlertDialog
+ * - Supports variant styling (default, destructive)
+ * - Customizable button labels and callbacks
+ *
+ * **Features:**
+ * - Customizable title and description text
+ * - Destructive variant with red styling for dangerous actions
+ * - Custom icon support in header
+ * - Cancel and confirm button callbacks
+ * - Keyboard accessible (Escape to cancel, Enter to confirm)
+ *
+ * @example
+ * ```svelte
+ * showDelete = false}
+ * />
+ * ```
+ */
+export { default as DialogConfirmation } from './DialogConfirmation.svelte';
+
+/**
+ * **DialogConversationTitleUpdate** - Conversation rename confirmation
+ *
+ * Confirmation dialog shown when editing the first user message in a conversation.
+ * Asks user whether to update the conversation title to match the new message content.
+ *
+ * **Architecture:**
+ * - Uses ShadCN AlertDialog
+ * - Shows current vs proposed title comparison
+ * - Triggered by ChatMessages when first message is edited
+ *
+ * **Features:**
+ * - Side-by-side display of current and new title
+ * - "Keep Current Title" and "Update Title" action buttons
+ * - Styled title previews in muted background boxes
+ *
+ * @example
+ * ```svelte
+ * showTitleUpdate = false}
+ * />
+ * ```
+ */
+export { default as DialogConversationTitleUpdate } from './DialogConversationTitleUpdate.svelte';
+
+/**
+ *
+ * CONTENT PREVIEW DIALOGS
+ *
+ * Dialogs for previewing and displaying content in full-screen or modal views.
+ *
+ */
+
+/**
+ * **DialogCodePreview** - Full-screen code/HTML preview
+ *
+ * Full-screen dialog for previewing HTML or code in an isolated iframe.
+ * Used by MarkdownContent component for previewing rendered HTML blocks
+ * from code blocks in chat messages.
+ *
+ * **Architecture:**
+ * - Uses ShadCN Dialog with full viewport layout
+ * - Sandboxed iframe execution (allow-scripts only)
+ * - Clears content when closed for security
+ *
+ * **Features:**
+ * - Full viewport iframe preview
+ * - Sandboxed execution environment
+ * - Close button with mix-blend-difference for visibility over any content
+ * - Automatic content cleanup on close
+ * - Supports HTML preview with proper isolation
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as DialogCodePreview } from './DialogCodePreview.svelte';
+
+/**
+ *
+ * ATTACHMENT DIALOGS
+ *
+ * Dialogs for viewing and managing file attachments. Support both
+ * uploaded files (pending) and stored attachments (in messages).
+ *
+ */
+
+/**
+ * **DialogChatAttachmentsPreview** - Unified attachment preview dialog
+ *
+ * Modal dialog for previewing file attachments. Automatically adapts to the
+ * number of items: shows a single file preview without carousel for one item,
+ * or a gallery with carousel navigation for multiple items.
+ *
+ * **Architecture:**
+ * - Wraps ChatAttachmentsPreview component in ShadCN Dialog
+ * - Accepts uploadedFiles and attachments arrays as data sources
+ * - Filters out MCP prompts and MCP resources from display
+ *
+ * **Features:**
+ * - Single item mode: direct preview without navigation controls
+ * - Multi-item mode: gallery with left/right arrows and thumbnail strip
+ * - File type aware preview (images, text, PDFs, audio)
+ * - File name and size/count display in header
+ *
+ * @example
+ * ```svelte
+ *
+ *
+ * ```
+ */
+export { default as DialogChatAttachmentsPreview } from './DialogChatAttachmentsPreview.svelte';
+
+/**
+ *
+ * ERROR & ALERT DIALOGS
+ *
+ * Dialogs for displaying errors, warnings, and alerts to users.
+ * Provide context about what went wrong and recovery options.
+ *
+ */
+
+/**
+ * **DialogChatError** - Chat/generation error display
+ *
+ * Alert dialog for displaying chat and generation errors with context
+ * information. Supports different error types with appropriate styling
+ * and messaging.
+ *
+ * **Architecture:**
+ * - Uses ShadCN AlertDialog for modal display
+ * - Differentiates between timeout and server errors
+ * - Shows context info when available (token counts)
+ *
+ * **Error Types:**
+ * - **timeout**: TCP timeout with timer icon, red destructive styling
+ * - **server**: Server error with warning icon, amber warning styling
+ *
+ * **Features:**
+ * - Type-specific icons (TimerOff for timeout, AlertTriangle for server)
+ * - Error message display in styled badge
+ * - Context info showing prompt tokens and context size
+ * - Close button to dismiss
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as DialogChatError } from './DialogChatError.svelte';
+
+/**
+ * **DialogEmptyFileAlert** - Empty file upload warning
+ *
+ * Alert dialog shown when user attempts to upload empty files. Lists the
+ * empty files that were detected and removed from attachments, with
+ * explanation of why empty files cannot be processed.
+ *
+ * **Architecture:**
+ * - Uses ShadCN AlertDialog for modal display
+ * - Receives list of empty file names from ChatScreen
+ * - Triggered during file upload validation
+ *
+ * **Features:**
+ * - FileX icon indicating file error
+ * - List of empty file names in monospace font
+ * - Explanation of what happened and why
+ * - Single "Got it" dismiss button
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as DialogEmptyFileAlert } from './DialogEmptyFileAlert.svelte';
+
+/**
+ * **DialogFileUploadError** - File upload compatibility error
+ *
+ * Alert dialog shown when files cannot be uploaded due to type incompatibility
+ * or model modality restrictions. Displays a categorized list of problematic
+ * files with explanations and shows which file types the current model supports.
+ *
+ * **Architecture:**
+ * - Uses ShadCN AlertDialog for modal display
+ * - Receives structured file error data from ChatScreen
+ * - Triggered during file upload validation in processFiles()
+ *
+ * **Features:**
+ * - Categorized display: unsupported types vs modality restrictions
+ * - File name in monospace with contextual error messages
+ * - Summary of supported file types for the current model
+ * - Scrollable content area for large error lists
+ * - Single "Got it" dismiss button
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as DialogFileUploadError } from './DialogFileUploadError.svelte';
+
+/**
+ * **DialogModelNotAvailable** - Model unavailable error
+ *
+ * Alert dialog shown when the requested model (from URL params or selection)
+ * is not available on the server. Displays the requested model name and
+ * offers selection from available models.
+ *
+ * **Architecture:**
+ * - Uses ShadCN AlertDialog for modal display
+ * - Integrates with SvelteKit navigation for model switching
+ * - Receives available models list from modelsStore
+ *
+ * **Features:**
+ * - Warning icon with amber styling
+ * - Requested model name display in styled badge
+ * - Scrollable list of available models
+ * - Click model to navigate with updated URL params
+ * - Cancel button to dismiss without selection
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as DialogModelNotAvailable } from './DialogModelNotAvailable.svelte';
+
+/**
+ *
+ * DATA MANAGEMENT DIALOGS
+ *
+ * Dialogs for managing conversation data, including import/export
+ * and selection operations.
+ *
+ */
+
+/**
+ * **DialogConversationSelection** - Conversation picker for import/export
+ *
+ * Dialog for selecting conversations during import or export operations.
+ * Displays list of conversations with checkboxes for multi-selection.
+ * Used by ChatSettingsImportExportTab for data management.
+ *
+ * **Architecture:**
+ * - Wraps ConversationSelection component in ShadCN Dialog
+ * - Supports export mode (select from local) and import mode (select from file)
+ * - Resets selection state when dialog opens
+ * - High z-index to appear above settings dialog
+ *
+ * **Features:**
+ * - Multi-select with checkboxes
+ * - Conversation title and message count display
+ * - Select all / deselect all controls
+ * - Mode-specific descriptions (export vs import)
+ * - Cancel and confirm callbacks with selected conversations
+ *
+ * @example
+ * ```svelte
+ * showExportSelection = false}
+ * />
+ * ```
+ */
+export { default as DialogConversationSelection } from './DialogConversationSelection.svelte';
+
+/**
+ *
+ * MODEL INFORMATION DIALOGS
+ *
+ * Dialogs for displaying model and server information.
+ *
+ */
+
+/**
+ * **DialogModelInformation** - Model details display
+ *
+ * Dialog showing comprehensive information about the currently loaded model
+ * and server configuration. Displays model metadata, capabilities, and
+ * server settings in a structured table format.
+ *
+ * **Architecture:**
+ * - Uses ShadCN Dialog with wide layout for table display
+ * - Fetches data from serverStore (props) and modelsStore (metadata)
+ * - Auto-fetches models when dialog opens if not loaded
+ *
+ * **Information Displayed:**
+ * - **Model**: Name with copy button
+ * - **File Path**: Full path to model file with copy button
+ * - **Context Size**: Current context window size
+ * - **Training Context**: Original training context (if available)
+ * - **Model Size**: File size in human-readable format
+ * - **Parameters**: Parameter count (e.g., "7B", "70B")
+ * - **Embedding Size**: Embedding dimension
+ * - **Vocabulary Size**: Token vocabulary size
+ * - **Vocabulary Type**: Tokenizer type (BPE, etc.)
+ * - **Parallel Slots**: Number of concurrent request slots
+ * - **Modalities**: Supported input types (text, vision, audio)
+ * - **Build Info**: Server build information
+ * - **Chat Template**: Full Jinja template in scrollable code block
+ *
+ * **Features:**
+ * - Copy buttons for model name and path
+ * - Modality badges with icons
+ * - Responsive table layout with container queries
+ * - Loading state while fetching model info
+ * - Scrollable chat template display
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as DialogModelInformation } from './DialogModelInformation.svelte';
+
+/**
+ * **DialogMcpResourcesBrowser** - MCP resources browser dialog
+ *
+ * Dialog for browsing and attaching MCP resources to chat context.
+ * Displays resources from connected MCP servers in a tree structure
+ * with preview panel and multi-select support.
+ *
+ * **Architecture:**
+ * - Uses ShadCN Dialog with two-panel layout
+ * - Left panel: McpResourcesBrowser with tree navigation
+ * - Right panel: McpResourcePreview for selected resource
+ * - Integrates with mcpStore for resource fetching and attachment
+ *
+ * **Features:**
+ * - Tree-based resource navigation by server and path
+ * - Single and multi-select with shift+click
+ * - Resource preview with content display
+ * - Quick attach button per resource
+ * - Batch attach for multiple selections
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as DialogMcpResourcesBrowser } from './DialogMcpResourcesBrowser.svelte';
+
+/**
+ * **DialogMcpResourcePreview** - MCP resource content preview
+ *
+ * Dialog for previewing the content of a stored MCP resource attachment.
+ * Displays the resource content with syntax highlighting for code,
+ * image rendering for images, and plain text for other content.
+ *
+ * **Features:**
+ * - Syntax highlighted code preview
+ * - Image rendering for image resources
+ * - Copy to clipboard and download actions
+ * - Server name and favicon display
+ * - MIME type badge
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as DialogMcpResourcePreview } from './DialogMcpResourcePreview.svelte';
+
+/**
+ * **DialogMermaidPreview** - Full-screen Mermaid diagram preview with zoom and pan
+ *
+ * Full-screen dialog for previewing Mermaid diagrams with interactive controls.
+ * Supports mouse wheel zoom, drag-to-pan, and toolbar buttons for zoom in/out,
+ * fit to view, and reset.
+ *
+ * **Architecture:**
+ * - Uses UI dialog components (`Dialog.Root`, `Dialog.Overlay`, `Dialog.Content`)
+ * for consistent styling, animations, and accessibility
+ * - CSS transform-based zoom and pan (no external dependencies)
+ * - Pointer events for cross-device drag support (mouse + touch)
+ * - Wheel events for zoom-to-cursor functionality
+ *
+ * **Features:**
+ * - Scroll wheel zoom centered on cursor position
+ * - Click and drag to pan the diagram
+ * - Toolbar with zoom in, zoom out, fit to view, reset controls
+ * - Zoom percentage indicator
+ * - Keyboard accessible close button
+ * - Dark/light theme support
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as DialogMermaidPreview } from './DialogMermaidPreview.svelte';
diff --git a/llama.cpp/tools/ui/src/lib/components/app/forms/InputWithSuggestions.svelte b/llama.cpp/tools/ui/src/lib/components/app/forms/InputWithSuggestions.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..5d047c59a961182635b8dbda3e930c1989deb65b
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/forms/InputWithSuggestions.svelte
@@ -0,0 +1,78 @@
+
+
+
+
+
+ {name}
+
+ *
+
+
+ {#if isLoadingSuggestions}
+ ...
+ {/if}
+
+
+
onInput(e.currentTarget.value)}
+ onkeydown={onKeydown}
+ onblur={onBlur}
+ onfocus={onFocus}
+ placeholder="Enter {name}"
+ autocomplete="off"
+ />
+
+ {#if isAutocompleteActive && suggestions.length > 0}
+
+ {#each suggestions as suggestion, i (suggestion)}
+ onSelectSuggestion(suggestion)}
+ class="w-full px-3 py-1.5 text-left text-sm hover:bg-accent {i === autocompleteIndex
+ ? 'bg-accent'
+ : ''}"
+ >
+ {suggestion}
+
+ {/each}
+
+ {/if}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/forms/KeyValuePairs.svelte b/llama.cpp/tools/ui/src/lib/components/app/forms/KeyValuePairs.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..fd6e59a5b83066bf6a644d2a9b4c04505147eb24
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/forms/KeyValuePairs.svelte
@@ -0,0 +1,164 @@
+
+
+
+
+ {#if sectionLabel}
+
+ {sectionLabel}
+ {#if sectionLabelOptional}
+ (optional)
+ {/if}
+
+ {/if}
+
+
+
+ {addButtonLabel}
+
+
+ {#if pairs.length > 0}
+
+ {#each pairs as pair, index (index)}
+
+ updatePairKey(index, e.currentTarget.value)}
+ onblur={(e) => trimPairKey(index, e.currentTarget.value)}
+ class="flex-1"
+ />
+
+
+
+ removePair(index)}
+ aria-label="Remove item"
+ >
+
+
+
+ {/each}
+
+ {:else}
+
{emptyMessage}
+ {/if}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/forms/SearchInput.svelte b/llama.cpp/tools/ui/src/lib/components/app/forms/SearchInput.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..c4389c52373afcea8896c0a91af1a597d02616c3
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/forms/SearchInput.svelte
@@ -0,0 +1,78 @@
+
+
+
+
+
+
+
+ {#if showClearButton}
+
+
+
+ {/if}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/forms/index.ts b/llama.cpp/tools/ui/src/lib/components/app/forms/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..4cf56cdc9d046e0bb6388c44d97bc79bf41357a5
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/forms/index.ts
@@ -0,0 +1,44 @@
+/**
+ *
+ * FORMS & INPUTS
+ *
+ * Form-related utility components.
+ *
+ */
+
+/**
+ * **InputWithSuggestions** - Input field with autocomplete suggestions
+ *
+ * Text input with dropdown suggestions and keyboard navigation.
+ * Supports autocomplete functionality with suggestion loading.
+ *
+ * **Features:**
+ * - Autocomplete dropdown with suggestions
+ * - Keyboard navigation (arrow keys, enter)
+ * - Loading state for suggestions
+ * - Focus and blur handling
+ */
+export { default as InputWithSuggestions } from './InputWithSuggestions.svelte';
+
+/**
+ * **KeyValuePairs** - Editable key-value list
+ *
+ * Dynamic list of key-value pairs with add/remove functionality.
+ * Used for HTTP headers, metadata, and configuration.
+ *
+ * **Features:**
+ * - Add new pairs with button
+ * - Remove individual pairs
+ * - Customizable placeholders and labels
+ * - Empty state message
+ * - Auto-resize value textarea
+ */
+export { default as KeyValuePairs } from './KeyValuePairs.svelte';
+
+/**
+ * **SearchInput** - Search field with clear button
+ *
+ * Input field optimized for search with clear button and keyboard handling.
+ * Supports placeholder, autofocus, and change callbacks.
+ */
+export { default as SearchInput } from './SearchInput.svelte';
diff --git a/llama.cpp/tools/ui/src/lib/components/app/index.ts b/llama.cpp/tools/ui/src/lib/components/app/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..4914c743a50dd909cc29d387a04f33f893b140e4
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/index.ts
@@ -0,0 +1,12 @@
+export * from './actions';
+export * from './badges';
+export * from './chat';
+export * from './content';
+export * from './dialogs';
+export * from './forms';
+export * from './mcp';
+export * from './misc';
+export * from './settings';
+export * from './models';
+export * from './navigation';
+export * from './server';
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpActiveServersAvatars.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpActiveServersAvatars.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..2f732cfd5370de8b8f482318652f19bf9e8b4acd
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpActiveServersAvatars.svelte
@@ -0,0 +1,89 @@
+
+
+{#if !hasEnabledMcpServers}
+
+
+
+
+
+
+
+ MCP Servers
+
+
+
+{:else if mcpFavicons.length > 0}
+
+
+ {#each mcpFavicons as favicon (favicon.id)}
+
+
+
+
{
+ (e.currentTarget as HTMLImageElement).style.display = 'none';
+ }}
+ />
+
+
+
+ {favicon.name}
+
+
+ {/each}
+
+
+ {#if extraServersCount > 0}
+ +{extraServersCount}
+ {/if}
+
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpCapabilitiesBadges.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpCapabilitiesBadges.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..d17b24ebb04d906d054affcfede660d9f3e3533f
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpCapabilitiesBadges.svelte
@@ -0,0 +1,61 @@
+
+
+{#if capabilities}
+ {#if capabilities.server.tools}
+
+
+
+ Tools
+
+ {/if}
+
+ {#if capabilities.server.resources}
+
+
+
+ Resources
+
+ {/if}
+
+ {#if capabilities.server.prompts}
+
+
+
+ Prompts
+
+ {/if}
+
+ {#if capabilities.server.logging}
+
+
+
+ Logging
+
+ {/if}
+
+ {#if capabilities.server.completions}
+
+
+
+ Completions
+
+ {/if}
+
+ {#if capabilities.server.tasks}
+
+
+
+ Tasks
+
+ {/if}
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpConnectionLogs.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpConnectionLogs.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..305c9db3aee749c788b2b2e9f7d9b0a2a299bca4
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpConnectionLogs.svelte
@@ -0,0 +1,81 @@
+
+
+{#if logs.length > 0}
+
+
+
+ {#if isExpanded}
+
+ {:else}
+
+ {/if}
+
+ Connection Log ({logs.length})
+
+ {#if connectionTimeMs !== undefined}
+ · Connected in {connectionTimeMs}ms
+ {/if}
+
+
+
+
+
+ {#each logs as log (log.timestamp.getTime() + log.message)}
+ {@const IconComponent = getMcpLogLevelIcon(log.level)}
+
+
+
+ {formatTime(log.timestamp)}
+
+
+
+
+ {log.message}
+
+
+ {#if log.details !== undefined}
+
+ details
+
+
+{formatLogDetails(log.details)}
+
+ {/if}
+ {/each}
+
+
+
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpLogo.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpLogo.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..9f73db84d6107d6532f95a60baad1b2541aa2a04
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpLogo.svelte
@@ -0,0 +1,111 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcePreview.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcePreview.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..55e1e20a2c7bb222f18a2240285c6b958b0e219f
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcePreview.svelte
@@ -0,0 +1,174 @@
+
+
+
+ {#if !resource}
+
+
+
+ Select a resource to preview
+
+ {:else}
+
+
+
{resource.title || resource.name}
+
+
{resource.uri}
+
+ {#if resource.description}
+
{resource.description}
+ {/if}
+
+
+
+
+
+
+ {#if isLoading}
+
+
+
+ {:else if error}
+
+ {:else if content}
+ {@const textContent = getResourceTextContent(content)}
+ {@const blobContent = getResourceBlobContent(content)}
+
+ {#if textContent}
+
{textContent}
+ {/if}
+
+ {#each blobContent as blob (blob.uri)}
+ {#if isImageMimeType(blob.mimeType ?? MimeTypeApplication.OCTET_STREAM)}
+
+ {:else}
+
+
+
+ Binary content ({blob.mimeType || 'unknown type'})
+
+ {/if}
+ {/each}
+
+ {#if !textContent && blobContent.length === 0}
+
No content available
+ {/if}
+ {/if}
+
+
+ {#if resource.mimeType || resource.annotations}
+
+ {#if resource.mimeType}
+ {resource.mimeType}
+ {/if}
+
+ {#if resource.annotations?.priority !== undefined}
+
+ Priority: {resource.annotations.priority}
+
+ {/if}
+
+
+ Server: {resource.serverName}
+
+
+ {/if}
+ {/if}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourceTemplateForm.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourceTemplateForm.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..f6263251424cbba242c947bea41b7817df86f94d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourceTemplateForm.svelte
@@ -0,0 +1,171 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/McpResourcesBrowser.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/McpResourcesBrowser.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..24538e8d71b0928b5a85c15f72c19dbbb1b9c8b7
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/McpResourcesBrowser.svelte
@@ -0,0 +1,153 @@
+
+
+
+
(searchQuery = q)}
+ {searchQuery}
+ />
+
+
+ {#if filteredResources.size === 0}
+
+ {:else}
+ {#each [...filteredResources.entries()] as [serverName, serverRes] (serverName)}
+ toggleServer(serverName as string)}
+ onToggleFolder={toggleFolder}
+ {onSelect}
+ {onToggle}
+ {onTemplateSelect}
+ {searchQuery}
+ />
+ {/each}
+ {/if}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/McpResourcesBrowserEmptyState.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/McpResourcesBrowserEmptyState.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..4fb0c1e24f72e239e360a18c954b6adb56f0f539
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/McpResourcesBrowserEmptyState.svelte
@@ -0,0 +1,15 @@
+
+
+
+ {#if isLoading}
+ Loading resources...
+ {:else}
+ No resources available
+ {/if}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/McpResourcesBrowserHeader.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/McpResourcesBrowserHeader.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..419654c13ca4708c1f455268a38cdfb32034aba1
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/McpResourcesBrowserHeader.svelte
@@ -0,0 +1,41 @@
+
+
+
+
+ onSearch?.(value)}
+ />
+
+
+ {#if isLoading}
+
+ {:else}
+
+ {/if}
+
+
+
+
Available resources
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/McpResourcesBrowserServerItem.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/McpResourcesBrowserServerItem.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..9acd101cdfd68b9651eca47e7df99e14a19ea25a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/McpResourcesBrowserServerItem.svelte
@@ -0,0 +1,230 @@
+
+
+{#snippet renderTreeNode(node: ResourceTreeNode, depth: number, parentPath: string)}
+ {@const isFolder = !node.resource && node.children.size > 0}
+ {@const folderId = `${serverName}:${parentPath}/${node.name}`}
+ {@const isFolderExpanded = expandedFolders.has(folderId)}
+
+ {#if isFolder}
+ {@const folderCount = countTreeResources(node)}
+ onToggleFolder(folderId)}>
+
+ {#if isFolderExpanded}
+
+ {:else}
+
+ {/if}
+
+
+
+ {node.name}
+
+ ({folderCount})
+
+
+
+
+ {#each sortTreeChildren( [...node.children.values()] ) as child (child.resource?.uri || `${serverName}:${parentPath}/${node.name}/${child.name}`)}
+ {@render renderTreeNode(child, depth + 1, `${parentPath}/${node.name}`)}
+ {/each}
+
+
+
+ {:else if node.resource}
+ {@const resource = node.resource}
+ {@const ResourceIcon = getResourceIcon(resource.mimeType, resource.uri)}
+ {@const isSelected = isResourceSelected(resource)}
+ {@const resourceDisplayName = resource.title || getDisplayName(node.name)}
+
+
+ {#if onToggle}
+
+ handleCheckboxChange(resource, checked === true)}
+ class="h-4 w-4"
+ />
+ {/if}
+
+ handleResourceClick(resource, e)}
+ title={resourceDisplayName}
+ >
+
+
+
+ {resourceDisplayName}
+
+
+
+ {/if}
+{/snippet}
+
+
+
+ {#if isExpanded}
+
+ {:else}
+
+ {/if}
+
+
+
+
+
+
+
+ ({serverRes.resources.length} resource{serverRes.resources.length !== 1
+ ? 's'
+ : ''}{#if hasTemplates}, {serverRes.templates.length} template{serverRes.templates
+ .length !== 1
+ ? 's'
+ : ''}{/if})
+
+
+
+ {#if serverRes.loading}
+
+ {/if}
+
+
+
+
+ {#if serverRes.error}
+
+ Error: {serverRes.error}
+
+ {:else if !hasContent}
+
No resources
+ {:else}
+ {#if hasResources}
+ {#each sortTreeChildren( [...resourceTree.children.values()] ) as child (child.resource?.uri || `${serverName}:${child.name}`)}
+ {@render renderTreeNode(child, 1, '')}
+ {/each}
+ {/if}
+
+ {#if hasTemplates && onTemplateSelect}
+ {#if hasResources}
+
+ {/if}
+
+
+ Templates
+
+
+ {#each templateInfos as template (template.uriTemplate)}
+
onTemplateSelect(template)}
+ title={template.uriTemplate}
+ >
+
+
+
+ {template.title || template.name}
+
+
+ {/each}
+ {/if}
+ {/if}
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/mcp-resources-browser.ts b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/mcp-resources-browser.ts
new file mode 100644
index 0000000000000000000000000000000000000000..804fa7fe2fc541fc0f1b8eb364b147208bfee951
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpResourcesBrowser/mcp-resources-browser.ts
@@ -0,0 +1,118 @@
+import type { MCPResource, MCPResourceInfo } from '$lib/types';
+import { parseResourcePath } from '$lib/utils';
+
+export interface ResourceTreeNode {
+ name: string;
+ resource?: MCPResourceInfo;
+ children: Map;
+ isFiltered?: boolean;
+}
+
+function resourceMatchesSearch(resource: MCPResource, query: string): boolean {
+ return (
+ resource.title?.toLowerCase().includes(query) || resource.uri.toLowerCase().includes(query)
+ );
+}
+
+export function buildResourceTree(
+ resourceList: MCPResource[],
+ serverName: string,
+ searchQuery?: string
+): ResourceTreeNode {
+ const root: ResourceTreeNode = { name: 'root', children: new Map() };
+
+ if (!searchQuery || !searchQuery.trim()) {
+ for (const resource of resourceList) {
+ const pathParts = parseResourcePath(resource.uri);
+ let current = root;
+
+ for (let i = 0; i < pathParts.length - 1; i++) {
+ const part = pathParts[i];
+ if (!current.children.has(part)) {
+ current.children.set(part, { name: part, children: new Map() });
+ }
+ current = current.children.get(part)!;
+ }
+
+ const fileName = pathParts[pathParts.length - 1] || resource.name;
+ current.children.set(resource.uri, {
+ name: fileName,
+ resource: { ...resource, serverName },
+ children: new Map()
+ });
+ }
+
+ return root;
+ }
+
+ const query = searchQuery.toLowerCase();
+
+ // Build tree with filtering
+ for (const resource of resourceList) {
+ if (!resourceMatchesSearch(resource, query)) continue;
+
+ const pathParts = parseResourcePath(resource.uri);
+ let current = root;
+
+ for (let i = 0; i < pathParts.length - 1; i++) {
+ const part = pathParts[i];
+ if (!current.children.has(part)) {
+ current.children.set(part, { name: part, children: new Map(), isFiltered: true });
+ }
+ current = current.children.get(part)!;
+ }
+
+ const fileName = pathParts[pathParts.length - 1] || resource.name;
+
+ current.children.set(resource.uri, {
+ name: fileName,
+ resource: { ...resource, serverName },
+ children: new Map(),
+ isFiltered: true
+ });
+ }
+
+ function cleanupEmptyFolders(node: ResourceTreeNode): boolean {
+ if (node.resource) return true;
+
+ const toDelete: string[] = [];
+ for (const [name, child] of node.children.entries()) {
+ if (!cleanupEmptyFolders(child)) {
+ toDelete.push(name);
+ }
+ }
+
+ for (const name of toDelete) {
+ node.children.delete(name);
+ }
+
+ return node.children.size > 0;
+ }
+
+ cleanupEmptyFolders(root);
+
+ return root;
+}
+
+export function countTreeResources(node: ResourceTreeNode): number {
+ if (node.resource) return 1;
+ let count = 0;
+
+ for (const child of node.children.values()) {
+ count += countTreeResources(child);
+ }
+
+ return count;
+}
+
+export function sortTreeChildren(children: ResourceTreeNode[]): ResourceTreeNode[] {
+ return children.sort((a, b) => {
+ const aIsFolder = !a.resource && a.children.size > 0;
+ const bIsFolder = !b.resource && b.children.size > 0;
+
+ if (aIsFolder && !bIsFolder) return -1;
+ if (!aIsFolder && bIsFolder) return 1;
+
+ return a.name.localeCompare(b.name);
+ });
+}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCard.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCard.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..d238d1ffbad45c3e8dfa775ae6ab63f35c391a52
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCard.svelte
@@ -0,0 +1,192 @@
+
+
+
+ {#if isEditing}
+
+ {:else}
+
+
+ {#if isError && errorMessage}
+ {errorMessage}
+ {/if}
+
+ {#if isConnected && serverInfo?.description}
+
+ {serverInfo.description}
+
+ {/if}
+
+
+ {#if showSkeleton}
+
+
+
+ {:else}
+ {#if isConnected && instructions}
+
+ {/if}
+
+ {#if tools.length > 0}
+
+ {/if}
+
+ {#if connectionLogs.length > 0}
+
+ {/if}
+ {/if}
+
+
+
+ {#if showSkeleton}
+
+ {:else if protocolVersion}
+
+
+ Protocol version: {protocolVersion}
+
+
+ {/if}
+
+
+
+ {/if}
+
+
+ (showDeleteDialog = open)}
+ onConfirm={onDelete}
+/>
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardActions.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardActions.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..6f137fa21b7b5e1a2543f6a7507e358714bb6140
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardActions.svelte
@@ -0,0 +1,40 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardCompact.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardCompact.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..6cb3e18b651107fe3200df6f58c43e1f0e123301
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardCompact.svelte
@@ -0,0 +1,156 @@
+
+
+
+
+
+ {#if showSkeleton}
+
+
+
+
+ {:else}
+
+ {/if}
+
+
+
+
+
+ {#if isError && errorMessage}
+ {errorMessage}
+ {/if}
+
+ {#if showSkeleton}
+
+
+
+
+
+
+
+
+
+
+ {:else}
+ {#if description}
+ {#if description.lines === 2}
+
+ {description.text}
+
+ {:else}
+
+ {description.text}
+
+ {/if}
+ {/if}
+
+ {#if tools.length > 0}
+
+ {#each visibleTools as tool (tool.name)}
+
+
+
+ {tool.name}
+
+
+
+
+
+ {tool.description ?? 'No description'}
+
+
+
+ {/each}
+
+ {#if hiddenToolCount > 0}
+
+
+
+ + {hiddenToolCount} more tools
+
+
+
+
+
+ {hiddenTools.map((tool) => tool.name).join(', ')}
+
+
+
+ {/if}
+
+ {/if}
+ {/if}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardDeleteDialog.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardDeleteDialog.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..8f650148a2f828e9348eee44b1c9127785b11223
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardDeleteDialog.svelte
@@ -0,0 +1,36 @@
+
+
+
+
+
+ Delete Server
+
+
+ Are you sure you want to delete {displayName} ? This action cannot be
+ undone.
+
+
+
+
+ Cancel
+
+
+ Delete
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardEditForm.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardEditForm.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..8ed4ee8b802360417c2e0d08046820fdca567780
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardEditForm.svelte
@@ -0,0 +1,75 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardHeader.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardHeader.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..5544bcec42199bed4f013b598ac60313ee7b1c92
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardHeader.svelte
@@ -0,0 +1,70 @@
+
+
+
+
+
+
+
+
+
+ {#if capabilities || transportType}
+
+ {#if transportType}
+ {@const TransportIcon = MCP_TRANSPORT_ICONS[transportType]}
+
+ {#if TransportIcon}
+
+ {/if}
+
+ {MCP_TRANSPORT_LABELS[transportType] || transportType}
+
+ {/if}
+
+ {#if capabilities}
+
+ {/if}
+
+ {/if}
+
+
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardToolsList.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardToolsList.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..d0397c17a980dad230c741cb21aa3dfed14dfebc
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCard/McpServerCardToolsList.svelte
@@ -0,0 +1,47 @@
+
+
+
+
+ {#if isExpanded}
+
+ {:else}
+
+ {/if}
+
+ {toolsCount} tools available · Show details
+
+
+
+
+ {#each tools as tool (tool.name)}
+
+
{tool.name}
+
+ {#if tool.description}
+
{tool.description}
+ {/if}
+
+ {/each}
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCardSkeleton.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCardSkeleton.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..39a1372806a57f86d337ec4433e6550449dcc9e2
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerCardSkeleton.svelte
@@ -0,0 +1,34 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerForm.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerForm.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..7f05d5fef31f0f6ccaa2f93d82998f40ab323c3f
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerForm.svelte
@@ -0,0 +1,219 @@
+
+
+
+
+
+ Server URL *
+
+
+
onUrlChange(e.currentTarget.value)}
+ class={urlError ? 'border-destructive' : ''}
+ bind:ref={urlInput}
+ />
+
+ {#if urlError}
+
{urlError}
+ {/if}
+
+
+
+
+
+ Authorization
+
+
+ {#if showAuthorization}
+
+ updateBearerToken(e.currentTarget.value)}
+ class="pl-16"
+ bind:ref={bearerInput}
+ />
+
+
+ Bearer
+
+
+ {/if}
+
+
!ownedByBearerUi(p))}
+ onPairsChange={(pairs) => {
+ const auth = headerPairs.find(ownedByBearerUi);
+ updateHeaderPairs(auth ? [...pairs, auth] : pairs);
+ }}
+ keyPlaceholder="Header name"
+ valuePlaceholder="Value"
+ addButtonLabel="Add"
+ emptyMessage="No custom headers configured."
+ sectionLabel="Custom Headers"
+ sectionLabelOptional
+ />
+
+ {#if !isWebSocket && onUseProxyChange}
+
+ onUseProxyChange?.(checked)}
+ />
+
+
+ Use llama-server proxy
+
+
+
+ {#if !mcpStore.isProxyAvailable}
+ (Run llama-server
+ with
+ {CLI_FLAGS.MCP_PROXY}
+ flag)
+ {/if}
+
+
+ {/if}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerIdentity.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerIdentity.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..3f128e02c967aec3dde0de76b2de1eeb7e6c70e6
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerIdentity.svelte
@@ -0,0 +1,63 @@
+
+
+
+ {#if faviconUrl}
+
+ {:else}
+
+ {/if}
+
+
+
+ {#if showVersion && serverInfo?.version}
+
+
+
+ {/if}
+
+ {#if showWebsite && safeWebsiteUrl}
+ e.stopPropagation()}
+ >
+
+
+ {/if}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerInfo.svelte b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerInfo.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..aecae6e57b0a0a7d264e63dfe44c8b235eb82920
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/McpServerInfo.svelte
@@ -0,0 +1,35 @@
+
+
+{#if instructions}
+
+
+ {#if isExpanded}
+
+ {:else}
+
+ {/if}
+
+ Server instructions
+
+
+
+
+ {instructions}
+
+
+
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/mcp/index.ts b/llama.cpp/tools/ui/src/lib/components/app/mcp/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..b142841f42910b63b80c176a2d1d1c01ddba72a8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/mcp/index.ts
@@ -0,0 +1,264 @@
+/**
+ *
+ * MCP (Model Context Protocol)
+ *
+ * Components for managing MCP server connections and displaying server status.
+ * MCP enables agentic workflows by connecting to external tool servers.
+ *
+ * The MCP system integrates with:
+ * - `mcpStore` for server CRUD operations and health checks
+ * - `conversationsStore` for per-conversation server enable/disable
+ *
+ */
+
+/**
+ * **McpServersSettings** - MCP servers configuration section
+ *
+ * Settings section for configuring MCP server connections.
+ * Displays server cards with status, tools, and management actions.
+ * Used within the MCP tab of ChatSettings.
+ *
+ * **Architecture:**
+ * - Manages add server form state locally
+ * - Delegates server display to McpServerCard components
+ * - Integrates with mcpStore for server operations
+ * - Shows skeleton loading states during health checks
+ *
+ * **Features:**
+ * - Add new MCP servers by URL with validation
+ * - Server cards with connection status indicators
+ * - Health check status (connected/disconnected/error)
+ * - Tools list per server showing available capabilities
+ * - Enable/disable toggle per conversation
+ * - Edit/delete server actions
+ * - Skeleton loading states during connection
+ * - Empty state with helpful message
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as McpServersSettings } from '../settings/SettingsMcpServers.svelte';
+
+/**
+ * **McpActiveServersAvatars** - Active MCP servers indicator
+ *
+ * Compact avatar row showing favicons of active MCP servers.
+ * Displays up to 3 server icons with "+N" counter for additional servers.
+ * Clickable to open MCP settings dialog.
+ *
+ * **Architecture:**
+ * - Filters servers by enabled status and health check
+ * - Fetches favicons from server URLs
+ * - Integrates with conversationsStore for per-chat server state
+ *
+ * **Features:**
+ * - Overlapping favicon avatars (max 3 visible)
+ * - "+N" counter for additional servers
+ * - Click handler for settings navigation
+ * - Disabled state support
+ * - Only shows healthy, enabled servers
+ *
+ * @example
+ * ```svelte
+ * showMcpSettings = true}
+ * />
+ * ```
+ */
+export { default as McpActiveServersAvatars } from './McpActiveServersAvatars.svelte';
+
+/**
+ * **McpCapabilitiesBadges** - Server capabilities display
+ *
+ * Displays MCP server capabilities as colored badges.
+ * Shows which features the server supports (tools, resources, prompts, etc.).
+ *
+ * **Features:**
+ * - Tools badge (green) - server provides callable tools
+ * - Resources badge (blue) - server provides data resources
+ * - Prompts badge (purple) - server provides prompt templates
+ * - Logging badge (orange) - server supports logging
+ * - Completions badge (cyan) - server provides completions
+ * - Tasks badge (pink) - server supports task management
+ */
+export { default as McpCapabilitiesBadges } from './McpCapabilitiesBadges.svelte';
+
+/**
+ * **McpConnectionLogs** - Connection log viewer
+ *
+ * Collapsible panel showing MCP server connection logs.
+ * Displays timestamped log entries with level-based styling.
+ *
+ * **Features:**
+ * - Collapsible log list with entry count
+ * - Connection time display in milliseconds
+ * - Log level icons and color coding
+ * - Scrollable log container with max height
+ * - Monospace font for log readability
+ */
+export { default as McpConnectionLogs } from './McpConnectionLogs.svelte';
+
+/**
+ * **McpServerForm** - Server URL and headers input form
+ *
+ * Reusable form for entering MCP server connection details.
+ * Used in both add new server and edit server flows.
+ *
+ * **Features:**
+ * - URL input with validation error display
+ * - Custom headers key-value pairs editor
+ * - Controlled component with change callbacks
+ *
+ * @example
+ * ```svelte
+ * serverUrl = v}
+ * onHeadersChange={(v) => serverHeaders = v}
+ * urlError={validationError}
+ * />
+ * ```
+ */
+export { default as McpServerForm } from './McpServerForm.svelte';
+
+/**
+ * MCP protocol logo SVG component. Renders the official MCP icon
+ * with customizable size via class and style props.
+ */
+export { default as McpLogo } from './McpLogo.svelte';
+
+/**
+ *
+ * SERVER CARD
+ *
+ * Components for displaying individual MCP server status and controls.
+ * McpServerCard is the main component, with sub-components for specific sections.
+ *
+ */
+
+/**
+ * **McpServerCard** - Individual server display card
+ *
+ * Main component for displaying a single MCP server with all its details.
+ * Manages edit mode, delete confirmation, and health check actions.
+ *
+ * **Architecture:**
+ * - Composes header, tools list, logs, and actions sub-components
+ * - Manages local edit/delete state
+ * - Reads health state from mcpStore
+ * - Triggers health checks via mcpStore
+ *
+ * **Features:**
+ * - Server header with favicon, name, version, and toggle
+ * - Capabilities badges display
+ * - Tools list with descriptions
+ * - Connection logs viewer
+ * - Edit form for URL and headers
+ * - Delete confirmation dialog
+ * - Skeleton loading states
+ */
+export { default as McpServerCard } from './McpServerCard/McpServerCard.svelte';
+
+/** Server card header with favicon, name, version badge, and enable toggle. */
+export { default as McpServerCardHeader } from './McpServerCard/McpServerCardHeader.svelte';
+
+/** Action buttons row: edit, refresh, delete. */
+export { default as McpServerCardActions } from './McpServerCard/McpServerCardActions.svelte';
+
+/** Collapsible tools list showing available server tools with descriptions. */
+export { default as McpServerCardToolsList } from './McpServerCard/McpServerCardToolsList.svelte';
+
+/** Inline edit form for server URL and custom headers. */
+export { default as McpServerCardEditForm } from './McpServerCard/McpServerCardEditForm.svelte';
+
+/** Delete confirmation dialog with server name display. */
+export { default as McpServerCardDeleteDialog } from './McpServerCard/McpServerCardDeleteDialog.svelte';
+
+/** Skeleton loading state for server card during health checks. */
+export { default as McpServerCardSkeleton } from './McpServerCardSkeleton.svelte';
+
+/**
+ * **McpServerCardCompact** - Condensed MCP server card
+ *
+ * Compact alternative to McpServerCard tailored for picker-style UIs.
+ * Shows the server identity, status, and a flex-wrapped list of available tools.
+ * Tool names are rendered as badges; hovering a badge shows its description in a tooltip.
+ * Does not show connection logs or server instructions.
+ */
+export { default as McpServerCardCompact } from './McpServerCard/McpServerCardCompact.svelte';
+
+/**
+ * **McpServerIdentity** - Server identity display (icon, name, version)
+ *
+ * Reusable headless component for displaying server name, favicon/icon, and version badge.
+ * Accepts all data via props with no store dependencies for predictable rendering.
+ *
+ * **Features:**
+ * - Server favicon/icon with fallback
+ * - Truncated display name with max-width
+ * - Optional version badge (v1.2.3)
+ * - Optional external link to server website
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as McpServerIdentity } from './McpServerIdentity.svelte';
+
+/**
+ * **McpServerInfo** - Server instructions display
+ *
+ * Collapsible panel showing server-provided instructions.
+ * Displays guidance text from the MCP server for users.
+ */
+export { default as McpServerInfo } from './McpServerInfo.svelte';
+
+/**
+ * **McpResourcesBrowser** - MCP resources tree browser
+ *
+ * Tree view component showing resources grouped by server.
+ * Supports resource selection and quick attach actions.
+ *
+ * **Features:**
+ * - Collapsible server sections
+ * - Resource icons based on MIME type
+ * - Resource selection highlighting
+ * - Quick attach button per resource
+ * - Refresh all resources action
+ * - Loading states per server
+ */
+export { default as McpResourcesBrowser } from './McpResourcesBrowser/McpResourcesBrowser.svelte';
+
+/**
+ * **McpResourcePreview** - MCP resource content preview
+ *
+ * Preview panel showing resource content with metadata.
+ * Supports text and binary content display.
+ *
+ * **Features:**
+ * - Text content display with monospace formatting
+ * - Image preview for image MIME types
+ * - Copy to clipboard action
+ * - Download content action
+ * - Resource metadata display (MIME type, priority, server)
+ * - Loading and error states
+ */
+export { default as McpResourcePreview } from './McpResourcePreview.svelte';
+
+/**
+ * **McpResourceTemplateForm** - MCP resource template variable form
+ *
+ * Form for filling in resource template variables with auto-completion
+ * via the Completions API. Shows live URI preview as variables are filled.
+ *
+ * **Features:**
+ * - Template variable input fields
+ * - Completions API integration for variable auto-complete
+ * - Live URI preview as variables are filled
+ * - Read resolved resource action
+ */
+export { default as McpResourceTemplateForm } from './McpResourceTemplateForm.svelte';
diff --git a/llama.cpp/tools/ui/src/lib/components/app/misc/CodeBlockActions.svelte b/llama.cpp/tools/ui/src/lib/components/app/misc/CodeBlockActions.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..fa12d1c6249061d5ae569e70b82f0d4ed5d9d8ba
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/misc/CodeBlockActions.svelte
@@ -0,0 +1,33 @@
+
+
+
+
+
+ {#if showPreview}
+
onPreview!(code, language)}
+ />
+ {/if}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/misc/ConversationSelection.svelte b/llama.cpp/tools/ui/src/lib/components/app/misc/ConversationSelection.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..db14fd631e3193db0b04b854b49cb2a9bd11db6d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/misc/ConversationSelection.svelte
@@ -0,0 +1,194 @@
+
+
+
+
+
+
+
+ {selectedIds.size} of {conversations.length} selected
+ {#if searchQuery}
+ ({filteredConversations.length} shown)
+ {/if}
+
+
+
+
+
+
+
+
+
+
+
+
+ Conversation Name
+
+ Messages
+
+
+
+ {#if filteredConversations.length === 0}
+
+
+ {#if searchQuery}
+ No conversations found matching "{searchQuery}"
+ {:else}
+ No conversations available
+ {/if}
+
+
+ {:else}
+ {#each filteredConversations as conv (conv.id)}
+ toggleConversation(conv.id, event.shiftKey)}
+ >
+
+ {
+ event.preventDefault();
+ event.stopPropagation();
+ toggleConversation(conv.id, event.shiftKey);
+ }}
+ />
+
+
+
+
+ {conv.name || 'Untitled conversation'}
+
+
+
+
+ {messageCountMap.get(conv.id) ?? 0}
+
+
+ {/each}
+ {/if}
+
+
+
+
+
+
+ Cancel
+
+
+ {mode === 'export' ? 'Export' : 'Import'} ({selectedIds.size})
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/misc/HorizontalScrollCarousel.svelte b/llama.cpp/tools/ui/src/lib/components/app/misc/HorizontalScrollCarousel.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..a04f3956f8a5f5cea1f626a54121c91198360550
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/misc/HorizontalScrollCarousel.svelte
@@ -0,0 +1,93 @@
+
+
+
+
+
+
+
+
+ {@render children?.()}
+
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/misc/KeyboardShortcutInfo.svelte b/llama.cpp/tools/ui/src/lib/components/app/misc/KeyboardShortcutInfo.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..da55abda0234fa6ece0e600782e4dab962e5b17a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/misc/KeyboardShortcutInfo.svelte
@@ -0,0 +1,33 @@
+
+
+
+ {#each keys as key, index (index)}
+ {#if key === 'shift'}
+
+ {:else if key === 'cmd'}
+ ⌘
+ {:else}
+ {key.toUpperCase()}
+ {/if}
+
+ {#if index < keys.length - 1}
+
+ {/if}
+ {/each}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/misc/Logo.svelte b/llama.cpp/tools/ui/src/lib/components/app/misc/Logo.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..ff2b7f2f0723a8d056557dd2d2570d9546626ff8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/misc/Logo.svelte
@@ -0,0 +1,15 @@
+
+
+
+ {@html logoMark}
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/misc/TruncatedText.svelte b/llama.cpp/tools/ui/src/lib/components/app/misc/TruncatedText.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..a6b7cb483e5be6a195c85df06fcf0dd6c31fd981
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/misc/TruncatedText.svelte
@@ -0,0 +1,49 @@
+
+
+{#if isTruncated && showTooltip}
+
+
+
+ {text}
+
+
+
+
+ {text}
+
+
+{:else}
+
+ {text}
+
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/misc/index.ts b/llama.cpp/tools/ui/src/lib/components/app/misc/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..b550ae66a53cf90735c31c63b4c5b3d42596cd00
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/misc/index.ts
@@ -0,0 +1,61 @@
+/**
+ *
+ * MISC
+ *
+ * Miscellaneous utility components.
+ *
+ */
+
+/**
+ * **ConversationSelection** - Multi-select conversation picker
+ *
+ * List of conversations with checkboxes for multi-selection.
+ * Used in import/export dialogs for selecting conversations.
+ *
+ * **Features:**
+ * - Search/filter conversations by name
+ * - Select all / deselect all controls
+ * - Shift-click for range selection
+ * - Message count display per conversation
+ * - Mode-specific UI (export vs import)
+ */
+export { default as ConversationSelection } from './ConversationSelection.svelte';
+
+/**
+ * Horizontal scrollable carousel with navigation arrows.
+ * Used for displaying items in a horizontally scrollable container
+ * with left/right navigation buttons that appear on hover.
+ */
+export { default as HorizontalScrollCarousel } from './HorizontalScrollCarousel.svelte';
+
+/**
+ * **TruncatedText** - Text with ellipsis and tooltip
+ *
+ * Displays text with automatic truncation and full content in tooltip.
+ * Useful for long names or paths in constrained spaces.
+ */
+export { default as TruncatedText } from './TruncatedText.svelte';
+
+/**
+ * **KeyboardShortcutInfo** - Keyboard shortcut hint display
+ *
+ * Displays keyboard shortcut hints (e.g., "⌘ + Enter").
+ * Supports special keys like shift, cmd, and custom text.
+ */
+export { default as KeyboardShortcutInfo } from './KeyboardShortcutInfo.svelte';
+
+/**
+ * **CodeBlockActions** - Actions bar for code blocks (copy, preview)
+ *
+ * Displays copy-to-clipboard and preview buttons for code blocks.
+ * Preview button is shown only for HTML code blocks.
+ */
+export { default as CodeBlockActions } from './CodeBlockActions.svelte';
+
+/**
+ * **Logo** - Application brand mark
+ *
+ * Inline SVG of the application logo. Accepts styling via the standard
+ * `class` and `style` props and inherits color via `currentColor`.
+ */
+export { default as Logo } from './Logo.svelte';
diff --git a/llama.cpp/tools/ui/src/lib/components/app/models/ModelBadge.svelte b/llama.cpp/tools/ui/src/lib/components/app/models/ModelBadge.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..b840687d4ef059b0c25abc96ebae32c23f2ccd79
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/models/ModelBadge.svelte
@@ -0,0 +1,63 @@
+
+
+{#snippet badgeContent(triggerProps?: Record)}
+
+ {#snippet icon()}
+
+ {/snippet}
+
+ {#if model}
+
+ {/if}
+
+ {#if showCopyIcon}
+
+ {/if}
+
+{/snippet}
+
+{#if shouldShow}
+ {#if showTooltip}
+
+
+
+ {#snippet child({ props })}
+ {@render badgeContent(props)}
+ {/snippet}
+
+
+
+ {onclick ? 'Click for model details' : model}
+
+
+ {:else}
+ {@render badgeContent()}
+ {/if}
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/models/ModelId.svelte b/llama.cpp/tools/ui/src/lib/components/app/models/ModelId.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..f566b55ee88b6db7941501fba336adefbfee1679
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/models/ModelId.svelte
@@ -0,0 +1,82 @@
+
+
+{#if resolvedShowRaw}
+
+{:else}
+
+
+ {#if !hideOrgName && parsed.orgName}{parsed.orgName}/{/if}{displayName}
+
+
+ {#if parsed.params}
+
+ {parsed.params}{parsed.activatedParams ? `-${parsed.activatedParams}` : ''}
+
+ {/if}
+
+ {#if parsed.quantization && !resolvedHideQuantization}
+
+ {parsed.quantization}
+
+ {/if}
+
+ {#if primaryAlias}
+ {#if primaryAlias !== parsed.modelName}
+ {parsed.modelName ?? modelId}
+ {/if}
+ {:else if uniqueAliases.length > 1}
+ {#each uniqueAliases as alias (alias)}
+ {alias}
+ {/each}
+ {/if}
+
+ {#if uniqueTags.length > 0 && !resolvedHideTags}
+ {#each uniqueTags as tag (tag)}
+ {tag}
+ {/each}
+ {/if}
+
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/models/ModelLoadHighlight.svelte b/llama.cpp/tools/ui/src/lib/components/app/models/ModelLoadHighlight.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..fa9a02108a0be12d5c7de44af46a9c010cf8042a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/models/ModelLoadHighlight.svelte
@@ -0,0 +1,11 @@
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/models/ModelsSelectorDropdown.svelte b/llama.cpp/tools/ui/src/lib/components/app/models/ModelsSelectorDropdown.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..720963a8db9dc45205a2270ae19e4fe1f8de6dc8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/models/ModelsSelectorDropdown.svelte
@@ -0,0 +1,312 @@
+
+
+
+ {#if ms.loading && ms.options.length === 0 && ms.isRouter}
+
+
+
+ Loading models…
+
+ {:else if ms.options.length === 0 && ms.isRouter}
+ {#if currentModel}
+
+
+
+ {:else}
+
No models available.
+ {/if}
+ {:else}
+ {@const selectedOption = ms.getDisplayOption()}
+ {@const triggerModel = selectedOption?.model}
+ {@const triggerStatus = triggerModel
+ ? routerModels().find((m) => m.id === triggerModel)?.status?.value
+ : undefined}
+ {@const triggerLoading =
+ !!triggerModel &&
+ (triggerStatus === ServerModelStatus.LOADING ||
+ modelsStore.isModelOperationInProgress(triggerModel))}
+ {@const triggerLoadPercent = triggerLoading
+ ? Math.round(modelLoadFraction(modelsStore.getLoadProgress(triggerModel)) * 100)
+ : 0}
+
+ {#if ms.isRouter}
+
+
+
+
+ {#snippet child({ props })}
+
+
+
+ {#if selectedOption}
+
+ {:else}
+ Select model
+ {/if}
+
+ {#if ms.updating || ms.isLoadingModel}
+
+ {:else}
+
+ {/if}
+
+ {#if triggerLoading}
+
+ {/if}
+
+ {/snippet}
+
+
+ {#if selectedOption}
+
+ {selectedOption.model}
+
+ {/if}
+
+
+
+ ms.setSearchTerm(v)}
+ placeholder="Search models..."
+ onSearchKeyDown={handleSearchKeyDown}
+ emptyMessage="No models found."
+ isEmpty={ms.filteredOptions.length === 0 && ms.isCurrentModelInCache}
+ >
+
+ {#if !ms.isCurrentModelInCache && currentModel}
+
+
+
+
+ (not available)
+
+ {/if}
+
+ {#if ms.filteredOptions.length === 0}
+
No models found.
+ {/if}
+
+ {#snippet modelOption(item: ModelItem, hideOrgName: boolean)}
+ {@const { option, flatIndex } = item}
+ {@const isSelected = currentModel === option.model || ms.activeId === option.id}
+ {@const isHighlighted = flatIndex === highlightedIndex}
+ {@const isFav = ms.isFavorite(option.model)}
+
+
(highlightedIndex = flatIndex)}
+ onKeyDown={(event) => {
+ if (event.key === KeyboardKey.ENTER || event.key === KeyboardKey.SPACE) {
+ event.preventDefault();
+ ms.handleSelect(option.id);
+ }
+ }}
+ />
+ {/snippet}
+
+
+
+
+
+
+ {:else}
+
+
+
+ {#snippet child({ props })}
+ ms.handleOpenChange(true)}
+ disabled={disabled || ms.updating}
+ >
+
+
+ {#if selectedOption}
+
+ {/if}
+
+ {#if ms.updating}
+
+ {/if}
+
+ {/snippet}
+
+
+ {#if selectedOption}
+
+ {selectedOption.model}
+
+ {/if}
+
+ {/if}
+ {/if}
+
+
+{#if ms.showModelDialog}
+ ms.setShowModelDialog(v)}
+ modelId={ms.infoModelId}
+ />
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/models/ModelsSelectorList.svelte b/llama.cpp/tools/ui/src/lib/components/app/models/ModelsSelectorList.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..61a4cf0f662ece97c6af44bb197ff0f5936f58a9
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/models/ModelsSelectorList.svelte
@@ -0,0 +1,72 @@
+
+
+{#snippet defaultOption(item: ModelItem, hideOrgName: boolean)}
+ {@const { option } = item}
+ {@const isSelected = currentModel === option.model || activeId === option.id}
+ {@const isFav = modelsStore.favoriteModelIds.has(option.model)}
+
+ {}}
+ onKeyDown={() => {}}
+ />
+{/snippet}
+
+{#if groups.loaded.length > 0}
+
+ {#each groups.loaded as item (`loaded-${item.option.id}`)}
+ {@render render(item, false)}
+ {/each}
+{/if}
+
+{#if groups.favorites.length > 0}
+
+ {#each groups.favorites as item (`fav-${item.option.id}`)}
+ {@render render(item, true)}
+ {/each}
+{/if}
+
+{#if groups.available.length > 0}
+
+ {#each groups.available as group (group.orgName)}
+ {#if group.orgName}
+
+ {/if}
+ {#each group.items as item (item.option.id)}
+ {@render render(item, true)}
+ {/each}
+ {/each}
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/models/ModelsSelectorOption.svelte b/llama.cpp/tools/ui/src/lib/components/app/models/ModelsSelectorOption.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..981111e20157d600249826653219a16e54d10f0a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/models/ModelsSelectorOption.svelte
@@ -0,0 +1,202 @@
+
+
+ onSelect(option.id)}
+ onmouseenter={onMouseEnter}
+ onkeydown={onKeyDown}
+>
+
+
+
+
+
+
+
+ {#if isLoading}
+
+
+
+ {:else if isFailed}
+
+ {:else if isSleeping}
+
+ {:else if isLoaded}
+
+ {:else}
+
+ {/if}
+
+
+ {#if isLoading}
+
+ {/if}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/models/ModelsSelectorSheet.svelte b/llama.cpp/tools/ui/src/lib/components/app/models/ModelsSelectorSheet.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..a9e9ea1c8d5d52c8ff032b11b859675dbf89d5d4
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/models/ModelsSelectorSheet.svelte
@@ -0,0 +1,208 @@
+
+
+
+ {#if ms.loading && ms.options.length === 0 && ms.isRouter}
+
+
+ Loading models…
+
+ {:else if ms.options.length === 0 && ms.isRouter}
+
No models available.
+ {:else}
+ {@const selectedOption = ms.getDisplayOption()}
+ {@const triggerModel = selectedOption?.model}
+ {@const triggerStatus = triggerModel
+ ? routerModels().find((m) => m.id === triggerModel)?.status?.value
+ : undefined}
+ {@const triggerLoading =
+ !!triggerModel &&
+ (triggerStatus === ServerModelStatus.LOADING ||
+ modelsStore.isModelOperationInProgress(triggerModel))}
+ {@const triggerLoadPercent = triggerLoading
+ ? Math.round(modelLoadFraction(modelsStore.getLoadProgress(triggerModel)) * 100)
+ : 0}
+
+ {#if ms.isRouter}
+
ms.handleOpenChange(true)}
+ >
+
+
+ {#if !selectedOption}
+ Select model
+ {:else}
+
+ {/if}
+
+ {#if ms.updating || ms.isLoadingModel}
+
+ {:else}
+
+ {/if}
+
+ {#if triggerLoading}
+
+ {/if}
+
+
+
+
+
+ Select Model
+
+
+ Choose a model to use for the conversation
+
+
+
+
+
+ ms.setSearchTerm(v)}
+ />
+
+
+
+ {#if !ms.isCurrentModelInCache && currentModel}
+
+
+ {selectedOption?.name || currentModel}
+
+ (not available)
+
+
+ {/if}
+
+ {#if ms.filteredOptions.length === 0}
+
No models found.
+ {/if}
+
+
+
+
+
+
+ {:else}
+
ms.handleOpenChange(true)}
+ disabled={disabled || ms.updating}
+ >
+
+
+
+
+ {#if ms.updating}
+
+ {/if}
+
+ {/if}
+ {/if}
+
+
+{#if ms.showModelDialog}
+ ms.setShowModelDialog(v)}
+ modelId={ms.infoModelId}
+ />
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/models/index.ts b/llama.cpp/tools/ui/src/lib/components/app/models/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..3ac6ecb678b257af12e49c2951704a23169219f8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/models/index.ts
@@ -0,0 +1,111 @@
+/**
+ *
+ * MODELS
+ *
+ * Components for model selection and display. Supports two server modes:
+ * - **Single model mode**: Server runs with one model, selector shows model info
+ * - **Router mode**: Server runs with multiple models, selector enables switching
+ *
+ * Integrates with modelsStore for model data and serverStore for mode detection.
+ *
+ */
+
+/**
+ * **ModelsSelectorDropdown** - Model selection dropdown (desktop)
+ *
+ * Dropdown for selecting AI models with status indicators,
+ * search, and model information display. Adapts UI based on server mode.
+ *
+ * **Architecture:**
+ * - Uses DropdownMenuSearchable for model list
+ * - Integrates with modelsStore for model options and selection
+ * - Detects router vs single mode from serverStore
+ * - Opens DialogModelInformation for model details
+ *
+ * **Features:**
+ * - Searchable model list with keyboard navigation
+ * - Model status indicators (loading/ready/error/updating)
+ * - Model capabilities badges (vision, tools, etc.)
+ * - Current/active model highlighting
+ * - Model information dialog on info button click
+ * - Router mode: shows all available models with status
+ * - Single mode: shows current model name only
+ * - Loading/updating skeleton states
+ * - Global selection support for form integration
+ *
+ * @example
+ * ```svelte
+ * updateModel(id)}
+ * useGlobalSelection
+ * />
+ * ```
+ */
+export { default as ModelsSelectorDropdown } from './ModelsSelectorDropdown.svelte';
+
+/**
+ * **ModelsSelectorList** - Grouped model options list
+ *
+ * Renders grouped model options (loaded, favorites, available) with section
+ * headers and org subgroups. Shared between ModelsSelectorDropdown and ModelsSelectorSheet
+ * to avoid template duplication.
+ *
+ * Accepts an optional `renderOption` snippet to customize how each option is
+ * rendered (e.g., to add keyboard navigation or highlighting).
+ */
+export { default as ModelsSelectorList } from './ModelsSelectorList.svelte';
+
+/**
+ * **ModelsSelectorOption** - Single model option row
+ *
+ * Renders a single model option with selection state, favorite toggle,
+ * load/unload actions, status indicators, and an info button.
+ * Used inside ModelsSelectorList or directly in custom render snippets.
+ */
+export { default as ModelsSelectorOption } from './ModelsSelectorOption.svelte';
+
+/**
+ * **ModelsSelectorSheet** - Mobile model selection sheet
+ *
+ * Bottom sheet variant of ModelsSelectorDropdown optimized for touch interaction
+ * on mobile devices. Same functionality as ModelsSelectorDropdown but uses Sheet UI
+ * instead of DropdownMenu.
+ */
+export { default as ModelsSelectorSheet } from './ModelsSelectorSheet.svelte';
+
+/** * **ModelBadge** - Model name display badge
+ *
+ * Compact badge showing current model name with package icon.
+ * Only visible in single model mode. Supports tooltip and copy functionality.
+ *
+ * **Architecture:**
+ * - Reads model name from modelsStore or prop
+ * - Checks server mode from serverStore
+ * - Uses BadgeInfo for consistent styling
+ *
+ * **Features:**
+ * - Optional copy to clipboard button
+ * - Optional tooltip with model details
+ * - Click handler for model info dialog
+ * - Only renders in model mode (not router)
+ *
+ * @example
+ * ```svelte
+ * showModelInfo = true}
+ * showTooltip
+ * showCopyIcon
+ * />
+ * ```
+ */
+export { default as ModelBadge } from './ModelBadge.svelte';
+
+/**
+ * **ModelId** - Parsed model identifier display
+ *
+ * Displays a model ID with optional org name, parameter badges, quantization,
+ * aliases, and tags. Supports raw mode to show the unprocessed model name.
+ * Respects the user's `showRawModelNames` setting.
+ */
+export { default as ModelId } from './ModelId.svelte';
diff --git a/llama.cpp/tools/ui/src/lib/components/app/models/utils.ts b/llama.cpp/tools/ui/src/lib/components/app/models/utils.ts
new file mode 100644
index 0000000000000000000000000000000000000000..ae1f511e9f667c2ee09b666cd3574c5118126978
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/models/utils.ts
@@ -0,0 +1,75 @@
+import { SvelteMap } from 'svelte/reactivity';
+import type { ModelOption } from '$lib/types/models';
+
+export interface ModelItem {
+ option: ModelOption;
+ flatIndex: number;
+}
+
+export interface OrgGroup {
+ orgName: string | null;
+ items: ModelItem[];
+}
+
+export interface GroupedModelOptions {
+ loaded: ModelItem[];
+ favorites: ModelItem[];
+ available: OrgGroup[];
+}
+
+export function filterModelOptions(options: ModelOption[], searchTerm: string): ModelOption[] {
+ const term = searchTerm.trim().toLowerCase();
+ if (!term) return options;
+
+ return options.filter(
+ (option) =>
+ option.model.toLowerCase().includes(term) ||
+ option.name?.toLowerCase().includes(term) ||
+ option.aliases?.some((alias: string) => alias.toLowerCase().includes(term)) ||
+ option.tags?.some((tag: string) => tag.toLowerCase().includes(term))
+ );
+}
+
+export function groupModelOptions(
+ filteredOptions: ModelOption[],
+ favoriteIds: Set,
+ isModelLoaded: (model: string) => boolean
+): GroupedModelOptions {
+ // Loaded models
+ const loaded: ModelItem[] = [];
+ for (let i = 0; i < filteredOptions.length; i++) {
+ if (isModelLoaded(filteredOptions[i].model)) {
+ loaded.push({ option: filteredOptions[i], flatIndex: i });
+ }
+ }
+
+ // Favorites (excluding loaded)
+ const loadedModelIds = new Set(loaded.map((item) => item.option.model));
+ const favorites: ModelItem[] = [];
+ for (let i = 0; i < filteredOptions.length; i++) {
+ if (
+ favoriteIds.has(filteredOptions[i].model) &&
+ !loadedModelIds.has(filteredOptions[i].model)
+ ) {
+ favorites.push({ option: filteredOptions[i], flatIndex: i });
+ }
+ }
+
+ // Available models grouped by org (excluding loaded and favorites)
+ const available: OrgGroup[] = [];
+ const orgGroups = new SvelteMap();
+ for (let i = 0; i < filteredOptions.length; i++) {
+ const option = filteredOptions[i];
+ if (loadedModelIds.has(option.model) || favoriteIds.has(option.model)) continue;
+
+ const key = option.parsedId?.orgName ?? '';
+ if (!orgGroups.has(key)) orgGroups.set(key, []);
+ orgGroups.get(key)!.push({ option, flatIndex: i });
+ }
+
+ for (const [orgName, items] of orgGroups) {
+ available.push({ orgName: orgName || null, items });
+ }
+
+ return { loaded, favorites, available };
+}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/navigation/DropdownMenuActions.svelte b/llama.cpp/tools/ui/src/lib/components/app/navigation/DropdownMenuActions.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..951831149fc9e1c087032b7d6eba07781c9b7962
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/navigation/DropdownMenuActions.svelte
@@ -0,0 +1,90 @@
+
+
+
+
+
+
+ {#snippet child({ props })}
+ e.stopPropagation()}
+ >
+ {@render iconComponent(triggerIcon, 'h-3 w-3')}
+ {#if triggerTooltip}
+ {triggerTooltip}
+ {/if}
+
+ {/snippet}
+
+ {#if triggerTooltip}
+
+ {triggerTooltip}
+
+ {/if}
+
+
+
+ {#each actions as action, index (action.label)}
+ {#if action.separator && index > 0}
+
+ {/if}
+
+
+
+ {@render iconComponent(
+ action.icon,
+ `h-4 w-4 ${action.variant === 'destructive' ? 'text-destructive' : ''}`
+ )}
+ {action.label}
+
+
+ {#if action.shortcut}
+
+ {/if}
+
+ {/each}
+
+
+
+{#snippet iconComponent(IconComponent: Component, className: string)}
+
+{/snippet}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/navigation/DropdownMenuSearchable.svelte b/llama.cpp/tools/ui/src/lib/components/app/navigation/DropdownMenuSearchable.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..3bd68d3bd6e4c30b3166e8e86214b91484ab3c34
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/navigation/DropdownMenuSearchable.svelte
@@ -0,0 +1,50 @@
+
+
+
+
+
+
+
+ {@render children()}
+
+ {#if isEmpty}
+
{emptyMessage}
+ {/if}
+
+
+{#if footer}
+
+
+ {@render footer()}
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigation.svelte b/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigation.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..fe503f53baa1a0bc6b63d5a0bece8afa0266d964
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigation.svelte
@@ -0,0 +1,291 @@
+
+
+
+
+{#if innerWidth > 768 || (!page.url.hash.includes(ROUTES.SETTINGS) && !page.url.hash.includes(ROUTES.MCP_SERVERS) && !page.url.hash.includes(ROUTES.SEARCH))}
+
+
+
(logoHovered = true)}
+ onmouseleave={() => (logoHovered = false)}
+ >
+
768 ? PanelLeftOpen : Logo}
+ size="lg"
+ iconSize="h-4.5 w-4.5 md:h-4 md:w-4"
+ class="{isExpandedMode
+ ? 'bg-muted! md:bg-foreground/5!'
+ : 'bg-transparent!'} md:h-9 md:w-9 h-10 w-10 rounded-full md:hover:bg-foreground/10! pointer-events-auto"
+ href={isExpandedMode ? ROUTES.START : undefined}
+ onclick={isExpandedMode ? undefined : toggleExpandedMode}
+ tooltip={isExpandedMode ? undefined : 'Open Sidebar'}
+ tooltipSide={TooltipSide.RIGHT}
+ ariaLabel={isExpandedMode ? 'Go to start' : 'Expand navigation'}
+ />
+
+
+ {#if isOnMobile || (isExpandedMode && !alwaysShowOnDesktop)}
+
+ {/if}
+
+
+
+
+ 768 ? isExpandedMode : true}
+ class="px-2"
+ bind:isSearchModeActive
+ bind:searchQuery
+ onSearchDeactivated={() => {
+ isSearchModeActive = false;
+ searchQuery = '';
+ }}
+ onSearchClick={() => {
+ isExpandedMode = true;
+ isSearchModeActive = true;
+ }}
+ onNewChat={() => {
+ if (isMobile.current) {
+ scheduleMobileCollapse();
+ }
+ }}
+ />
+
+ {#if isExpandedMode || isOnMobile}
+
+ {/if}
+
+
+
+{/if}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationActions.svelte b/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationActions.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..f118a68dfc670232d29fe5acf6c1e485296ae3ac
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationActions.svelte
@@ -0,0 +1,196 @@
+
+
+{#snippet itemIcon(IconComponent: Component)}
+
+{/snippet}
+
+{#if isSearchModeActive}
+
+ e.key === 'Escape' && handleSearchModeDeactivate()}
+ placeholder="Search conversations..."
+ />
+
+{:else if isExpandedMode || isOnMobile}
+
+ {#each SIDEBAR_ACTIONS_ITEMS as item, i (item.tooltip)}
+ {@const isActive = isItemActive(item)}
+ {@const isSearchOnMobile = item.icon === Search && isMobile.current}
+ {@const itemHref = isSearchOnMobile ? ROUTES.SEARCH : item.route}
+ {@const itemOnClick = item.route
+ ? () => {
+ onNewChat?.();
+ goto(item.route!);
+ }
+ : isSearchOnMobile
+ ? undefined
+ : onSearchClick}
+ {@const itemTransition = {
+ duration: ICON_STRIP_TRANSITION_DURATION,
+ delay: !initialized
+ ? ICON_STRIP_TRANSITION_DELAY_MULTIPLIER + i * ICON_STRIP_TRANSITION_DELAY_MULTIPLIER
+ : 0,
+ easing: circIn
+ }}
+
+ {#if showIcons}
+
+
+
+ {@render itemIcon(item.icon)}
+
+ {#if showIcons}
+ {item.tooltip}
+ {/if}
+
+
+ {#if item.keys}
+
+ {/if}
+
+
+ {/if}
+ {/each}
+
+{:else}
+
+ {#each SIDEBAR_ACTIONS_ITEMS as item, i (item.tooltip)}
+ {@const isActive = isItemActive(item)}
+ {@const isSearchOnMobile = item.icon === Search && isMobile.current}
+ {@const itemOnClick = item.route
+ ? () => {
+ onNewChat?.();
+ goto(item.route!);
+ }
+ : isSearchOnMobile
+ ? undefined
+ : onSearchClick}
+ {@const itemTransition = {
+ duration: ICON_STRIP_TRANSITION_DURATION,
+ delay: !initialized
+ ? ICON_STRIP_TRANSITION_DELAY_MULTIPLIER + i * ICON_STRIP_TRANSITION_DELAY_MULTIPLIER
+ : 0,
+ easing: circIn
+ }}
+
+ {#if showIcons}
+
+ {/if}
+ {/each}
+
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationConversationItem.svelte b/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationConversationItem.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..b1c2b78f65eaa4ccec0b99630ad5a3bfe6f1fbe7
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationConversationItem.svelte
@@ -0,0 +1,252 @@
+
+
+
+ {
+ if (!e.currentTarget.contains(e.relatedTarget as Node | null)) {
+ handleMouseLeave();
+ }
+ }}
+>
+
+ {#if depth > 0}
+
+
+
+ {#snippet child({ props })}
+
+
+
+ {/snippet}
+
+
+
+ See parent conversation
+
+
+ {/if}
+
+ {#if isLoading}
+
+
+ e.key === 'Enter' && handleStop(e)}
+ role="button"
+ tabindex="0"
+ aria-label="Stop generation"
+ >
+
+
+
+
+
+
+
+ Stop generation
+
+
+ {/if}
+
+
+
+
+ {#if renderActionsDropdown}
+
+ {
+ e.stopPropagation();
+ handleTogglePin();
+ }
+ },
+ {
+ icon: Pencil,
+ label: 'Edit',
+ onclick: handleEdit,
+ shortcut: ['shift', 'cmd', 'e']
+ },
+ {
+ icon: Download,
+ label: 'Export',
+ onclick: (e: Event) => {
+ e.stopPropagation();
+ conversationsStore.downloadConversation(conversation.id);
+ },
+ shortcut: ['shift', 'cmd', 's']
+ },
+ {
+ icon: Trash2,
+ label: 'Delete',
+ onclick: handleDelete,
+ variant: 'destructive',
+ shortcut: ['shift', 'cmd', 'd'],
+ separator: true
+ }
+ ]}
+ />
+
+ {/if}
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationConversationList.svelte b/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationConversationList.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..488e96bbcf617285bc1129f5cc9c9e023974727b
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationConversationList.svelte
@@ -0,0 +1,135 @@
+
+
+{#if isSearchModeActive}
+
+{:else}
+ {#if pinnedConversations.length > 0}
+
+
+
+ {#each pinnedConversations as { conversation, depth } (conversation.id)}
+
+
+
+ {/each}
+
+ {/if}
+
+
+ {#if filteredConversations.length > 0}
+
+ Recent conversations
+
+ {/if}
+
+
+
+ {#each unpinnedConversations as { conversation, depth } (conversation.id)}
+
+
+
+ {/each}
+
+ {#if unpinnedConversations.length === 0}
+
+
+ {recentEmptyMessage}
+
+
+ {/if}
+
+
+
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationSearch.svelte b/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationSearch.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..491e7c34798357269a8b25f37628f7c4faf94610
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationSearch.svelte
@@ -0,0 +1,21 @@
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationSearchResults.svelte b/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationSearchResults.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..92d8fd0bda882f7e5b3121e7151fa46961084d3f
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/navigation/SidebarNavigation/SidebarNavigationSearchResults.svelte
@@ -0,0 +1,76 @@
+
+
+
+ {#if showHeader}
+
+ Search results
+
+ {/if}
+
+
+
+ {#each tree as { conversation, depth } (conversation.id)}
+
+
+
+ {/each}
+
+ {#if tree.length === 0}
+
+
+ {emptyMessage}
+
+
+ {/if}
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/navigation/index.ts b/llama.cpp/tools/ui/src/lib/components/app/navigation/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..e07dde6bc9012d6b69ba2bfc391c90c80545223b
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/navigation/index.ts
@@ -0,0 +1,174 @@
+/**
+ *
+ * NAVIGATION & MENUS
+ *
+ * Components for dropdown menus and action selection.
+ *
+ */
+
+/**
+ * **DropdownMenuSearchable** - Searchable content for dropdown menus
+ *
+ * Renders a search input with filtered content area, empty state, and optional footer.
+ * Designed to be injected into any dropdown container (DropdownMenu.Content,
+ * DropdownMenu.SubContent, etc.) without providing its own Root.
+ *
+ * **Features:**
+ * - Search/filter input
+ * - Keyboard navigation support
+ * - Custom content and footer via snippets
+ * - Empty state message
+ *
+ * @example
+ * ```svelte
+ *
+ * ...
+ *
+ *
+ * {#each items as item} {/each}
+ *
+ *
+ *
+ * ```
+ */
+export { default as DropdownMenuSearchable } from './DropdownMenuSearchable.svelte';
+
+/**
+ * **DropdownMenuActions** - Multi-action dropdown menu
+ *
+ * Dropdown menu for multiple action options with icons and shortcuts.
+ * Supports destructive variants and keyboard shortcut hints.
+ *
+ * **Features:**
+ * - Configurable trigger icon with tooltip
+ * - Action items with icons and labels
+ * - Destructive variant styling
+ * - Keyboard shortcut display
+ * - Separator support between groups
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as DropdownMenuActions } from './DropdownMenuActions.svelte';
+/**
+ * **SidebarNavigation** - Sidebar with actions menu and conversation list
+ *
+ * Collapsible sidebar displaying conversation history with search and
+ * management actions. Integrates with ShadCN sidebar component for
+ * consistent styling and mobile responsiveness.
+ *
+ * **Architecture:**
+ * - Uses ShadCN Sidebar.* components for structure
+ * - Fetches conversations from conversationsStore
+ * - Manages search state and filtered results locally
+ * - Handles conversation CRUD operations via conversationsStore
+ *
+ * **Navigation:**
+ * - Click conversation to navigate to `/chat/[id]`
+ * - New chat button navigates to `/` (root)
+ * - Active conversation highlighted based on route params
+ *
+ * **Conversation Management:**
+ * - Right-click or menu button for context menu
+ * - Rename: Opens inline edit dialog
+ * - Delete: Shows confirmation with conversation preview
+ * - Delete All: Removes all conversations with confirmation
+ *
+ * **Features:**
+ * - Search/filter conversations by title
+ * - Conversation list with message previews (first message truncated)
+ * - Active conversation highlighting
+ * - Mobile-responsive collapse/expand via ShadCN sidebar
+ * - New chat button in header
+ * - Settings button opens DialogChatSettings
+ *
+ * **Exported API:**
+ * - `activateSearchMode()` - Focus search input programmatically
+ * - `editActiveConversation()` - Open rename dialog for current conversation
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as SidebarNavigation } from './SidebarNavigation/SidebarNavigation.svelte';
+
+/**
+ * Single conversation item in sidebar. Displays conversation title (truncated),
+ * last message preview, and timestamp. Shows context menu on right-click with
+ * rename and delete options. Highlights when active (matches current route).
+ * Handles click to navigate and keyboard accessibility.
+ */
+export { default as SidebarNavigationConversationItem } from './SidebarNavigation/SidebarNavigationConversationItem.svelte';
+
+/**
+ * **SidebarNavigationConversationList** - Grouped conversation list
+ *
+ * Pure-presentational list of conversations. Splits items into a Pinned
+ * section (when not in search mode) and a Recent Conversations / Search
+ * Results section with the unpinned items. Item selection, edit, delete,
+ * and stop-generation are delegated to the caller via callbacks.
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as SidebarNavigationConversationList } from './SidebarNavigation/SidebarNavigationConversationList.svelte';
+export { default as SidebarNavigationActions } from './SidebarNavigation/SidebarNavigationActions.svelte';
+
+/**
+ * **SidebarNavigationSearchResults** - Filtered conversation list for search.
+ *
+ * Pure-presentational rendering of the search-mode subtree: "Search results"
+ * header, the matching items rendered through {@link SidebarNavigationConversationItem},
+ * and contextual empty-state messages. Used both inline inside
+ * {@link SidebarNavigationConversationList} (when search mode is active in the
+ * sidebar) and as the body of the mobile `/search` route.
+ *
+ * The caller is expected to provide an already-filtered list via
+ * `filteredConversations` and a `searchQuery` for the empty-state messages.
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as SidebarNavigationSearchResults } from './SidebarNavigation/SidebarNavigationSearchResults.svelte';
+
+/**
+ * Search input for filtering conversations in sidebar. Filters conversation
+ * list by title as user types. Shows clear button when query is not empty.
+ * Integrated into sidebar header with proper styling.
+ */
+export { default as SidebarNavigationSearch } from './SidebarNavigation/SidebarNavigationSearch.svelte';
diff --git a/llama.cpp/tools/ui/src/lib/components/app/server/ServerErrorSplash.svelte b/llama.cpp/tools/ui/src/lib/components/app/server/ServerErrorSplash.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..4da0d1ddfa800c1803dd144faf231647ab6b01c8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/server/ServerErrorSplash.svelte
@@ -0,0 +1,285 @@
+
+
+
+
+
+
+
+
Server Connection Error
+
+
+ {error}
+
+
+
+ {#if isAccessDeniedError && !showApiKeyInput}
+
+
+
+ Enter API Key
+
+
+ {/if}
+
+ {#if showApiKeyInput}
+
+
+
API Key
+
+
+
+ {#if apiKeyState === 'validating'}
+
+
+
+ {:else if apiKeyState === 'success'}
+
+
+
+ {:else if apiKeyState === 'error'}
+
+
+
+ {/if}
+
+ {#if apiKeyError}
+
+ {apiKeyError}
+
+ {/if}
+ {#if apiKeyState === 'success'}
+
+ ✓ API key validated successfully! Connecting...
+
+ {/if}
+
+
+
+ {#if apiKeyState === 'validating'}
+
+ Validating...
+ {:else if apiKeyState === 'success'}
+ Success!
+ {:else}
+ Save & Retry
+ {/if}
+
+ {
+ showApiKeyInput = false;
+ apiKeyState = 'idle';
+ apiKeyError = '';
+ }}
+ variant="outline"
+ class="flex-1"
+ disabled={apiKeyState === 'validating'}
+ >
+ Cancel
+
+
+
+ {/if}
+
+ {#if showRetry}
+
+
+ {#if isServerLoading}
+
+
+ Connecting...
+ {:else}
+
+
+ Retry Connection
+ {/if}
+
+
+ {/if}
+
+ {#if showTroubleshooting}
+
+
+
+ Troubleshooting
+
+
+
+
+
Start the llama-server:
+
+
+
llama-server -hf ggml-org/gemma-3-4b-it-GGUF
+
+
+
or
+
+
+
llama-server -m locally-stored-model.gguf
+
+
+
+ Check that the server is accessible at the correct URL
+
+ Verify your network connection
+
+ Check server logs for any error messages
+
+
+
+
+ {/if}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/server/ServerLoadingSplash.svelte b/llama.cpp/tools/ui/src/lib/components/app/server/ServerLoadingSplash.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..95fa61e93696a3896aa52fb986e626c637d6c0b6
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/server/ServerLoadingSplash.svelte
@@ -0,0 +1,32 @@
+
+
+
+
+
+
+
+
+
+
Connecting to Server
+
+
+ {message}
+
+
+
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/server/ServerStatus.svelte b/llama.cpp/tools/ui/src/lib/components/app/server/ServerStatus.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..86a962de123fa89aaba29a9e3e57a7eff57c911a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/server/ServerStatus.svelte
@@ -0,0 +1,65 @@
+
+
+
+
+
+
+
{getStatusText()}
+
+
+ {#if serverData && !error}
+
+
+
+ {model || 'Unknown Model'}
+
+
+ {#if serverData?.default_generation_settings?.n_ctx}
+
+ ctx: {serverData.default_generation_settings.n_ctx.toLocaleString()}
+
+ {/if}
+ {/if}
+
+ {#if showActions && error}
+
+
+
+ {error}
+
+ {/if}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/server/index.ts b/llama.cpp/tools/ui/src/lib/components/app/server/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..39ac5b482d235164a932f372ac10b0bfe5247c51
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/server/index.ts
@@ -0,0 +1,80 @@
+/**
+ *
+ * SERVER
+ *
+ * Components for displaying server connection state and handling
+ * connection errors. Integrates with serverStore for state management.
+ *
+ */
+
+/**
+ * **ServerStatus** - Server connection status indicator
+ *
+ * Compact status display showing connection state, model name,
+ * and context size. Used in headers and loading screens.
+ *
+ * **Architecture:**
+ * - Reads state from serverStore (props, loading, error)
+ * - Displays model name from modelsStore
+ *
+ * **Features:**
+ * - Status dot: green (connected), yellow (connecting), red (error), gray (unknown)
+ * - Status text label
+ * - Model name badge with icon
+ * - Context size badge
+ * - Optional error action button
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as ServerStatus } from './ServerStatus.svelte';
+
+/**
+ * **ServerErrorSplash** - Full-screen connection error display
+ *
+ * Blocking error screen shown when server connection fails.
+ * Provides retry options and API key input for authentication errors.
+ *
+ * **Architecture:**
+ * - Detects access denied errors for API key flow
+ * - Validates API key against server before saving
+ * - Integrates with settingsStore for API key persistence
+ *
+ * **Features:**
+ * - Error message display with icon
+ * - Retry connection button with loading state
+ * - API key input for authentication errors
+ * - API key validation with success/error feedback
+ * - Troubleshooting section with server start commands
+ * - Animated transitions for UI elements
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as ServerErrorSplash } from './ServerErrorSplash.svelte';
+
+/**
+ * **ServerLoadingSplash** - Full-screen loading display
+ *
+ * Shown during initial server connection. Displays loading animation
+ * with ServerStatus component for real-time connection state.
+ *
+ * **Features:**
+ * - Animated server icon
+ * - Customizable loading message
+ * - Embedded ServerStatus for live updates
+ *
+ * @example
+ * ```svelte
+ *
+ * ```
+ */
+export { default as ServerLoadingSplash } from './ServerLoadingSplash.svelte';
diff --git a/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChat.svelte b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChat.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..41105baa5e629b87ee4b5f5babcc153ed9b382e8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChat.svelte
@@ -0,0 +1,187 @@
+
+
+
+
+
section.slug === activeSlug}
+ getHref={getSectionHref ??
+ ((section: SettingsSection) => RouterService.settings(section.slug))}
+ />
+
+ section.slug === activeSlug}
+ getHref={getSectionHref ??
+ ((section: SettingsSection) => RouterService.settings(section.slug))}
+ bind:this={mobileHeader}
+ />
+
+
+
+
+
+
+
{currentSection.title}
+
+
+ {#if currentSection.title === SETTINGS_SECTION_TITLES.TOOLS}
+
+ {:else if currentSection.title === SETTINGS_SECTION_TITLES.IMPORT_EXPORT}
+
+ {:else if currentSection.fields}
+
+
+
+ {#if currentSection.title === SETTINGS_SECTION_TITLES.GENERAL}
+
+ window.location.reload()}>
+
+ Reload app
+
+
+ {/if}
+
+ {/if}
+
+
+
+
Settings are saved in browser's localStorage
+
+
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatFields.svelte b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatFields.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..7c1c5c8977686ede70254a3e0ec6c51b51c5215c
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatFields.svelte
@@ -0,0 +1,267 @@
+
+
+{#each fields as field (field.key)}
+
+ {#if field.type === SettingsFieldType.INPUT}
+ {@const currentValue = String(localConfig[field.key] ?? '')}
+ {@const serverDefault = currentModelParams[field.key]}
+ {@const isCustomRealTime = (() => {
+ if (serverDefault == null) return false;
+ if (currentValue === '') return false;
+
+ const numericInput = parseFloat(currentValue);
+ const normalizedInput = !isNaN(numericInput)
+ ? Math.round(numericInput * 1000000) / 1000000
+ : currentValue;
+ const normalizedDefault =
+ typeof serverDefault === 'number'
+ ? Math.round(serverDefault * 1000000) / 1000000
+ : serverDefault;
+
+ return normalizedInput !== normalizedDefault;
+ })()}
+
+
+
+ {field.label}
+
+ {#if field.isExperimental}
+
+ {/if}
+
+ {#if isCustomRealTime}
+
+ {/if}
+
+
+
+ {
+ // Update local config immediately for real-time badge feedback
+ onConfigChange(field.key, e.currentTarget.value);
+ }}
+ placeholder={currentModelParams[field.key] != null
+ ? `Default: ${normalizeFloatingPoint(currentModelParams[field.key])}`
+ : ''}
+ class="w-full {isCustomRealTime ? 'pr-8' : ''}"
+ />
+ {#if isCustomRealTime}
+ {
+ settingsStore.resetParameterToServerDefault(field.key);
+ onConfigChange(field.key, '');
+ }}
+ class="absolute top-1/2 right-2 inline-flex h-5 w-5 -translate-y-1/2 items-center justify-center rounded transition-colors hover:bg-muted"
+ aria-label="Reset to default"
+ title="Reset to default"
+ >
+
+
+ {/if}
+
+ {#if field.help || SETTING_CONFIG_INFO[field.key]}
+
+ {@html field.help || SETTING_CONFIG_INFO[field.key]}
+
+ {/if}
+ {:else if field.type === SettingsFieldType.TEXTAREA}
+ {#if field.label}
+
+ {field.label}
+
+ {#if field.isExperimental}
+
+ {/if}
+
+ {/if}
+
+
+{/each}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatImportExportSection.svelte b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatImportExportSection.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..6b36526c333491db5c431223dfe99b72a0e4f59d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatImportExportSection.svelte
@@ -0,0 +1,62 @@
+
+
+
+
{title}
+
+
{description}
+
+
+
+
+ {buttonText}
+
+
+ {#if summary && summary.show && summary.items.length > 0}
+
+
+ {summary.verb}
+ {summary.items.length} conversation{summary.items.length === 1 ? '' : 's'}
+
+
+
+ {#each summary.items.slice(0, 10) as conv (conv.id)}
+ • {conv.name || 'Untitled conversation'}
+ {/each}
+
+ {#if summary.items.length > 10}
+ ... and {summary.items.length - 10} more
+ {/if}
+
+
+ {/if}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatImportExportTab.svelte b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatImportExportTab.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..a86d68584c0162bbaed013f77ea9e9a505abbea2
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatImportExportTab.svelte
@@ -0,0 +1,333 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ (showExportDialog = false)}
+ onConfirm={handleExportConfirm}
+/>
+
+ (showImportDialog = false)}
+ onConfirm={handleImportConfirm}
+/>
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatParameterSourceIndicator.svelte b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatParameterSourceIndicator.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..1407eb87ebe8691cd613d494db33e825455f9392
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatParameterSourceIndicator.svelte
@@ -0,0 +1,19 @@
+
+
+
+
+
+ Custom
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatToolsTab.svelte b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatToolsTab.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..b568324965802a5de3923c9a84728096d4027b69
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatToolsTab.svelte
@@ -0,0 +1,102 @@
+
+
+{#if groups.length === 0}
+ No tools available
+{:else}
+
+ {#each groups as group (group.label)}
+ {@const isExpanded = expandedGroups.has(group.label)}
+
toggleExpanded(group.label)}>
+
+ {#if isExpanded}
+
+ {:else}
+
+ {/if}
+
+ {@const faviconUrl = group.serverId ? mcpStore.getServerFavicon(group.serverId) : null}
+
+
+
+
+
+
+ {group.tools.length} tool{group.tools.length !== 1 ? 's' : ''}
+
+
+
+
+
+
+
+ Tool
+ Enabled
+ Always allow
+
+
+ {#each group.tools as entry (entry.key)}
+ {@const toolName = entry.definition.function.name}
+ {@const isEnabled = toolsStore.isToolEnabled(entry.key)}
+ {@const permissionKey = entry.key}
+ {@const isAlwaysAllowed = permissionsStore.hasTool(permissionKey)}
+
+
+
+
+
+ toolsStore.toggleTool(entry.key)}
+ class="h-4 w-4"
+ />
+
+
+
+ {
+ if (isAlwaysAllowed) {
+ permissionsStore.revokeTool(permissionKey);
+ } else {
+ permissionsStore.allowTool(permissionKey);
+ }
+ }}
+ class="h-4 w-4"
+ />
+
+
+ {/each}
+
+
+
+ {/each}
+
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChatDesktopSidebar.svelte b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChatDesktopSidebar.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..8723217b29a90662b05b6978ecbf595884794138
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChatDesktopSidebar.svelte
@@ -0,0 +1,51 @@
+
+
+
+
+
+
+
Settings
+
+
+
+ {#each sections as section (section.title)}
+ {#if getHref}
+
+
+ {section.title}
+
+ {:else}
+ onSectionChange?.(section.title)}
+ >
+
+ {section.title}
+
+ {/if}
+ {/each}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChatMobileHeader.svelte b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChatMobileHeader.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..15eedd108b1e9935ad1eede458d5475fb7818cd0
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsChatMobileHeader.svelte
@@ -0,0 +1,107 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsFooter.svelte b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsFooter.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..afc37377d0caac81e9df325dba0635d8719bd065
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsFooter.svelte
@@ -0,0 +1,59 @@
+
+
+
+
+
+
+
+ Reset to default
+
+
+
+
Save settings
+
+
+
+
+
+ Reset Settings to Default
+
+ Are you sure you want to reset all settings to their default values? This will reset all
+ parameters to the values provided by the server's /props endpoint and remove all your custom
+ configurations.
+
+
+
+ Cancel
+ Reset to Default
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsGroup.svelte b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsGroup.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..113d32176b873712e0483685274adef0f5995a57
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsGroup.svelte
@@ -0,0 +1,18 @@
+
+
+
+
{title}
+
+
+ {@render children()}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsMcpServers.svelte b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsMcpServers.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..e35cfadbbbe70c50f34e7c800cbb3c8e07b28f02
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/settings/SettingsMcpServers.svelte
@@ -0,0 +1,139 @@
+
+
+
+
+
+
+
+
+
+
MCP Servers
+
+
+
(isAddingServer = true)}
+ >
+
+
+ Add New Server
+
+
+
+
+
+
+ {#if servers.length === 0 && !isAddingServer}
+
+ No MCP Servers configured yet. Add one to enable agentic features.
+
+ {/if}
+
+ {#if servers.length > 0}
+
+ {#each servers as server (server.id)}
+ {#if !initialLoadComplete}
+
+ {:else}
+ {
+ const wasEnabled = conversationsStore.isMcpServerEnabledForChat(server.id);
+ await conversationsStore.toggleMcpServerForChat(server.id);
+ if (!wasEnabled) {
+ toolsStore.enableAllToolsForServer(server.id);
+ }
+ }}
+ onUpdate={(updates) => mcpStore.updateServer(server.id, updates)}
+ onDelete={() => mcpStore.removeServer(server.id)}
+ />
+ {/if}
+ {/each}
+
+ {/if}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/app/settings/index.ts b/llama.cpp/tools/ui/src/lib/components/app/settings/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..63f9651df62dad97d543ece4d55c95ba18933de3
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/app/settings/index.ts
@@ -0,0 +1,76 @@
+/**
+ * Full chat settings page layout with sidebar, mobile header, and content area.
+ * Manages local configuration state, section navigation, and context setup.
+ * Accepts an optional `initialSection` prop to override the URL-based section resolution.
+ */
+export { default as SettingsChat } from './SettingsChat/SettingsChat.svelte';
+
+/**
+ * Desktop sidebar navigation for chat settings.
+ * Displays a list of settings sections with icons and titles.
+ * Supports both hash-link navigation (via `getHref`) and in-app section switching (via `onSectionChange`).
+ */
+export { default as SettingsChatDesktopSidebar } from './SettingsChatDesktopSidebar.svelte';
+
+/**
+ * Mobile header with a horizontally scrollable section picker for chat settings.
+ * Shows chevron buttons for scroll navigation and highlights the active section.
+ * Supports both hash-link navigation (via `getHref`) and in-app section switching (via `onSectionChange`).
+ */
+export { default as SettingsChatMobileHeader } from './SettingsChatMobileHeader.svelte';
+
+/**
+ * Badge indicating parameter source for sampling settings. Shows one of:
+ * - **Custom**: User has explicitly set this value (orange badge)
+ * - **Server Props**: Using default from `/props` endpoint (blue badge)
+ * - **Default**: Using app default, server props unavailable (gray badge)
+ * Updates in real-time as user types to show immediate feedback.
+ */
+export { default as SettingsChatParameterSourceIndicator } from './SettingsChat/SettingsChatParameterSourceIndicator.svelte';
+
+/**
+ * Section wrapper for settings panels. Displays a title heading with
+ * child content in a structured layout.
+ */
+export { default as SettingsGroup } from './SettingsGroup.svelte';
+
+/**
+ * Footer with save/cancel buttons for settings panel. Positioned at bottom
+ * of settings dialog. Save button commits form state to config store,
+ * cancel button triggers reset and close.
+ */
+export { default as SettingsFooter } from './SettingsFooter.svelte';
+
+/**
+ * Settings Import/Export panel.
+ * Provides UI for importing and exporting chat conversations.
+ */
+export { default as SettingsChatImportExportTab } from './SettingsChat/SettingsChatImportExportTab.svelte';
+
+/**
+ * Section wrapper for import/export sections. Displays a title, description,
+ * icon button, and optional summary of recent actions.
+ */
+export { default as SettingsChatImportExportSection } from './SettingsChat/SettingsChatImportExportSection.svelte';
+
+/**
+ * MCP Servers configuration panel.
+ * Provides UI for managing Model Context Protocol (MCP) server connections.
+ */
+export { default as SettingsMcpServers } from './SettingsMcpServers.svelte';
+
+/**
+ * Form fields renderer for individual settings. Generates appropriate input
+ * components based on field type (text, number, select, checkbox, textarea).
+ * Handles validation, help text display, and parameter source indicators.
+ */
+export { default as SettingsChatFields } from './SettingsChat/SettingsChatFields.svelte';
+
+/**
+ * **SettingsChatToolsTab** - Tools configuration tab for chat settings
+ *
+ * Displays available tools grouped by source (built-in, MCP, custom) with
+ * toggles to enable/disable individual tools and tool groups. Shows MCP
+ * server favicons and permission management controls.
+ */
+export { default as SettingsChatToolsTab } from './SettingsChat/SettingsChatToolsTab.svelte';
diff --git a/llama.cpp/tools/ui/src/lib/components/pwa/PwaMetaTags.svelte b/llama.cpp/tools/ui/src/lib/components/pwa/PwaMetaTags.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..a3a406f26fafce40306802ca3c6e1d8a8be584f3
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/pwa/PwaMetaTags.svelte
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/pwa/PwaRefreshAlert.svelte b/llama.cpp/tools/ui/src/lib/components/pwa/PwaRefreshAlert.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..500abdc135617a054f7cc1be57ef7cf6f149b2ed
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/pwa/PwaRefreshAlert.svelte
@@ -0,0 +1,35 @@
+
+
+{#if needRefresh}
+
+
+ Update available
+
+
+
+ A new version is available. Reload to update.
+
+ {
+ updateServiceWorker();
+
+ if (forceReload) {
+ window.location.reload();
+ }
+
+ needRefresh = false;
+ }}
+ >
+ Reload
+
+
+
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/pwa/index.ts b/llama.cpp/tools/ui/src/lib/components/pwa/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..fbaaae2396bb0b0cc5a60a5c638d10a3c8aeee8d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/pwa/index.ts
@@ -0,0 +1,2 @@
+export { default as PwaMetaTags } from './PwaMetaTags.svelte';
+export { default as PwaRefreshAlert } from './PwaRefreshAlert.svelte';
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-action.svelte b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-action.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..162107eb1ef8650b46a28d7fa22d8148248c8e11
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-action.svelte
@@ -0,0 +1,18 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-cancel.svelte b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-cancel.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..6b3f354a91dcfc7a27bb4b6fad86c64d0951d53a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-cancel.svelte
@@ -0,0 +1,18 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-content.svelte b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-content.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..c0bb2a34e4f216f1ef9ffaf663753800fbec7fdd
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-content.svelte
@@ -0,0 +1,35 @@
+
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-description.svelte b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-description.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..84735d870c669b27050f6496305689a29dca4e5b
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-description.svelte
@@ -0,0 +1,17 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-footer.svelte b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-footer.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..da0f7be74b65a78018d4944120c5452556b5bbab
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-footer.svelte
@@ -0,0 +1,23 @@
+
+
+*]:flex-1 sm:[&>*]:flex-none',
+ className
+ )}
+ {...restProps}
+>
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-header.svelte b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-header.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..fa6539db2902b32c307d09a4c228a2732063abd0
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-header.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-overlay.svelte b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-overlay.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..b047dcf6c450f07b506fb0d71c42f293fe3d9ec6
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-overlay.svelte
@@ -0,0 +1,20 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-title.svelte b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-title.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..4c610aa602336970a6ff3a7e526d95bc42189245
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-title.svelte
@@ -0,0 +1,17 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-trigger.svelte b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-trigger.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..51a3da159f60875caa6b45999f10a654f36bea5f
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/alert-dialog-trigger.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..a4439bc2e16ffc262cb7061c2f92b5d92f461ad0
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert-dialog/index.ts
@@ -0,0 +1,39 @@
+import { AlertDialog as AlertDialogPrimitive } from 'bits-ui';
+import Trigger from './alert-dialog-trigger.svelte';
+import Title from './alert-dialog-title.svelte';
+import Action from './alert-dialog-action.svelte';
+import Cancel from './alert-dialog-cancel.svelte';
+import Footer from './alert-dialog-footer.svelte';
+import Header from './alert-dialog-header.svelte';
+import Overlay from './alert-dialog-overlay.svelte';
+import Content from './alert-dialog-content.svelte';
+import Description from './alert-dialog-description.svelte';
+
+const Root = AlertDialogPrimitive.Root;
+const Portal = AlertDialogPrimitive.Portal;
+
+export {
+ Root,
+ Title,
+ Action,
+ Cancel,
+ Portal,
+ Footer,
+ Header,
+ Trigger,
+ Overlay,
+ Content,
+ Description,
+ //
+ Root as AlertDialog,
+ Title as AlertDialogTitle,
+ Action as AlertDialogAction,
+ Cancel as AlertDialogCancel,
+ Portal as AlertDialogPortal,
+ Footer as AlertDialogFooter,
+ Header as AlertDialogHeader,
+ Trigger as AlertDialogTrigger,
+ Overlay as AlertDialogOverlay,
+ Content as AlertDialogContent,
+ Description as AlertDialogDescription
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert/alert-description.svelte b/llama.cpp/tools/ui/src/lib/components/ui/alert/alert-description.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..440d0069d3b1446df4eb204565139607648353f0
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert/alert-description.svelte
@@ -0,0 +1,23 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert/alert-title.svelte b/llama.cpp/tools/ui/src/lib/components/ui/alert/alert-title.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..0721aebf12a15fa36a51d6bbc8be57fad0bdb62a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert/alert-title.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert/alert.svelte b/llama.cpp/tools/ui/src/lib/components/ui/alert/alert.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..7d79e4bc0edf4962d1cb59eaba3382be6f72f8b8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert/alert.svelte
@@ -0,0 +1,44 @@
+
+
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/alert/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/alert/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..5e0f854da6f6184a15f5d70e7872193945ac5729
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/alert/index.ts
@@ -0,0 +1,14 @@
+import Root from './alert.svelte';
+import Description from './alert-description.svelte';
+import Title from './alert-title.svelte';
+export { alertVariants, type AlertVariant } from './alert.svelte';
+
+export {
+ Root,
+ Description,
+ Title,
+ //
+ Root as Alert,
+ Description as AlertDescription,
+ Title as AlertTitle
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/badge/badge.svelte b/llama.cpp/tools/ui/src/lib/components/ui/badge/badge.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..9fbf0b80a592b56bfabf90dc566797200612b8d7
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/badge/badge.svelte
@@ -0,0 +1,51 @@
+
+
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/badge/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/badge/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..f05fb87facb3830f097521b7643349a790f58425
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/badge/index.ts
@@ -0,0 +1,2 @@
+export { default as Badge } from './badge.svelte';
+export { badgeVariants, type BadgeVariant } from './badge.svelte';
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/button-group/button-group-root.svelte b/llama.cpp/tools/ui/src/lib/components/ui/button-group/button-group-root.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..4587ec38ec3f44a9ca47d9fd0a5a0d7bb1f1e914
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/button-group/button-group-root.svelte
@@ -0,0 +1,21 @@
+
+
+*:first-child]:rounded-r-none [&>*:last-child]:rounded-l-none [&>*:not(:first-child):not(:last-child)]:rounded-none',
+ className
+ )}
+ {...restProps}
+>
+ {@render children()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/button-group/button-group-separator.svelte b/llama.cpp/tools/ui/src/lib/components/ui/button-group/button-group-separator.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..d13c573c8e2f8ceb92d71574ac82412b5f9ad6d2
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/button-group/button-group-separator.svelte
@@ -0,0 +1,8 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/button-group/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/button-group/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..b0d2e30441b87d19b2151c4f3e9855584524d5a4
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/button-group/index.ts
@@ -0,0 +1,2 @@
+export { default as Root } from './button-group-root.svelte';
+export { default as Separator } from './button-group-separator.svelte';
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/button/button.svelte b/llama.cpp/tools/ui/src/lib/components/ui/button/button.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..165b9fd6895335faddfa5eb60b30a8890cce00cd
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/button/button.svelte
@@ -0,0 +1,90 @@
+
+
+
+
+{#if href}
+
+ {@render children?.()}
+
+{:else}
+
+ {@render children?.()}
+
+{/if}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/button/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/button/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..5414d9d34a73454903a9ae1d9826257d42fd8d10
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/button/index.ts
@@ -0,0 +1,17 @@
+import Root, {
+ type ButtonProps,
+ type ButtonSize,
+ type ButtonVariant,
+ buttonVariants
+} from './button.svelte';
+
+export {
+ Root,
+ type ButtonProps as Props,
+ //
+ Root as Button,
+ buttonVariants,
+ type ButtonProps,
+ type ButtonSize,
+ type ButtonVariant
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/card/card-action.svelte b/llama.cpp/tools/ui/src/lib/components/ui/card/card-action.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..0d4e965a67b3d649d34e0d18227bde04532f7c74
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/card/card-action.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/card/card-content.svelte b/llama.cpp/tools/ui/src/lib/components/ui/card/card-content.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..c68f6136078b11defbce249dc981e3b648f93cac
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/card/card-content.svelte
@@ -0,0 +1,15 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/card/card-description.svelte b/llama.cpp/tools/ui/src/lib/components/ui/card/card-description.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..81578dfdf8b8ca1e6e734f35b0d5632b80bbc4e1
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/card/card-description.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/card/card-footer.svelte b/llama.cpp/tools/ui/src/lib/components/ui/card/card-footer.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..0366459f8e6b1991f54090c61bb96cedd127c138
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/card/card-footer.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/card/card-header.svelte b/llama.cpp/tools/ui/src/lib/components/ui/card/card-header.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..74ab1639bdff163aa0a554907350de0119b9fa7a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/card/card-header.svelte
@@ -0,0 +1,23 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/card/card-title.svelte b/llama.cpp/tools/ui/src/lib/components/ui/card/card-title.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..8dfc062dc38d27014265ea3b07bb191e685dea5f
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/card/card-title.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/card/card.svelte b/llama.cpp/tools/ui/src/lib/components/ui/card/card.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..d0a57d0c9704da401a68810c11473160b116ac53
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/card/card.svelte
@@ -0,0 +1,25 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/card/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/card/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..77d36747788e7a492ecbb236cc6e5c1fa8fe6428
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/card/index.ts
@@ -0,0 +1,25 @@
+import Root from './card.svelte';
+import Content from './card-content.svelte';
+import Description from './card-description.svelte';
+import Footer from './card-footer.svelte';
+import Header from './card-header.svelte';
+import Title from './card-title.svelte';
+import Action from './card-action.svelte';
+
+export {
+ Root,
+ Content,
+ Description,
+ Footer,
+ Header,
+ Title,
+ Action,
+ //
+ Root as Card,
+ Content as CardContent,
+ Description as CardDescription,
+ Footer as CardFooter,
+ Header as CardHeader,
+ Title as CardTitle,
+ Action as CardAction
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/checkbox/checkbox.svelte b/llama.cpp/tools/ui/src/lib/components/ui/checkbox/checkbox.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..aafa071881701a544fad2a45ea959a64b46ee55b
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/checkbox/checkbox.svelte
@@ -0,0 +1,36 @@
+
+
+
+ {#snippet children({ checked, indeterminate })}
+
+ {#if checked}
+
+ {:else if indeterminate}
+
+ {/if}
+
+ {/snippet}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/checkbox/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/checkbox/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..5c276719befc1a25da540e07f2dcd2de65cc3a16
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/checkbox/index.ts
@@ -0,0 +1,6 @@
+import Root from './checkbox.svelte';
+export {
+ Root,
+ //
+ Root as Checkbox
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/collapsible/collapsible-content.svelte b/llama.cpp/tools/ui/src/lib/components/ui/collapsible/collapsible-content.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..59b068c5f3ef2b74679e2297904fef09047f7b46
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/collapsible/collapsible-content.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/collapsible/collapsible-trigger.svelte b/llama.cpp/tools/ui/src/lib/components/ui/collapsible/collapsible-trigger.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..c88cebaff3f972eeba04df6e91ddc216211f4922
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/collapsible/collapsible-trigger.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/collapsible/collapsible.svelte b/llama.cpp/tools/ui/src/lib/components/ui/collapsible/collapsible.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..7a8c5da46811a97fc2ba0ac9f4dd3c04859723f2
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/collapsible/collapsible.svelte
@@ -0,0 +1,11 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/collapsible/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/collapsible/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..8181f6448d98ddfbade523e410f71d37bdc17f44
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/collapsible/index.ts
@@ -0,0 +1,13 @@
+import Root from './collapsible.svelte';
+import Trigger from './collapsible-trigger.svelte';
+import Content from './collapsible-content.svelte';
+
+export {
+ Root,
+ Content,
+ Trigger,
+ //
+ Root as Collapsible,
+ Content as CollapsibleContent,
+ Trigger as CollapsibleTrigger
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-close.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-close.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..e8a96a773163dcf63fb34fa3281e2b83600d7f5b
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-close.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-content.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-content.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..0e1b07c40ed203a75dff262acda532f049e76af7
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-content.svelte
@@ -0,0 +1,43 @@
+
+
+
+
+
+ {@render children?.()}
+ {#if showCloseButton}
+
+
+ Close
+
+ {/if}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-description.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-description.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..6c0c192316266ac5db18eae7910ddcb01c09b5cd
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-description.svelte
@@ -0,0 +1,17 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-footer.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-footer.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..abf948fc8ea9c970d6552aef3d4413e8c40a401f
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-footer.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-header.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-header.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..7ba9ba17b0dccd12120bc74255f91db3251f49ab
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-header.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-overlay.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-overlay.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..a7803f9036802cdca0b54d7094c6e3e5822081c5
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-overlay.svelte
@@ -0,0 +1,20 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-title.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-title.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..e8c99c5d95080ec45a368242405f95637cadf8c2
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-title.svelte
@@ -0,0 +1,17 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-trigger.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-trigger.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..ac04d9fe453d39aac03f42cdf0d8e08e9d847333
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dialog/dialog-trigger.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dialog/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/dialog/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..d9e5fb86efe22c051478774f1b179ba6da4dfdda
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dialog/index.ts
@@ -0,0 +1,37 @@
+import { Dialog as DialogPrimitive } from 'bits-ui';
+
+import Title from './dialog-title.svelte';
+import Footer from './dialog-footer.svelte';
+import Header from './dialog-header.svelte';
+import Overlay from './dialog-overlay.svelte';
+import Content from './dialog-content.svelte';
+import Description from './dialog-description.svelte';
+import Trigger from './dialog-trigger.svelte';
+import Close from './dialog-close.svelte';
+
+const Root = DialogPrimitive.Root;
+const Portal = DialogPrimitive.Portal;
+
+export {
+ Root,
+ Title,
+ Portal,
+ Footer,
+ Header,
+ Trigger,
+ Overlay,
+ Content,
+ Description,
+ Close,
+ //
+ Root as Dialog,
+ Title as DialogTitle,
+ Portal as DialogPortal,
+ Footer as DialogFooter,
+ Header as DialogHeader,
+ Trigger as DialogTrigger,
+ Overlay as DialogOverlay,
+ Content as DialogContent,
+ Description as DialogDescription,
+ Close as DialogClose
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-checkbox-item.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-checkbox-item.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..e71acefab673b66dcc63b8ac6004b0b58a1c190f
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-checkbox-item.svelte
@@ -0,0 +1,41 @@
+
+
+
+ {#snippet children({ checked, indeterminate })}
+
+ {#if indeterminate}
+
+ {:else}
+
+ {/if}
+
+ {@render childrenProp?.()}
+ {/snippet}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-content.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-content.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..0ca0d3964acf789ca1781115d4914d681bc6941c
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-content.svelte
@@ -0,0 +1,27 @@
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-group-heading.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-group-heading.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..f2179668b5612a1c3601a34b9a9722c2e767f41b
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-group-heading.svelte
@@ -0,0 +1,22 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-group.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-group.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..261ab7e6399029ce8750afdd3c0d3b8788f3cc01
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-group.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-item.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-item.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..1ac561595d98c38738f5b8c6c8ebe4f6b8df0c74
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-item.svelte
@@ -0,0 +1,27 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-label.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-label.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..15b546ea57c31cabe16fa4c1a17301628c1edde3
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-label.svelte
@@ -0,0 +1,24 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-radio-group.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-radio-group.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..3e98749198cae5a18fe5981e90e01c29dcf398c8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-radio-group.svelte
@@ -0,0 +1,16 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-radio-item.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-radio-item.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..97ba772838739b9873f6facbb8138adf9a8bb768
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-radio-item.svelte
@@ -0,0 +1,31 @@
+
+
+
+ {#snippet children({ checked })}
+
+ {#if checked}
+
+ {/if}
+
+ {@render childrenProp?.({ checked })}
+ {/snippet}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-separator.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-separator.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..17b64ac9c2477ff9e278b7599b33b6f1e7082f0a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-separator.svelte
@@ -0,0 +1,17 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-shortcut.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-shortcut.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..c3ccc21920cb5d5082b86803cb7e05730cb365de
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-shortcut.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-sub-content.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-sub-content.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..e26c51cdc23c7f350b22ebc328f396cf39293adc
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-sub-content.svelte
@@ -0,0 +1,20 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-sub-trigger.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-sub-trigger.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..550a789ce852d220f3d1076d01300e8544b08e4a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-sub-trigger.svelte
@@ -0,0 +1,29 @@
+
+
+
+ {@render children?.()}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-trigger.svelte b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-trigger.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..032b6453159dab5cb49e6a78c8bbe7287b78aa3c
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/dropdown-menu-trigger.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..aeb398e0611336e9f6317169ea66f953c4ae84ae
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/dropdown-menu/index.ts
@@ -0,0 +1,49 @@
+import { DropdownMenu as DropdownMenuPrimitive } from 'bits-ui';
+import CheckboxItem from './dropdown-menu-checkbox-item.svelte';
+import Content from './dropdown-menu-content.svelte';
+import Group from './dropdown-menu-group.svelte';
+import Item from './dropdown-menu-item.svelte';
+import Label from './dropdown-menu-label.svelte';
+import RadioGroup from './dropdown-menu-radio-group.svelte';
+import RadioItem from './dropdown-menu-radio-item.svelte';
+import Separator from './dropdown-menu-separator.svelte';
+import Shortcut from './dropdown-menu-shortcut.svelte';
+import Trigger from './dropdown-menu-trigger.svelte';
+import SubContent from './dropdown-menu-sub-content.svelte';
+import SubTrigger from './dropdown-menu-sub-trigger.svelte';
+import GroupHeading from './dropdown-menu-group-heading.svelte';
+const Sub = DropdownMenuPrimitive.Sub;
+const Root = DropdownMenuPrimitive.Root;
+
+export {
+ CheckboxItem,
+ Content,
+ Root as DropdownMenu,
+ CheckboxItem as DropdownMenuCheckboxItem,
+ Content as DropdownMenuContent,
+ Group as DropdownMenuGroup,
+ Item as DropdownMenuItem,
+ Label as DropdownMenuLabel,
+ RadioGroup as DropdownMenuRadioGroup,
+ RadioItem as DropdownMenuRadioItem,
+ Separator as DropdownMenuSeparator,
+ Shortcut as DropdownMenuShortcut,
+ Sub as DropdownMenuSub,
+ SubContent as DropdownMenuSubContent,
+ SubTrigger as DropdownMenuSubTrigger,
+ Trigger as DropdownMenuTrigger,
+ GroupHeading as DropdownMenuGroupHeading,
+ Group,
+ GroupHeading,
+ Item,
+ Label,
+ RadioGroup,
+ RadioItem,
+ Root,
+ Separator,
+ Shortcut,
+ Sub,
+ SubContent,
+ SubTrigger,
+ Trigger
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/input/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/input/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..15c0933dc845c449221a24445ccf2baf2d7c3149
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/input/index.ts
@@ -0,0 +1,7 @@
+import Root from './input.svelte';
+
+export {
+ Root,
+ //
+ Root as Input
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/input/input.svelte b/llama.cpp/tools/ui/src/lib/components/ui/input/input.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..2b6279b6420de0b5efe201d9566fc8ef0267abb1
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/input/input.svelte
@@ -0,0 +1,52 @@
+
+
+{#if type === 'file'}
+
+{:else}
+
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/label/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/label/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..808d14152c07dc9219312d17a7f2bb155820a7a6
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/label/index.ts
@@ -0,0 +1,7 @@
+import Root from './label.svelte';
+
+export {
+ Root,
+ //
+ Root as Label
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/label/label.svelte b/llama.cpp/tools/ui/src/lib/components/ui/label/label.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..9da4ae369dfbbb638dc5743b7b5c3333eb28331a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/label/label.svelte
@@ -0,0 +1,20 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/popover/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/popover/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..c5937fb3a04309294233fa1655a3ffd44dadcf65
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/popover/index.ts
@@ -0,0 +1,19 @@
+import Root from './popover.svelte';
+import Close from './popover-close.svelte';
+import Content from './popover-content.svelte';
+import Trigger from './popover-trigger.svelte';
+import Portal from './popover-portal.svelte';
+
+export {
+ Root,
+ Content,
+ Trigger,
+ Close,
+ Portal,
+ //
+ Root as Popover,
+ Content as PopoverContent,
+ Trigger as PopoverTrigger,
+ Close as PopoverClose,
+ Portal as PopoverPortal
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/popover/popover-close.svelte b/llama.cpp/tools/ui/src/lib/components/ui/popover/popover-close.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..dc4dec4b339525ba50bde8e833117f8b37f73a2e
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/popover/popover-close.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/popover/popover-content.svelte b/llama.cpp/tools/ui/src/lib/components/ui/popover/popover-content.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..b46e928b1b90c1501644ed27530b5814cacbfab1
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/popover/popover-content.svelte
@@ -0,0 +1,37 @@
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/popover/popover-portal.svelte b/llama.cpp/tools/ui/src/lib/components/ui/popover/popover-portal.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..25efb877b73fc8100a4e797c6e5d28f6166c87e3
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/popover/popover-portal.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/popover/popover-trigger.svelte b/llama.cpp/tools/ui/src/lib/components/ui/popover/popover-trigger.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..5ef3d0e932464348a90816fa0e12e3224e2792da
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/popover/popover-trigger.svelte
@@ -0,0 +1,17 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/popover/popover.svelte b/llama.cpp/tools/ui/src/lib/components/ui/popover/popover.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..f39b867a694bc3608303a289f4a66db0911f4244
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/popover/popover.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/scroll-area/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/scroll-area/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..d5468067dee8acc4ee158d9201fb462fb9929f78
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/scroll-area/index.ts
@@ -0,0 +1,10 @@
+import Scrollbar from './scroll-area-scrollbar.svelte';
+import Root from './scroll-area.svelte';
+
+export {
+ Root,
+ Scrollbar,
+ //,
+ Root as ScrollArea,
+ Scrollbar as ScrollAreaScrollbar
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/scroll-area/scroll-area-scrollbar.svelte b/llama.cpp/tools/ui/src/lib/components/ui/scroll-area/scroll-area-scrollbar.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..3f0d00d5ebf87a2de9fbd3e42dc62c88c2bbce83
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/scroll-area/scroll-area-scrollbar.svelte
@@ -0,0 +1,31 @@
+
+
+
+ {@render children?.()}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/scroll-area/scroll-area.svelte b/llama.cpp/tools/ui/src/lib/components/ui/scroll-area/scroll-area.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..ba6f8382e5679ca3b044bcc7f5d8381b5d6d5bcd
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/scroll-area/scroll-area.svelte
@@ -0,0 +1,40 @@
+
+
+
+
+ {@render children?.()}
+
+ {#if orientation === 'vertical' || orientation === 'both'}
+
+ {/if}
+ {#if orientation === 'horizontal' || orientation === 'both'}
+
+ {/if}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/select/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/select/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..bfa73d90ebd3e1c750682062831233bfbd88b575
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/select/index.ts
@@ -0,0 +1,37 @@
+import { Select as SelectPrimitive } from 'bits-ui';
+
+import Group from './select-group.svelte';
+import Label from './select-label.svelte';
+import Item from './select-item.svelte';
+import Content from './select-content.svelte';
+import Trigger from './select-trigger.svelte';
+import Separator from './select-separator.svelte';
+import ScrollDownButton from './select-scroll-down-button.svelte';
+import ScrollUpButton from './select-scroll-up-button.svelte';
+import GroupHeading from './select-group-heading.svelte';
+
+const Root = SelectPrimitive.Root;
+
+export {
+ Root,
+ Group,
+ Label,
+ Item,
+ Content,
+ Trigger,
+ Separator,
+ ScrollDownButton,
+ ScrollUpButton,
+ GroupHeading,
+ //
+ Root as Select,
+ Group as SelectGroup,
+ Label as SelectLabel,
+ Item as SelectItem,
+ Content as SelectContent,
+ Trigger as SelectTrigger,
+ Separator as SelectSeparator,
+ ScrollDownButton as SelectScrollDownButton,
+ ScrollUpButton as SelectScrollUpButton,
+ GroupHeading as SelectGroupHeading
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/select/select-content.svelte b/llama.cpp/tools/ui/src/lib/components/ui/select/select-content.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..b54bc60c2274ce8f3e12c79830934d089a9640ef
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/select/select-content.svelte
@@ -0,0 +1,111 @@
+
+
+
+
+
+
+ {@render children?.()}
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/select/select-group-heading.svelte b/llama.cpp/tools/ui/src/lib/components/ui/select/select-group-heading.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..77c2042c8cd7e7397095f479615c83e3dc5e2b9d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/select/select-group-heading.svelte
@@ -0,0 +1,21 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/select/select-group.svelte b/llama.cpp/tools/ui/src/lib/components/ui/select/select-group.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..2520795d7e70d5e48426b5a36aef42584eed1f07
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/select/select-group.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/select/select-item.svelte b/llama.cpp/tools/ui/src/lib/components/ui/select/select-item.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..02543c1fc314a62bade65cd330ba9f5e3450430e
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/select/select-item.svelte
@@ -0,0 +1,38 @@
+
+
+
+ {#snippet children({ selected, highlighted })}
+
+ {#if selected}
+
+ {/if}
+
+ {#if childrenProp}
+ {@render childrenProp({ selected, highlighted })}
+ {:else}
+ {label || value}
+ {/if}
+ {/snippet}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/select/select-label.svelte b/llama.cpp/tools/ui/src/lib/components/ui/select/select-label.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..e2b830cf11b4ac5906ecd37e3014808b74fce891
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/select/select-label.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/select/select-scroll-down-button.svelte b/llama.cpp/tools/ui/src/lib/components/ui/select/select-scroll-down-button.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..9256dd8b5934ee4ccb5e18ad3fb8cac83a7e04dc
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/select/select-scroll-down-button.svelte
@@ -0,0 +1,20 @@
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/select/select-scroll-up-button.svelte b/llama.cpp/tools/ui/src/lib/components/ui/select/select-scroll-up-button.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..552e52728de8dc7a1625350220ead5bea041e59e
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/select/select-scroll-up-button.svelte
@@ -0,0 +1,20 @@
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/select/select-separator.svelte b/llama.cpp/tools/ui/src/lib/components/ui/select/select-separator.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..7daaa8d09f0de940db196804d5be2af5b024a1e8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/select/select-separator.svelte
@@ -0,0 +1,18 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/select/select-trigger.svelte b/llama.cpp/tools/ui/src/lib/components/ui/select/select-trigger.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..5bc28eeb47bf3d72218b0d40e984db8b366f2fe8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/select/select-trigger.svelte
@@ -0,0 +1,40 @@
+
+
+
+ {@render children?.()}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/separator/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/separator/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..768efac960b9507daa32ac8c3c9394a50a082c88
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/separator/index.ts
@@ -0,0 +1,7 @@
+import Root from './separator.svelte';
+
+export {
+ Root,
+ //
+ Root as Separator
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/separator/separator.svelte b/llama.cpp/tools/ui/src/lib/components/ui/separator/separator.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..00307fdcae720fef6ca3c8a20e5521280e2d2563
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/separator/separator.svelte
@@ -0,0 +1,20 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/sheet/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/sheet/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..139e2d253429f93414c7678ae3cc2f42d676fa98
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/sheet/index.ts
@@ -0,0 +1,36 @@
+import { Dialog as SheetPrimitive } from 'bits-ui';
+import Trigger from './sheet-trigger.svelte';
+import Close from './sheet-close.svelte';
+import Overlay from './sheet-overlay.svelte';
+import Content from './sheet-content.svelte';
+import Header from './sheet-header.svelte';
+import Footer from './sheet-footer.svelte';
+import Title from './sheet-title.svelte';
+import Description from './sheet-description.svelte';
+
+const Root = SheetPrimitive.Root;
+const Portal = SheetPrimitive.Portal;
+
+export {
+ Root,
+ Close,
+ Trigger,
+ Portal,
+ Overlay,
+ Content,
+ Header,
+ Footer,
+ Title,
+ Description,
+ //
+ Root as Sheet,
+ Close as SheetClose,
+ Trigger as SheetTrigger,
+ Portal as SheetPortal,
+ Overlay as SheetOverlay,
+ Content as SheetContent,
+ Header as SheetHeader,
+ Footer as SheetFooter,
+ Title as SheetTitle,
+ Description as SheetDescription
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-close.svelte b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-close.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..b0180c0c194cfa7f47d21bd741ca7237423b1650
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-close.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-content.svelte b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-content.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..b616c469a9188123ee8add5889800338f0d2d407
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-content.svelte
@@ -0,0 +1,61 @@
+
+
+
+
+
+
+
+ {@render children?.()}
+
+
+ Close
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-description.svelte b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-description.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..ef4d58f227f31c6bda2c468deff9400175b00b29
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-description.svelte
@@ -0,0 +1,17 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-footer.svelte b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-footer.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..4e1b927a5c21f065b2397a806c50a1bb02b8b0fd
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-footer.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-header.svelte b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-header.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..6c6c1ec9d4ab3d57d0cb0d007195c6dc71ef56b0
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-header.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-overlay.svelte b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-overlay.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..f402d81aa6f7f0f1f068d19e7d8ec4b750167133
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-overlay.svelte
@@ -0,0 +1,20 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-title.svelte b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-title.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..0efcc7a4fd71c44a43797d3699c2acaaa3519f89
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-title.svelte
@@ -0,0 +1,17 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-trigger.svelte b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-trigger.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..d95719a60ce92753344f9ab9f62411256ed35ac1
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/sheet/sheet-trigger.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/skeleton/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/skeleton/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..3120ce123c98e661627e4ac9aa3cdf9ebe601dc9
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/skeleton/index.ts
@@ -0,0 +1,7 @@
+import Root from './skeleton.svelte';
+
+export {
+ Root,
+ //
+ Root as Skeleton
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/skeleton/skeleton.svelte b/llama.cpp/tools/ui/src/lib/components/ui/skeleton/skeleton.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..62b6f80dfa71bf43e78032dd9984e49ac7985272
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/skeleton/skeleton.svelte
@@ -0,0 +1,17 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/switch/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/switch/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..129f8f5c3b4da7a3941effb5f4937248afe43d85
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/switch/index.ts
@@ -0,0 +1,7 @@
+import Root from './switch.svelte';
+
+export {
+ Root,
+ //
+ Root as Switch
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/switch/switch.svelte b/llama.cpp/tools/ui/src/lib/components/ui/switch/switch.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..e0848790d3c15cd04f8ccecdd53c96a4c74b2431
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/switch/switch.svelte
@@ -0,0 +1,29 @@
+
+
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/table/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/table/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..99239aeead53e63f2777740bd36768243ac5d7b9
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/table/index.ts
@@ -0,0 +1,28 @@
+import Root from './table.svelte';
+import Body from './table-body.svelte';
+import Caption from './table-caption.svelte';
+import Cell from './table-cell.svelte';
+import Footer from './table-footer.svelte';
+import Head from './table-head.svelte';
+import Header from './table-header.svelte';
+import Row from './table-row.svelte';
+
+export {
+ Root,
+ Body,
+ Caption,
+ Cell,
+ Footer,
+ Head,
+ Header,
+ Row,
+ //
+ Root as Table,
+ Body as TableBody,
+ Caption as TableCaption,
+ Cell as TableCell,
+ Footer as TableFooter,
+ Head as TableHead,
+ Header as TableHeader,
+ Row as TableRow
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/table/table-body.svelte b/llama.cpp/tools/ui/src/lib/components/ui/table/table-body.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..f8df65cf689b3f501951cf66f00e27276963c1b4
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/table/table-body.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/table/table-caption.svelte b/llama.cpp/tools/ui/src/lib/components/ui/table/table-caption.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..0fdcc6439c1b32a66090b82801dc2dccc586a9d5
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/table/table-caption.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/table/table-cell.svelte b/llama.cpp/tools/ui/src/lib/components/ui/table/table-cell.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..4506fdfc5bc3ce62d259d935604ef349f2722a05
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/table/table-cell.svelte
@@ -0,0 +1,23 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/table/table-footer.svelte b/llama.cpp/tools/ui/src/lib/components/ui/table/table-footer.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..77e4a64c08b233f680f608d7b957f2774469de4d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/table/table-footer.svelte
@@ -0,0 +1,20 @@
+
+
+tr]:last:border-b-0', className)}
+ {...restProps}
+>
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/table/table-head.svelte b/llama.cpp/tools/ui/src/lib/components/ui/table/table-head.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..c1c57ad44349534f482cae72ac0b808aade7a8c8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/table/table-head.svelte
@@ -0,0 +1,23 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/table/table-header.svelte b/llama.cpp/tools/ui/src/lib/components/ui/table/table-header.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..eb366739b39e3543d920512ed6f5a172e4e6046c
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/table/table-header.svelte
@@ -0,0 +1,20 @@
+
+
+
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/table/table-row.svelte b/llama.cpp/tools/ui/src/lib/components/ui/table/table-row.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..4131d3660a4c78f72728f3f428bcde2144fc1a11
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/table/table-row.svelte
@@ -0,0 +1,23 @@
+
+
+svelte-css-wrapper]:[&>th,td]:bg-muted/50',
+ className
+ )}
+ {...restProps}
+>
+ {@render children?.()}
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/table/table.svelte b/llama.cpp/tools/ui/src/lib/components/ui/table/table.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..c11a6a6c4ba62e7996758eb6bf7db82e3f64f43e
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/table/table.svelte
@@ -0,0 +1,22 @@
+
+
+
+
+ {@render children?.()}
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/textarea/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/textarea/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..9ccb3bff3b0e5ba924779b0a673b0cce0507c94a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/textarea/index.ts
@@ -0,0 +1,7 @@
+import Root from './textarea.svelte';
+
+export {
+ Root,
+ //
+ Root as Textarea
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/textarea/textarea.svelte b/llama.cpp/tools/ui/src/lib/components/ui/textarea/textarea.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..bf838829c02d6fb5c2167ad06d821f1d97e433c3
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/textarea/textarea.svelte
@@ -0,0 +1,22 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/tooltip/index.ts b/llama.cpp/tools/ui/src/lib/components/ui/tooltip/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..273d831e6e8ffc152005d356d7886dcd179b9ead
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/tooltip/index.ts
@@ -0,0 +1,21 @@
+import { Tooltip as TooltipPrimitive } from 'bits-ui';
+import Trigger from './tooltip-trigger.svelte';
+import Content from './tooltip-content.svelte';
+
+const Root = TooltipPrimitive.Root;
+const Provider = TooltipPrimitive.Provider;
+const Portal = TooltipPrimitive.Portal;
+
+export {
+ Root,
+ Trigger,
+ Content,
+ Provider,
+ Portal,
+ //
+ Root as Tooltip,
+ Content as TooltipContent,
+ Trigger as TooltipTrigger,
+ Provider as TooltipProvider,
+ Portal as TooltipPortal
+};
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/tooltip/tooltip-content.svelte b/llama.cpp/tools/ui/src/lib/components/ui/tooltip/tooltip-content.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..5b0c768181a94b60c7b077f29361c56a49ba6841
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/tooltip/tooltip-content.svelte
@@ -0,0 +1,61 @@
+
+
+{#snippet tooltipContent()}
+
+ {@render children?.()}
+
+ {#snippet child({ props })}
+
+ {/snippet}
+
+
+{/snippet}
+
+{#if noPortal}
+ {@render tooltipContent()}
+{:else}
+
+ {@render tooltipContent()}
+
+{/if}
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/tooltip/tooltip-trigger.svelte b/llama.cpp/tools/ui/src/lib/components/ui/tooltip/tooltip-trigger.svelte
new file mode 100644
index 0000000000000000000000000000000000000000..671d6e2201a9269ef0068f450cd84f2005820331
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/tooltip/tooltip-trigger.svelte
@@ -0,0 +1,12 @@
+
+
+
diff --git a/llama.cpp/tools/ui/src/lib/components/ui/utils.ts b/llama.cpp/tools/ui/src/lib/components/ui/utils.ts
new file mode 100644
index 0000000000000000000000000000000000000000..f92bfcbb3f6aaa73e14c2ab1c2648769876f0927
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/components/ui/utils.ts
@@ -0,0 +1,13 @@
+import { clsx, type ClassValue } from 'clsx';
+import { twMerge } from 'tailwind-merge';
+
+export function cn(...inputs: ClassValue[]) {
+ return twMerge(clsx(inputs));
+}
+
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+export type WithoutChild = T extends { child?: any } ? Omit : T;
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+export type WithoutChildren = T extends { children?: any } ? Omit : T;
+export type WithoutChildrenOrChild = WithoutChildren>;
+export type WithElementRef = T & { ref?: U | null };
diff --git a/llama.cpp/tools/ui/src/lib/constants/agentic.ts b/llama.cpp/tools/ui/src/lib/constants/agentic.ts
new file mode 100644
index 0000000000000000000000000000000000000000..c0575163efca9644b188d5dec31a340fa0fc4381
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/agentic.ts
@@ -0,0 +1,52 @@
+import type { AgenticConfig } from '$lib/types/agentic';
+
+export const ATTACHMENT_SAVED_REGEX = /\[Attachment saved: ([^\]]+)\]/;
+
+export const NEWLINE_SEPARATOR = '\n';
+
+export const DEFAULT_AGENTIC_CONFIG: AgenticConfig = {
+ enabled: true,
+ maxTurns: 100,
+ maxToolPreviewLines: 25
+} as const;
+
+export const REASONING_TAGS = {
+ START: '',
+ END: ' '
+} as const;
+
+/**
+ * @deprecated Legacy marker tags - only used for migration of old stored messages.
+ * New messages use structured fields (reasoningContent, toolCalls, toolCallId).
+ */
+export const LEGACY_AGENTIC_TAGS = {
+ TOOL_CALL_START: '<<>>',
+ TOOL_CALL_END: '<<>>',
+ TOOL_NAME_PREFIX: '<<>>',
+ TOOL_ARGS_END: '<<>>',
+ TAG_SUFFIX: '>>>'
+} as const;
+
+/**
+ * @deprecated Legacy reasoning tags - only used for migration of old stored messages.
+ * New messages use the dedicated reasoningContent field.
+ */
+export const LEGACY_REASONING_TAGS = {
+ START: '<<>>',
+ END: '<<>>'
+} as const;
+
+/**
+ * @deprecated Legacy regex patterns - only used for migration of old stored messages.
+ */
+export const LEGACY_AGENTIC_REGEX = {
+ COMPLETED_TOOL_CALL:
+ /<<>>\n<<>>\n<<>>([\s\S]*?)<<>>([\s\S]*?)<<>>/g,
+ REASONING_BLOCK: /<<>>[\s\S]*?<<>>/g,
+ REASONING_EXTRACT: /<<>>([\s\S]*?)<<>>/,
+ REASONING_OPEN: /<<>>[\s\S]*$/,
+ AGENTIC_TOOL_CALL_BLOCK: /\n*<<>>[\s\S]*?<<>>/g,
+ AGENTIC_TOOL_CALL_OPEN: /\n*<<>>[\s\S]*$/,
+ HAS_LEGACY_MARKERS: /<<<(?:AGENTIC_TOOL_CALL_START|reasoning_content_start)>>>/
+} as const;
diff --git a/llama.cpp/tools/ui/src/lib/constants/api-endpoints.ts b/llama.cpp/tools/ui/src/lib/constants/api-endpoints.ts
new file mode 100644
index 0000000000000000000000000000000000000000..37137c1c769900c933fed4d7df8cd76c0f3faad4
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/api-endpoints.ts
@@ -0,0 +1,31 @@
+export const API_MODELS = {
+ LIST: '/v1/models',
+ LOAD: '/models/load',
+ UNLOAD: '/models/unload',
+ SSE: '/models/sse'
+};
+
+// chat completion routes, the control route drives realtime inference (e.g. end reasoning)
+export const API_CHAT = {
+ COMPLETIONS: './v1/chat/completions',
+ CONTROL: './v1/chat/completions/control'
+};
+
+// slot introspection, requires the --slots flag on the server
+export const API_SLOTS = {
+ LIST: './slots'
+};
+
+export const API_TOOLS = {
+ LIST: '/tools',
+ EXECUTE: '/tools'
+};
+
+// resumable stream routes, the conv::model identity is appended as a path segment
+export const API_STREAM = {
+ BASE: './v1/stream',
+ LOOKUP: './v1/streams/lookup'
+};
+
+/** CORS proxy endpoint path */
+export const CORS_PROXY_ENDPOINT = '/cors-proxy';
diff --git a/llama.cpp/tools/ui/src/lib/constants/app.ts b/llama.cpp/tools/ui/src/lib/constants/app.ts
new file mode 100644
index 0000000000000000000000000000000000000000..c598f480fd4dfc3decfab10f4cf86bf8be371764
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/app.ts
@@ -0,0 +1 @@
+export const APP_NAME = import.meta.env?.VITE_PUBLIC_APP_NAME || 'llama-ui';
diff --git a/llama.cpp/tools/ui/src/lib/constants/attachment-labels.ts b/llama.cpp/tools/ui/src/lib/constants/attachment-labels.ts
new file mode 100644
index 0000000000000000000000000000000000000000..be9999c0f9eed66849bef18aa613e9414edafea5
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/attachment-labels.ts
@@ -0,0 +1,4 @@
+export const ATTACHMENT_LABEL_FILE = 'File';
+export const ATTACHMENT_LABEL_PDF_FILE = 'PDF File';
+export const ATTACHMENT_LABEL_MCP_PROMPT = 'MCP Prompt';
+export const ATTACHMENT_LABEL_MCP_RESOURCE = 'MCP Resource';
diff --git a/llama.cpp/tools/ui/src/lib/constants/attachment-menu.ts b/llama.cpp/tools/ui/src/lib/constants/attachment-menu.ts
new file mode 100644
index 0000000000000000000000000000000000000000..3d7381812e5d04bce2c799949350f0f8aca068c3
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/attachment-menu.ts
@@ -0,0 +1,114 @@
+import type { Component } from 'svelte';
+import { MessageSquare, Zap, FolderOpen } from '@lucide/svelte';
+import { FILE_TYPE_ICONS } from '$lib/constants/icons';
+import {
+ AttachmentAction,
+ AttachmentItemEnabledWhen,
+ AttachmentItemVisibleWhen,
+ AttachmentMenuItemId
+} from '$lib/enums';
+
+export interface AttachmentMenuItem {
+ /** Unique identifier for the item */
+ id: AttachmentMenuItemId;
+ /** Display label */
+ label: string;
+ /** Lucide icon component */
+ icon: Component;
+ /** Extra CSS class applied to the item (e.g. for test selectors) */
+ class?: string;
+ /** Whether the item requires a specific modality to be enabled */
+ enabledWhen?: AttachmentItemEnabledWhen;
+ /** Tooltip shown when the item is disabled */
+ disabledTooltip?: string;
+ /** Callback key on the Props interface to invoke when clicked */
+ action: AttachmentAction;
+ /** Whether the item is only shown when a specific capability is present */
+ visibleWhen?: AttachmentItemVisibleWhen;
+ /** Whether this item has a tooltip even when enabled (uses dynamic text) */
+ hasEnabledTooltip?: boolean;
+}
+
+/**
+ * File attachment menu items shown in both the desktop dropdown and mobile sheet.
+ * The "Tools" submenu is handled separately by each component.
+ */
+export const ATTACHMENT_FILE_ITEMS: AttachmentMenuItem[] = [
+ {
+ id: AttachmentMenuItemId.IMAGES,
+ label: 'Images',
+ icon: FILE_TYPE_ICONS.image,
+ class: 'images-button',
+ enabledWhen: AttachmentItemEnabledWhen.HAS_VISION_MODALITY,
+ disabledTooltip: 'Image processing requires a vision model',
+ action: AttachmentAction.FILE_UPLOAD
+ },
+ {
+ id: AttachmentMenuItemId.AUDIO,
+ label: 'Audio Files',
+ icon: FILE_TYPE_ICONS.audio,
+ class: 'audio-button',
+ enabledWhen: AttachmentItemEnabledWhen.HAS_AUDIO_MODALITY,
+ disabledTooltip: 'Audio files processing requires an audio model',
+ action: AttachmentAction.FILE_UPLOAD
+ },
+ {
+ id: AttachmentMenuItemId.VIDEO,
+ label: 'Video Files',
+ icon: FILE_TYPE_ICONS.video,
+ class: 'video-button',
+ enabledWhen: AttachmentItemEnabledWhen.HAS_VIDEO_MODALITY,
+ disabledTooltip: 'Video files processing requires a video model',
+ action: AttachmentAction.FILE_UPLOAD
+ },
+ {
+ id: AttachmentMenuItemId.TEXT,
+ label: 'Text Files',
+ icon: FILE_TYPE_ICONS.text,
+ enabledWhen: AttachmentItemEnabledWhen.ALWAYS,
+ action: AttachmentAction.FILE_UPLOAD
+ },
+ {
+ id: AttachmentMenuItemId.PDF,
+ label: 'PDF Files',
+ icon: FILE_TYPE_ICONS.pdf,
+ enabledWhen: AttachmentItemEnabledWhen.ALWAYS,
+ disabledTooltip: 'PDFs will be converted to text. Image-based PDFs may not work properly.',
+ hasEnabledTooltip: true,
+ action: AttachmentAction.FILE_UPLOAD
+ }
+];
+
+export const ATTACHMENT_EXTRA_ITEMS: AttachmentMenuItem[] = [];
+
+export const ATTACHMENT_PROMPT_ITEMS: AttachmentMenuItem[] = [
+ {
+ id: AttachmentMenuItemId.SYSTEM_MESSAGE,
+ label: 'System Message',
+ icon: MessageSquare,
+ enabledWhen: AttachmentItemEnabledWhen.ALWAYS,
+ hasEnabledTooltip: true,
+ action: AttachmentAction.SYSTEM_PROMPT_CLICK
+ },
+ {
+ id: AttachmentMenuItemId.MCP_PROMPT,
+ label: 'MCP Prompt',
+ icon: Zap,
+ enabledWhen: AttachmentItemEnabledWhen.ALWAYS,
+ action: AttachmentAction.MCP_PROMPT_CLICK,
+ visibleWhen: AttachmentItemVisibleWhen.HAS_MCP_PROMPTS_SUPPORT
+ }
+];
+
+export const ATTACHMENT_MCP_ITEMS: AttachmentMenuItem[] = [
+ {
+ id: AttachmentMenuItemId.MCP_RESOURCES,
+ label: 'MCP Resources',
+ icon: FolderOpen,
+ enabledWhen: AttachmentItemEnabledWhen.ALWAYS,
+ action: AttachmentAction.MCP_RESOURCES_CLICK,
+ visibleWhen: AttachmentItemVisibleWhen.HAS_MCP_RESOURCES_SUPPORT
+ }
+];
+
+export const ATTACHMENT_TOOLTIP_TEXT = 'Add files, prompts, tools or MCP Servers';
diff --git a/llama.cpp/tools/ui/src/lib/constants/auto-scroll.ts b/llama.cpp/tools/ui/src/lib/constants/auto-scroll.ts
new file mode 100644
index 0000000000000000000000000000000000000000..ca9ba5a9e832697ba1d8172eca8e8dd53dea5e6b
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/auto-scroll.ts
@@ -0,0 +1,2 @@
+export const AUTO_SCROLL_INTERVAL = 100;
+export const AUTO_SCROLL_AT_BOTTOM_THRESHOLD = 10;
diff --git a/llama.cpp/tools/ui/src/lib/constants/binary-detection.ts b/llama.cpp/tools/ui/src/lib/constants/binary-detection.ts
new file mode 100644
index 0000000000000000000000000000000000000000..21a95cc88371759785c856564226d4ce0daf32f8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/binary-detection.ts
@@ -0,0 +1,7 @@
+import type { BinaryDetectionOptions } from '$lib/types';
+
+export const DEFAULT_BINARY_DETECTION_OPTIONS: BinaryDetectionOptions = {
+ prefixLength: 1024 * 10, // Check the first 10KB of the string
+ suspiciousCharThresholdRatio: 0.15, // Allow up to 15% suspicious chars
+ maxAbsoluteNullBytes: 2
+};
diff --git a/llama.cpp/tools/ui/src/lib/constants/cache.ts b/llama.cpp/tools/ui/src/lib/constants/cache.ts
new file mode 100644
index 0000000000000000000000000000000000000000..07fe8683414a7fa165c9d0a22cca511e36cf2c44
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/cache.ts
@@ -0,0 +1,54 @@
+/**
+ * Cache configuration constants
+ */
+
+/**
+ * Default TTL (Time-To-Live) for cache entries in milliseconds
+ * @default 5 minutes
+ */
+export const DEFAULT_CACHE_TTL_MS = 5 * 60 * 1000;
+
+/**
+ * Default maximum number of entries in a cache
+ * @default 100
+ */
+export const DEFAULT_CACHE_MAX_ENTRIES = 100;
+
+/**
+ * TTL for model props cache in milliseconds
+ * Props don't change frequently, so we can cache them longer
+ * @default 10 minutes
+ */
+export const MODEL_PROPS_CACHE_TTL_MS = 10 * 60 * 1000;
+
+/**
+ * Maximum number of model props to cache
+ * @default 50
+ */
+export const MODEL_PROPS_CACHE_MAX_ENTRIES = 50;
+
+/**
+ * Maximum number of MCP resources to cache
+ * @default 50
+ */
+export const MCP_RESOURCE_CACHE_MAX_ENTRIES = 50;
+
+/**
+ * TTL for MCP resource cache entries in milliseconds
+ * @default 5 minutes
+ */
+export const MCP_RESOURCE_CACHE_TTL_MS = 5 * 60 * 1000;
+
+/**
+ * Maximum number of inactive conversation states to keep in memory
+ * States for conversations beyond this limit will be cleaned up
+ * @default 10
+ */
+export const MAX_INACTIVE_CONVERSATION_STATES = 10;
+
+/**
+ * Maximum age (in ms) for inactive conversation states before cleanup
+ * States older than this will be removed during cleanup
+ * @default 30 minutes
+ */
+export const INACTIVE_CONVERSATION_STATE_MAX_AGE_MS = 30 * 60 * 1000;
diff --git a/llama.cpp/tools/ui/src/lib/constants/chat-form.ts b/llama.cpp/tools/ui/src/lib/constants/chat-form.ts
new file mode 100644
index 0000000000000000000000000000000000000000..05ab8c1f821236c715f2eca6faed116d757a724d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/chat-form.ts
@@ -0,0 +1,6 @@
+export const INITIAL_FILE_SIZE = 0;
+export const PROMPT_CONTENT_SEPARATOR = '\n\n';
+export const CLIPBOARD_CONTENT_QUOTE_PREFIX = '"';
+export const PROMPT_TRIGGER_PREFIX = '/';
+export const RESOURCE_TRIGGER_PREFIX = '@';
+export const NEW_CHAT_DRAFT_KEY = '__new_chat__';
diff --git a/llama.cpp/tools/ui/src/lib/constants/cli-flags.ts b/llama.cpp/tools/ui/src/lib/constants/cli-flags.ts
new file mode 100644
index 0000000000000000000000000000000000000000..4fbee8a36973da18229ddc38e607b617f7024c46
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/cli-flags.ts
@@ -0,0 +1,6 @@
+export const CLI_FLAGS = {
+ API_KEY: '--api-key',
+ MCP_PROXY: '--ui-mcp-proxy',
+ SLOTS: '--slots',
+ TOOLS: '--tools'
+} as const;
diff --git a/llama.cpp/tools/ui/src/lib/constants/code-blocks.ts b/llama.cpp/tools/ui/src/lib/constants/code-blocks.ts
new file mode 100644
index 0000000000000000000000000000000000000000..0f7265104d9b1f96cbaf164b28ff89a250f6eb88
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/code-blocks.ts
@@ -0,0 +1,8 @@
+export const CODE_BLOCK_SCROLL_CONTAINER_CLASS = 'code-block-scroll-container';
+export const CODE_BLOCK_WRAPPER_CLASS = 'code-block-wrapper';
+export const CODE_BLOCK_HEADER_CLASS = 'code-block-header';
+export const CODE_BLOCK_ACTIONS_CLASS = 'code-block-actions';
+export const CODE_LANGUAGE_CLASS = 'code-language';
+export const COPY_CODE_BTN_CLASS = 'copy-code-btn';
+export const PREVIEW_CODE_BTN_CLASS = 'preview-code-btn';
+export const RELATIVE_CLASS = 'relative';
diff --git a/llama.cpp/tools/ui/src/lib/constants/code.ts b/llama.cpp/tools/ui/src/lib/constants/code.ts
new file mode 100644
index 0000000000000000000000000000000000000000..b55513d7420d1fc756268a2ac6a3f80c59f6f1d8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/code.ts
@@ -0,0 +1,8 @@
+export const NEWLINE = '\n';
+export const TAB = '\t';
+export const DEFAULT_LANGUAGE = 'text';
+export const LANG_PATTERN = /^(\w*)\n?/;
+export const AMPERSAND_REGEX = /&/g;
+export const LT_REGEX = //g;
+export const FENCE_PATTERN = /^```|\n```/g;
diff --git a/llama.cpp/tools/ui/src/lib/constants/context-keys.ts b/llama.cpp/tools/ui/src/lib/constants/context-keys.ts
new file mode 100644
index 0000000000000000000000000000000000000000..12de0d0bcb9aab5a3d720702ccbe458824f90185
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/context-keys.ts
@@ -0,0 +1,4 @@
+export const CONTEXT_KEY_MESSAGE_EDIT = 'chat-message-edit';
+export const CONTEXT_KEY_CHAT_ACTIONS = 'chat-actions';
+export const CONTEXT_KEY_CHAT_SETTINGS_CONFIG = 'chat-settings-config';
+export const CONTEXT_KEY_PROCESSING_INFO = 'processing-info';
diff --git a/llama.cpp/tools/ui/src/lib/constants/control-actions.ts b/llama.cpp/tools/ui/src/lib/constants/control-actions.ts
new file mode 100644
index 0000000000000000000000000000000000000000..935ae9542a360193888d7825b69d5c897ac4a020
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/control-actions.ts
@@ -0,0 +1,7 @@
+// actions accepted by the realtime inference control endpoint (API_CHAT.CONTROL)
+// kept separate from the endpoint paths since these are protocol level verbs
+export const CONTROL_ACTION = {
+ END_REASONING: 'reasoning_end'
+} as const;
+
+export type ControlAction = (typeof CONTROL_ACTION)[keyof typeof CONTROL_ACTION];
diff --git a/llama.cpp/tools/ui/src/lib/constants/css-classes.ts b/llama.cpp/tools/ui/src/lib/constants/css-classes.ts
new file mode 100644
index 0000000000000000000000000000000000000000..ca5386fcdf04251bba6918f7d28a837890f0f712
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/css-classes.ts
@@ -0,0 +1,19 @@
+export const BOX_BORDER =
+ 'border border-border/30 focus-within:border-border dark:border-border/20 dark:focus-within:border-border';
+
+export const INPUT_CLASSES = `
+ bg-muted/60 dark:bg-muted/75
+ ${BOX_BORDER}
+ shadow-sm
+ outline-none
+ text-foreground
+`;
+
+export const PANEL_CLASSES = `
+ bg-background
+ border border-border/30 dark:border-border/20
+ shadow-sm backdrop-blur-lg!
+ rounded-t-lg!
+`;
+
+export const CHAT_FORM_POPOVER_MAX_HEIGHT = 'max-h-80';
diff --git a/llama.cpp/tools/ui/src/lib/constants/database.ts b/llama.cpp/tools/ui/src/lib/constants/database.ts
new file mode 100644
index 0000000000000000000000000000000000000000..95e698f40012312d51a145fe4950aa51ddc19e25
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/database.ts
@@ -0,0 +1,29 @@
+/**
+ * Database-related constants (IndexedDB, Dexie).
+ *
+ * Centralized to ensure consistency across the app and simplify future
+ * naming changes.
+ */
+
+import { STORAGE_APP_NAME } from './storage';
+
+/** IndexedDB database name */
+export const DB_NAME = STORAGE_APP_NAME;
+
+/** IndexedDB store / table names */
+export const IDXDB_TABLES = {
+ conversations: 'conversations',
+ messages: 'messages'
+} as const;
+
+/** IndexedDB store schemas */
+export const IDXDB_STORE_SCHEMAS = {
+ conversations: 'id, lastModified, currNode, name',
+ messages: 'id, convId, type, role, timestamp, parent, children'
+} as const;
+
+/** Combined Dexie stores definition — keys are table names, values are schemas */
+export const IDXDB_STORES = {
+ [IDXDB_TABLES.conversations]: IDXDB_STORE_SCHEMAS.conversations,
+ [IDXDB_TABLES.messages]: IDXDB_STORE_SCHEMAS.messages
+} as const;
diff --git a/llama.cpp/tools/ui/src/lib/constants/diagram-blocks.ts b/llama.cpp/tools/ui/src/lib/constants/diagram-blocks.ts
new file mode 100644
index 0000000000000000000000000000000000000000..caeb6b5b33848a3d806fe8ad30375b3dc187e112
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/diagram-blocks.ts
@@ -0,0 +1,9 @@
+// Shared constants for diagram blocks (mermaid and svg) that toggle between a
+// rendered view and a source view. The wrapper carries the active mode, css
+// drives the visibility, the click handler only flips the attribute.
+
+export const DIAGRAM_VIEW_MODE_ATTR = 'data-view-mode';
+export const DIAGRAM_VIEW_RENDERED = 'rendered';
+export const DIAGRAM_VIEW_SOURCE = 'source';
+export const DIAGRAM_SOURCE_CLASS = 'diagram-source';
+export const TOGGLE_SOURCE_BTN_CLASS = 'toggle-source-btn';
diff --git a/llama.cpp/tools/ui/src/lib/constants/error.ts b/llama.cpp/tools/ui/src/lib/constants/error.ts
new file mode 100644
index 0000000000000000000000000000000000000000..4339bd25d55e2455c12c1e4248314c96d4b2b4bd
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/error.ts
@@ -0,0 +1,23 @@
+export const ERROR_MESSAGES = {
+ NETWORK: {
+ GENERIC: 'Failed to connect to server',
+ NXDOMAIN: 'Server not found - check server address',
+ REFUSED: 'Connection refused - server may be offline',
+ TIMEOUT: 'Request timed out',
+ UNREACHABLE: 'Server is not running or unreachable'
+ },
+ HTTP: {
+ GENERIC: 'Request failed',
+ ACCESS_DENIED: 'Access denied',
+ INTERNAL_ERROR: 'Server error - check server logs',
+ NOT_FOUND: 'Not found',
+ TEMPORARILY_UNAVAILABLE: 'Server temporarily unavailable'
+ }
+};
+
+export const HTTP_CODE_TO_STRING: Record = {
+ 401: ERROR_MESSAGES.HTTP.ACCESS_DENIED,
+ 403: ERROR_MESSAGES.HTTP.ACCESS_DENIED,
+ 500: ERROR_MESSAGES.HTTP.INTERNAL_ERROR,
+ 503: ERROR_MESSAGES.HTTP.TEMPORARILY_UNAVAILABLE
+};
diff --git a/llama.cpp/tools/ui/src/lib/constants/floating-ui-constraints.ts b/llama.cpp/tools/ui/src/lib/constants/floating-ui-constraints.ts
new file mode 100644
index 0000000000000000000000000000000000000000..003fc77acb08eac8b373bcf1243139f8cf911117
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/floating-ui-constraints.ts
@@ -0,0 +1,2 @@
+export const VIEWPORT_GUTTER = 8;
+export const MENU_OFFSET = 6;
diff --git a/llama.cpp/tools/ui/src/lib/constants/formatters.ts b/llama.cpp/tools/ui/src/lib/constants/formatters.ts
new file mode 100644
index 0000000000000000000000000000000000000000..c417faea43d93b5771895b53fde98fb7678354b3
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/formatters.ts
@@ -0,0 +1,35 @@
+export const MS_PER_SECOND = 1000;
+export const SECONDS_PER_MINUTE = 60;
+export const SECONDS_PER_HOUR = 3600;
+export const SHORT_DURATION_THRESHOLD = 1;
+export const MEDIUM_DURATION_THRESHOLD = 10;
+
+/** Default display value when no performance time is available */
+export const DEFAULT_PERFORMANCE_TIME = '0s';
+
+/** Max length before reasoning preview is truncated */
+export const MAX_PREVIEW_LENGTH = 120;
+
+export const STRIP_MARKDOWN_CAPTURE_PATTERNS: [RegExp, string][] = [
+ [/^```(.*)/gm, '$1'],
+ [/(.*)```$/gm, '$1'],
+ [/`([^`]*)`/g, '$1'],
+ [/\*\*(.*?)\*\*/g, '$1'],
+ [/__(.*?)__/g, '$1'],
+ [/\*(.*?)\*/g, '$1'],
+ [/_(.*?)_/g, '$1']
+];
+
+/* eslint-disable no-misleading-character-class */
+export const STRIP_MARKDOWN_INLINE_REGEX = new RegExp(
+ [
+ '<[^>]*>',
+ '^>\\s*',
+ '^#{1,6}\\s+',
+ '^[\\s]*[-*+]\\s+',
+ '^[\\s]*\\d+[.)]\\s+',
+ '[\\u{1F600}-\\u{1F64F}\\u{1F300}-\\u{1F5FF}\\u{1F680}-\\u{1F6FF}\\u{1F1E0}-\\u{1F1FF}\\u{2600}-\\u{26FF}\\u{2700}-\\u{27BF}\\u{FE00}-\\u{FE0F}\\u{1F900}-\\u{1F9FF}\\u{1FA00}-\\u{1FA6F}\\u{1FA70}-\\u{1FAFF}\\u{200D}\\u{20E3}\\u{231A}-\\u{231B}\\u{23E9}-\\u{23F3}\\u{23F8}-\\u{23FA}\\u{25AA}-\\u{25AB}\\u{25B6}\\u{25C0}\\u{25FB}-\\u{25FE}\\u{2934}-\\u{2935}\\u{2B05}-\\u{2B07}\\u{2B1B}-\\u{2B1C}\\u{2B50}\\u{2B55}\\u{3030}\\u{303D}\\u{3297}\\u{3299}]'
+ ].join('|'),
+ 'gmu'
+);
+/* eslint-enable no-misleading-character-class */
diff --git a/llama.cpp/tools/ui/src/lib/constants/icons.ts b/llama.cpp/tools/ui/src/lib/constants/icons.ts
new file mode 100644
index 0000000000000000000000000000000000000000..6ef02c4cb7cfd707f80ceddadce91d8d3b3eb623
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/icons.ts
@@ -0,0 +1,43 @@
+/**
+ * Icon mappings for file types and model modalities
+ * Centralized configuration to ensure consistent icon usage across the app
+ */
+
+import {
+ File as FileIcon,
+ FileText as FileTextIcon,
+ Image as ImageIcon,
+ Eye as VisionIcon,
+ Mic as AudioIcon,
+ Video as VideoIcon
+} from '@lucide/svelte';
+import { FileTypeCategory, ModelModality } from '$lib/enums';
+
+export const FILE_TYPE_ICONS = {
+ [FileTypeCategory.IMAGE]: ImageIcon,
+ [FileTypeCategory.AUDIO]: AudioIcon,
+ [FileTypeCategory.VIDEO]: VideoIcon,
+ [FileTypeCategory.TEXT]: FileTextIcon,
+ [FileTypeCategory.PDF]: FileIcon
+} as const;
+
+export const DEFAULT_FILE_ICON = FileIcon;
+
+export const MODALITY_ICONS = {
+ [ModelModality.VISION]: VisionIcon,
+ [ModelModality.AUDIO]: AudioIcon,
+ [ModelModality.VIDEO]: VideoIcon
+} as const;
+
+export const MODALITY_LABELS = {
+ [ModelModality.VISION]: 'Vision',
+ [ModelModality.AUDIO]: 'Audio',
+ [ModelModality.VIDEO]: 'Video'
+} as const;
+
+// Shared SVG icon strings for copy and preview buttons
+export const COPY_ICON_SVG = ` `;
+
+export const PREVIEW_ICON_SVG = ` `;
+
+export const CODE_ICON_SVG = ` `;
diff --git a/llama.cpp/tools/ui/src/lib/constants/image-size.ts b/llama.cpp/tools/ui/src/lib/constants/image-size.ts
new file mode 100644
index 0000000000000000000000000000000000000000..8a7f921fa019a0cec1a6b1e28b7fc30a3ecd5ec9
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/image-size.ts
@@ -0,0 +1,3 @@
+export const MEGAPIXELS_TO_PIXELS = 1_000_000;
+
+export const HEIC_JPEG_QUALITY = 0.85;
diff --git a/llama.cpp/tools/ui/src/lib/constants/index.ts b/llama.cpp/tools/ui/src/lib/constants/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..643864ad41521e8b1d823ae54a2cd8d9185711ca
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/index.ts
@@ -0,0 +1,59 @@
+// Central constants export file
+// All constants should be imported from '$lib/constants'
+
+export * from './agentic';
+export * from './api-endpoints';
+export * from './app';
+export * from './attachment-labels';
+export * from './database';
+export * from './reasoning-effort';
+export * from './reasoning-effort-tokens';
+export * from './recommended-mcp-servers';
+export * from './storage';
+export * from './attachment-menu';
+export * from './auto-scroll';
+export * from './binary-detection';
+export * from './cache';
+export * from './chat-form';
+export * from './cli-flags';
+export * from './code-blocks';
+export * from './icons';
+export * from './code';
+export * from './context-keys';
+export * from './control-actions';
+export * from './css-classes';
+export * from './floating-ui-constraints';
+export * from './formatters';
+export * from './key-value-pairs';
+export * from './icons';
+export * from './latex-protection';
+export * from './literal-html';
+export * from './markdown';
+export * from './mermaid-blocks';
+export * from './svg-blocks';
+export * from './diagram-blocks';
+export * from './max-bundle-size';
+export * from './mcp';
+export * from './mcp-form';
+export * from './mcp-resource';
+export * from './message-export';
+export * from './model-id';
+export * from './model-loading';
+export * from './sse';
+export * from './precision';
+export * from './processing-info';
+export * from './pwa';
+export * from './routes';
+export * from './sandbox';
+export * from './settings-keys';
+export * from './settings-registry';
+export * from './stream';
+export * from './supported-file-types';
+export * from './table-html-restorer';
+export * from './title-generation';
+export * from './tools';
+export * from './tooltip-config';
+export * from './ui';
+export * from './uri-template';
+export * from './url';
+export * from './viewport';
diff --git a/llama.cpp/tools/ui/src/lib/constants/jpeg-exif.ts b/llama.cpp/tools/ui/src/lib/constants/jpeg-exif.ts
new file mode 100644
index 0000000000000000000000000000000000000000..5b2591b04b1850c8f4784caf109b2865da800984
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/jpeg-exif.ts
@@ -0,0 +1,30 @@
+/**
+ * JPEG and EXIF binary format constants for orientation parsing.
+ */
+
+/** Bytes of file prefix to scan, the APP1 EXIF segment sits near the start */
+export const EXIF_SCAN_BYTE_LIMIT = 128 * 1024;
+
+/** JPEG start of image marker */
+export const JPEG_SOI_MARKER = 0xffd8;
+
+/** APP1 segment marker byte, carries the EXIF payload */
+export const APP1_MARKER = 0xe1;
+
+/** Start of scan marker byte, compressed data begins and no EXIF follows */
+export const SOS_MARKER = 0xda;
+
+/** "Exif" signature opening the APP1 payload, big endian uint32 */
+export const EXIF_SIGNATURE = 0x45786966;
+
+/** TIFF byte order mark for little endian ("II") */
+export const TIFF_LITTLE_ENDIAN = 0x4949;
+
+/** TIFF magic number following the byte order mark */
+export const TIFF_MAGIC = 42;
+
+/** EXIF tag id holding the orientation value */
+export const EXIF_ORIENTATION_TAG = 0x0112;
+
+/** Size in bytes of one IFD directory entry */
+export const IFD_ENTRY_SIZE = 12;
diff --git a/llama.cpp/tools/ui/src/lib/constants/key-value-pairs.ts b/llama.cpp/tools/ui/src/lib/constants/key-value-pairs.ts
new file mode 100644
index 0000000000000000000000000000000000000000..48dadbec43110a914b05111204d2172011a4e10a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/key-value-pairs.ts
@@ -0,0 +1,20 @@
+/**
+ * Key-value pair form constraints and sanitization patterns.
+ *
+ * Both regexes target characters dangerous in HTTP-header / env-var contexts:
+ * \x00 – null byte (injection)
+ * \x0A (\n) – LF (HTTP header injection / response splitting)
+ * \x0D (\r) – CR (HTTP header injection / response splitting)
+ * \x01–\x08, \x0B–\x0C, \x0E–\x1F, \x7F – other C0/DEL control chars
+ *
+ * KEY_UNSAFE_RE additionally strips TAB (\x09); values keep TAB because it is
+ * a valid header-value continuation character per RFC 7230.
+ */
+
+export const KEY_VALUE_PAIR_KEY_MAX_LENGTH = 256;
+export const KEY_VALUE_PAIR_VALUE_MAX_LENGTH = 8192;
+
+// eslint-disable-next-line no-control-regex
+export const KEY_VALUE_PAIR_UNSAFE_KEY_RE = /[\x00-\x1F\x7F]/g;
+// eslint-disable-next-line no-control-regex
+export const KEY_VALUE_PAIR_UNSAFE_VALUE_RE = /[\x00-\x08\x0A-\x0D\x0E-\x1F\x7F]/g;
diff --git a/llama.cpp/tools/ui/src/lib/constants/latex-protection.ts b/llama.cpp/tools/ui/src/lib/constants/latex-protection.ts
new file mode 100644
index 0000000000000000000000000000000000000000..27c88e7252a633be1ed28f22a63c20a3bdfb43d7
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/latex-protection.ts
@@ -0,0 +1,35 @@
+/**
+ * Matches common Markdown code blocks to exclude them from further processing (e.g. LaTeX).
+ * - Fenced: ```...```
+ * - Inline: `...` (does NOT support nested backticks or multi-backtick syntax)
+ *
+ * Note: This pattern does not handle advanced cases like:
+ * `` `code with `backticks` `` or \\``...\\``
+ */
+export const CODE_BLOCK_REGEXP = /(```[\s\S]*?```|`[^`\n]+`)/g;
+
+/**
+ * Matches LaTeX math delimiters \(...\) and \[...\] only when not preceded by a backslash (i.e., not escaped),
+ * while also capturing code blocks (```, `...`) so they can be skipped during processing.
+ *
+ * Uses negative lookbehind `(? = {
+ [MimeTypeImage.JPEG]: 'jpg',
+ [MimeTypeImage.JPG]: 'jpg',
+ [MimeTypeImage.PNG]: 'png',
+ [MimeTypeImage.GIF]: 'gif',
+ [MimeTypeImage.WEBP]: 'webp'
+} as const;
diff --git a/llama.cpp/tools/ui/src/lib/constants/mcp.ts b/llama.cpp/tools/ui/src/lib/constants/mcp.ts
new file mode 100644
index 0000000000000000000000000000000000000000..a7381df0bff13b7ce0a14377b7e4a55d0f8058fb
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/mcp.ts
@@ -0,0 +1,94 @@
+import { Zap, Globe, Radio } from '@lucide/svelte';
+import { MCPTransportType } from '$lib/enums';
+import type { ClientCapabilities, Implementation } from '$lib/types';
+import type { Component } from 'svelte';
+import { MimeTypeImage } from '$lib/enums/files.enums';
+
+export const DEFAULT_CLIENT_VERSION = '1.0.0';
+export const MCP_CLIENT_NAME = 'llama-ui-mcp';
+export const DEFAULT_IMAGE_MIME_TYPE = MimeTypeImage.PNG;
+
+/** MIME types considered safe for rendering MCP server icons */
+export const MCP_ALLOWED_ICON_MIME_TYPES = new Set([
+ MimeTypeImage.PNG,
+ MimeTypeImage.JPEG,
+ MimeTypeImage.JPG,
+ MimeTypeImage.SVG,
+ MimeTypeImage.WEBP,
+ MimeTypeImage.ICO,
+ MimeTypeImage.ICO_MICROSOFT
+]);
+
+/**
+ * MCP specification version this client targets.
+ * Update when the upstream MCP spec introduces a new stable version:
+ * https://spec.modelcontextprotocol.io/
+ */
+export const MCP_PROTOCOL_VERSION = '2025-06-18';
+
+export const DEFAULT_MCP_CONFIG = {
+ protocolVersion: MCP_PROTOCOL_VERSION,
+ capabilities: { tools: { listChanged: true } } as ClientCapabilities,
+ clientInfo: { name: MCP_CLIENT_NAME, version: DEFAULT_CLIENT_VERSION } as Implementation,
+ requestTimeoutSeconds: 300, // 5 minutes for long-running tools
+ connectionTimeoutMs: 10_000 // 10 seconds for connection establishment
+} as const;
+
+export const MCP_SERVER_ID_PREFIX = 'LlamaUI-MCP-Server';
+
+export const MCP_RECONNECT_INITIAL_DELAY = 1000;
+export const MCP_RECONNECT_BACKOFF_MULTIPLIER = 2;
+export const MCP_RECONNECT_MAX_DELAY = 30000;
+/** Per-attempt timeout for a single reconnection attempt before giving up and backing off. */
+export const MCP_RECONNECT_ATTEMPT_TIMEOUT_MS = 15_000;
+
+/** Maximum number of MCP server avatars to display in the chat form */
+export const MAX_DISPLAYED_MCP_AVATARS = 4;
+
+/** Expected count when two theme-less icons represent a light/dark pair */
+export const EXPECTED_THEMED_ICON_PAIR_COUNT = 2;
+
+/** CORS proxy URL query parameter name */
+export const CORS_PROXY_URL_PARAM = 'url';
+
+/** Header prefix for headers that should be forwarded by the CORS proxy */
+export const CORS_PROXY_HEADER_PREFIX = 'x-llama-server-proxy-header-';
+
+/** Number of trailing characters to keep visible when partially redacting mcp-session-id */
+export const MCP_SESSION_ID_VISIBLE_CHARS = 5;
+
+/** Partial-redaction rules for MCP headers: header name -> visible trailing chars */
+export const MCP_PARTIAL_REDACT_HEADERS = new Map([
+ ['mcp-session-id', MCP_SESSION_ID_VISIBLE_CHARS]
+]);
+
+/** Header names whose values should be redacted in diagnostic logs */
+export const REDACTED_HEADERS = new Set([
+ 'authorization',
+ 'api-key',
+ 'cookie',
+ 'mcp-session-id',
+ 'proxy-authorization',
+ 'set-cookie',
+ 'x-auth-token',
+ 'x-api-key'
+]);
+
+/** Human-readable labels for MCP transport types */
+export const MCP_TRANSPORT_LABELS: Record = {
+ [MCPTransportType.WEBSOCKET]: 'WebSocket',
+ [MCPTransportType.STREAMABLE_HTTP]: 'HTTP',
+ [MCPTransportType.SSE]: 'SSE'
+};
+
+/** Icon components for MCP transport types */
+export const MCP_TRANSPORT_ICONS: Record = {
+ [MCPTransportType.WEBSOCKET]: Zap,
+ [MCPTransportType.STREAMABLE_HTTP]: Globe,
+ [MCPTransportType.SSE]: Radio
+};
+
+/** Standard SSE endpoint path indicators */
+export const MCP_SSE_ENDPOINT = '/sse';
+export const MCP_SSE_ENDPOINT_SLASH = '/sse/';
+export const MCP_SSE_ENDPOINT_QUERY = '/sse?';
diff --git a/llama.cpp/tools/ui/src/lib/constants/mermaid-blocks.ts b/llama.cpp/tools/ui/src/lib/constants/mermaid-blocks.ts
new file mode 100644
index 0000000000000000000000000000000000000000..cd9467fb6e6366ae8190e873dbb355b2e803b7e9
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/mermaid-blocks.ts
@@ -0,0 +1,9 @@
+export const MERMAID_WRAPPER_CLASS = 'mermaid-block-wrapper';
+export const MERMAID_SCROLL_CONTAINER_CLASS = 'mermaid-scroll-container';
+export const MERMAID_BLOCK_CLASS = 'mermaid';
+
+export const MERMAID_LANGUAGE = 'mermaid';
+
+export const MERMAID_SYNTAX_ATTR = 'data-mermaid-syntax';
+export const MERMAID_ID_ATTR = 'data-mermaid-id';
+export const MERMAID_RENDERED_ATTR = 'data-mermaid-rendered';
diff --git a/llama.cpp/tools/ui/src/lib/constants/message-export.ts b/llama.cpp/tools/ui/src/lib/constants/message-export.ts
new file mode 100644
index 0000000000000000000000000000000000000000..79fa36f9141d06c9209d58228f05b14b3b15384d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/message-export.ts
@@ -0,0 +1,20 @@
+// Conversation filename constants
+
+// Length of the trimmed conversation ID in the filename
+export const EXPORT_CONV_ID_TRIM_LENGTH = 8;
+// Maximum length of the sanitized conversation name snippet
+export const EXPORT_CONV_NAME_SUFFIX_MAX_LENGTH = 20;
+// Characters to keep in the ISO timestamp. 19 keeps 2026-01-01T00:00:00
+export const ISO_TIMESTAMP_SLICE_LENGTH = 19;
+
+// Replacements for making the conversation title filename-friendly
+export const NON_ALPHANUMERIC_REGEX = /[^a-z0-9]/gi;
+export const EXPORT_CONV_NONALNUM_REPLACEMENT = '_';
+export const MULTIPLE_UNDERSCORE_REGEX = /_+/g;
+
+// Replacements to the ISO date for use in the export filename
+export const ISO_DATE_TIME_SEPARATOR = 'T';
+export const ISO_DATE_TIME_SEPARATOR_REPLACEMENT = '_';
+
+export const ISO_TIME_SEPARATOR = ':';
+export const ISO_TIME_SEPARATOR_REPLACEMENT = '-';
diff --git a/llama.cpp/tools/ui/src/lib/constants/model-id.ts b/llama.cpp/tools/ui/src/lib/constants/model-id.ts
new file mode 100644
index 0000000000000000000000000000000000000000..ab7932240e49e2d1c7e82b1ac992de7141c17ff8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/model-id.ts
@@ -0,0 +1,44 @@
+/** Sentinel value returned by `indexOf` when a substring is not found. */
+export const MODEL_ID_NOT_FOUND = -1;
+
+/** Separates `` from `` in a model ID, e.g. `org/ModelName`. */
+export const MODEL_ID_ORG_SEPARATOR = '/';
+
+/** Separates named segments within the model path, e.g. `ModelName-7B-GGUF`. */
+export const MODEL_ID_SEGMENT_SEPARATOR = '-';
+
+/** Separates the model path from the quantization tag, e.g. `model:Q4_K_M`. */
+export const MODEL_ID_QUANTIZATION_SEPARATOR = ':';
+
+/**
+ * Matches a quantization/precision segment, e.g. `Q4_K_M`, `IQ4_XS`, `F16`, `BF16`, `MXFP4`.
+ * Case-insensitive to handle both uppercase and lowercase inputs.
+ */
+export const MODEL_QUANTIZATION_SEGMENT_RE =
+ /^(I?Q\d+(_[A-Z0-9]+)*|F\d+|BF\d+|MXFP\d+(_[A-Z0-9]+)*)$/i;
+
+/**
+ * Matches prefix for custom quantization types, e.g. `UD-Q8_K_XL`.
+ */
+export const MODEL_CUSTOM_QUANTIZATION_PREFIX_RE = /^UD$/i;
+
+/**
+ * Matches a parameter-count segment, e.g. `7B`, `1.5b`, `120M`.
+ */
+export const MODEL_PARAMS_RE = /^\d+(\.\d+)?[BbMmKkTt]$/;
+
+/**
+ * Matches an activated-parameter-count segment, e.g. `A10B`, `a2.4b`.
+ * The leading `A`/`a` distinguishes it from a regular params segment.
+ */
+export const MODEL_ACTIVATED_PARAMS_RE = /^[Aa]\d+(\.\d+)?[BbMmKkTt]$/;
+
+/**
+ * Container format segments to exclude from tags (every model uses these).
+ */
+export const MODEL_IGNORED_SEGMENTS = new Set(['GGUF', 'GGML']);
+
+/**
+ * Matches a trailing weight file extension, e.g. `model.gguf` -> `model`.
+ */
+export const MODEL_WEIGHT_EXTENSION_RE = /\.(gguf|ggml)$/i;
diff --git a/llama.cpp/tools/ui/src/lib/constants/model-loading.ts b/llama.cpp/tools/ui/src/lib/constants/model-loading.ts
new file mode 100644
index 0000000000000000000000000000000000000000..a55ba708b1cd9a3c3ed5737bee5f7867071fb872
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/model-loading.ts
@@ -0,0 +1,14 @@
+/**
+ * Labels shown while a model loads, keyed by the stage reported on /models/sse.
+ */
+export const MODEL_LOAD_STAGE_LABELS: Record = {
+ text_model: 'Loading weights',
+ spec_model: 'Loading draft',
+ mmproj_model: 'Loading projector'
+};
+
+/**
+ * Share of the bar reserved for each load phase after text_model.
+ * text_model fills the rest, so a plain model reaches 100% on its own.
+ */
+export const MODEL_LOAD_TAIL_SHARE = 0.1;
diff --git a/llama.cpp/tools/ui/src/lib/constants/precision.ts b/llama.cpp/tools/ui/src/lib/constants/precision.ts
new file mode 100644
index 0000000000000000000000000000000000000000..8df5c4f966656a63085247faa8d43c7d20e78df3
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/precision.ts
@@ -0,0 +1,2 @@
+export const PRECISION_MULTIPLIER = 1000000;
+export const PRECISION_DECIMAL_PLACES = 6;
diff --git a/llama.cpp/tools/ui/src/lib/constants/processing-info.ts b/llama.cpp/tools/ui/src/lib/constants/processing-info.ts
new file mode 100644
index 0000000000000000000000000000000000000000..2c3f7dc53440614c4ab1ef98a517acb0e1a4b40f
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/processing-info.ts
@@ -0,0 +1,8 @@
+export const PROCESSING_INFO_TIMEOUT = 2000;
+
+/**
+ * Statistics units labels
+ */
+export const STATS_UNITS = {
+ TOKENS_PER_SECOND: 't/s'
+} as const;
diff --git a/llama.cpp/tools/ui/src/lib/constants/pwa.ts b/llama.cpp/tools/ui/src/lib/constants/pwa.ts
new file mode 100644
index 0000000000000000000000000000000000000000..b9171d4bbffebf24d2f99534293e15ab117bb7a8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/pwa.ts
@@ -0,0 +1,368 @@
+/**
+ * Centralized PWA constants to avoid magic strings, regexes, and duplicated
+ * definitions across the codebase.
+ */
+
+import { APP_NAME } from './app';
+
+export const MEDIA_QUERIES = {
+ PREFERS_DARK: '(prefers-color-scheme: dark)',
+ PREFERS_LIGHT: '(prefers-color-scheme: light)',
+ DISPLAY_MODE_STANDALONE: '(display-mode: standalone)'
+} as const;
+
+export const THEME_COLORS = {
+ LIGHT: '#ffffff',
+ DARK: '#0d0d0d',
+ ACCENT_BLUE: '#2563eb',
+ ACCENT_BLUE_HOVER: '#1d4ed8',
+ BACKGROUND_LIGHT: 'white',
+ BACKGROUND_DARK: '#111111',
+ TITLE_UPDATE_ALERT: {
+ BORDER_LIGHT: 'zinc-200',
+ BORDER_DARK: 'zinc-700',
+ BG_LIGHT: 'white',
+ BG_DARK: 'zinc-800',
+ TEXT_LIGHT: 'zinc-500',
+ TEXT_DARK: 'zinc-400'
+ }
+} as const;
+
+export const FAVICON_PATHS = {
+ ICO_LIGHT: 'favicon.ico',
+ ICO_DARK: 'favicon-dark.ico',
+ SVG_LIGHT: 'favicon.svg',
+ SVG_DARK: 'favicon-dark.svg'
+} as const;
+
+// Substituted for `currentColor` in src/lib/assets/logo.svg when generating
+// the light/dark static sources consumed by the PWA asset generator.
+export const FAVICON_COLORS = {
+ LIGHT: '#111111',
+ DARK: '#fafafa'
+} as const;
+
+export const FAVICON_SELECTORS = {
+ ICO_48X48: 'link[rel="icon"][sizes="48x48"]',
+ SVG_ANY: 'link[rel="icon"][type="image/svg+xml"]'
+} as const;
+
+export const APPLE_ASSETS = {
+ TOUCH_ICON: 'apple-touch-icon-180x180.png'
+} as const;
+
+export const PWA_MANIFEST = {
+ name: APP_NAME,
+ short_name: APP_NAME,
+ description: 'Local AI chat interface powered by llama.cpp',
+ start_url: './',
+ display: 'standalone' as const,
+ background_color: THEME_COLORS.BACKGROUND_LIGHT,
+ theme_color: THEME_COLORS.BACKGROUND_LIGHT,
+ icons: [
+ { src: 'pwa-64x64.png', sizes: '64x64', type: 'image/png' },
+ { src: 'pwa-192x192.png', sizes: '192x192', type: 'image/png' },
+ { src: 'pwa-512x512.png', sizes: '512x512', type: 'image/png', purpose: 'any' as const },
+ {
+ src: 'maskable-icon-512x512.png',
+ sizes: '512x512',
+ type: 'image/png',
+ purpose: 'maskable' as const
+ }
+ ]
+};
+
+export const PWA_ICON_PATHS = {
+ PWA_64: '/pwa-64x64.png',
+ PWA_192: '/pwa-192x192.png',
+ PWA_512: '/pwa-512x512.png',
+ MASKABLE_512: '/maskable-icon-512x512.png'
+} as const;
+
+/** Apple device dimensions (logical points) and DPR, from Apple HIG. */
+export const APPLE_DEVICES = {
+ // iPhones (DPR 3)
+ '1170x2532': { width: 390, height: 844, dpr: 3 }, // iPhone 13, 15
+ '1179x2556': { width: 393, height: 852, dpr: 3 }, // iPhone 14, 15 Pro, 16
+ '1206x2622': { width: 402, height: 874, dpr: 3 }, // iPhone 16 Plus, 16e
+ '1284x2778': { width: 428, height: 926, dpr: 3 }, // iPhone 15 Plus
+ '1290x2796': { width: 430, height: 932, dpr: 3 }, // iPhone 15 Pro Max, 16 Pro
+ '1320x2868': { width: 440, height: 956, dpr: 3 }, // iPhone 16 Pro Max
+ '750x1334': { width: 375, height: 667, dpr: 2 }, // iPhone 6/7/8, 14
+ '640x1136': { width: 320, height: 568, dpr: 2 }, // iPhone 6/7/8 Plus
+ // iPads (DPR 2)
+ '1668x2388': { width: 834, height: 1194, dpr: 2 }, // iPad Air 11", iPad 11"
+ '2048x2732': { width: 1024, height: 1366, dpr: 2 }, // iPad Pro 12.9"
+ '1640x2360': { width: 820, height: 1180, dpr: 2 }, // iPad Air 10.9"
+ '1032x1376': { width: 1032, height: 1376, dpr: 2 }, // iPad Air 13"
+ '744x1133': { width: 376, height: 573, dpr: 2 } // iPad mini 8.3"
+} as const;
+
+export type AppleDeviceKey = keyof typeof APPLE_DEVICES;
+
+export const PWA_FILE_PATHS = {
+ MANIFEST: '/manifest.webmanifest',
+ SERVICE_WORKER: '/sw.js',
+ VERSION: '/version.json',
+ WORKBOX: '/workbox-.js'
+} as const;
+
+// Used by the server middleware to skip API key validation.
+// Keep in sync with tools/server/server-http.cpp public_endpoints list.
+
+export const PUBLIC_ENDPOINTS = [
+ '/health',
+ '/v1/health',
+ '/models',
+ '/v1/models',
+ '/props',
+ '/metrics',
+ '/',
+ '/index.html',
+
+ '/favicon.ico',
+ '/favicon-dark.ico',
+ '/favicon.svg',
+ '/favicon-dark.svg',
+ '/pwa-64x64.png',
+ '/pwa-192x192.png',
+ '/pwa-512x512.png',
+ '/maskable-icon-512x512.png',
+ '/apple-touch-icon-180x180.png',
+ '/apple-splash-portrait-640x1136.png',
+ '/apple-splash-landscape-640x1136.png',
+ '/apple-splash-portrait-750x1334.png',
+ '/apple-splash-landscape-750x1334.png',
+ '/apple-splash-portrait-1170x2532.png',
+ '/apple-splash-landscape-1170x2532.png',
+ '/apple-splash-portrait-1179x2556.png',
+ '/apple-splash-landscape-1179x2556.png',
+ '/apple-splash-portrait-1206x2622.png',
+ '/apple-splash-landscape-1206x2622.png',
+ '/apple-splash-portrait-1284x2778.png',
+ '/apple-splash-landscape-1284x2778.png',
+ '/apple-splash-portrait-1290x2796.png',
+ '/apple-splash-landscape-1290x2796.png',
+ '/apple-splash-portrait-1320x2868.png',
+ '/apple-splash-landscape-1320x2868.png',
+ '/apple-splash-portrait-1488x2266.png',
+ '/apple-splash-landscape-1488x2266.png',
+ '/apple-splash-portrait-1640x2360.png',
+ '/apple-splash-landscape-1640x2360.png',
+ '/apple-splash-portrait-1668x2388.png',
+ '/apple-splash-landscape-1668x2388.png',
+ '/apple-splash-portrait-2048x2732.png',
+ '/apple-splash-landscape-2048x2732.png',
+ '/apple-splash-portrait-dark-640x1136.png',
+ '/apple-splash-landscape-dark-640x1136.png',
+ '/apple-splash-portrait-dark-750x1334.png',
+ '/apple-splash-landscape-dark-750x1334.png',
+ '/apple-splash-portrait-dark-1170x2532.png',
+ '/apple-splash-landscape-dark-1170x2532.png',
+ '/apple-splash-portrait-dark-1179x2556.png',
+ '/apple-splash-landscape-dark-1179x2556.png',
+ '/apple-splash-portrait-dark-1206x2622.png',
+ '/apple-splash-landscape-dark-1206x2622.png',
+ '/apple-splash-portrait-dark-1284x2778.png',
+ '/apple-splash-landscape-dark-1284x2778.png',
+ '/apple-splash-portrait-dark-1290x2796.png',
+ '/apple-splash-landscape-dark-1290x2796.png',
+ '/apple-splash-portrait-dark-1320x2868.png',
+ '/apple-splash-landscape-dark-1320x2868.png',
+ '/apple-splash-portrait-dark-1488x2266.png',
+ '/apple-splash-landscape-dark-1488x2266.png',
+ '/apple-splash-portrait-dark-1640x2360.png',
+ '/apple-splash-landscape-dark-1640x2360.png',
+ '/apple-splash-portrait-dark-1668x2388.png',
+ '/apple-splash-landscape-dark-1668x2388.png',
+ '/apple-splash-portrait-dark-2048x2732.png',
+ '/apple-splash-landscape-dark-2048x2732.png',
+ '/manifest.webmanifest',
+ '/sw.js',
+ '/version.json',
+ '/workbox-.js'
+] as const;
+export const BUILD_CONFIG = {
+ OUTPUT_DIR: './dist',
+ GUIDE_COMMENT: `
+
+`.trim()
+} as const;
+
+export const REGEX_PATTERNS = {
+ SPLASH_FILE: /^apple-splash-(portrait|landscape)-(dark-)?(\d+)x(\d+)\.png$/,
+ HEAD_CLOSE: /\t*<\/head>/
+} as const;
+
+// Device names used by @vite-pwa/assets-generator for splash screen generation.
+// Keep in sync with pwa-assets.config.ts.
+export const PWA_GENERATOR_DEVICES = [
+ 'iPhone 13',
+ 'iPhone 13 Pro',
+ 'iPhone 13 Pro Max',
+ 'iPhone 14',
+ 'iPhone 14 Plus',
+ 'iPhone 14 Pro',
+ 'iPhone 14 Pro Max',
+ 'iPhone 15',
+ 'iPhone 15 Plus',
+ 'iPhone 15 Pro',
+ 'iPhone 15 Pro Max',
+ 'iPhone 16',
+ 'iPhone 16 Plus',
+ 'iPhone 16 Pro',
+ 'iPhone 16 Pro Max',
+ 'iPhone 16e',
+ 'iPhone SE 4"',
+ 'iPhone SE 4.7"',
+ 'iPad 11"',
+ 'iPad Air 10.9"',
+ 'iPad Air 11"',
+ 'iPad Air 13"',
+ 'iPad Pro 11"',
+ 'iPad Pro 12.9"',
+ 'iPad mini 8.3"'
+] as const;
+
+// PWA assets generator configuration — used by pwa-assets.config.ts
+// FAVICON_PADDING: fraction (0..1) of the icon reserved as equal margin on
+// each side. Applied to icon PNG/ICO outputs by @vite-pwa/assets-generator and
+// post-processed into the static favicon.svg so the in-app logo (which reads
+// src/lib/assets/logo.svg directly) is unaffected.
+export const PWA_ASSET_GENERATOR = {
+ LINK_PRESET: '2023',
+ FAVICON_PADDING: 0.04,
+ SPLASH_PADDING: 0.75,
+ FIT_MODE: 'contain',
+ ADD_MEDIA_SCREEN: true,
+ BASE_PATH: './',
+ XHTML: false,
+ PNG_COMPRESSION_LEVEL: 9,
+ PNG_QUALITY: 60,
+ DARK_PREFIX: 'dark-'
+} as const;
+
+export const CACHE_SETTINGS = {
+ IMMUTABLE_MAX_AGE_SECONDS: 31536000,
+ API_CACHE_MAX_AGE_SECONDS: 60 * 60 * 24,
+ API_CACHE_MAX_ENTRIES: 50,
+ MAX_FILE_SIZE_BYTES: 10 * 1024 * 1024
+} as const;
+
+export const GLOB_PATTERNS: string[] = [
+ '**/*.{js,css,html,ico,svg,png,webp,woff,woff2,json,webmanifest}'
+];
+
+// loading.html is the model loading page served by llama-server itself.
+// The SvelteKit PWA manifest transform strips the html extension from every
+// precache entry to match clean URLs, but loading.html is a plain static asset
+// with no clean URL, so static servers answer 404 and the SW install fails.
+export const GLOB_IGNORES: string[] = ['**/loading.html'];
+
+export const SW_CONFIG = {
+ CHECK_INTERVAL_MS: 60000,
+ UPDATE_FETCH_OPTIONS: {
+ CACHE: 'no-store',
+ HEADERS: {
+ CACHE: 'no-store',
+ CACHE_CONTROL: 'no-cache'
+ }
+ }
+} as const;
+
+// Runtime caching configuration for Workbox
+export const RUNTIME_CACHING = {
+ HANDLER: 'NetworkFirst',
+ CACHE_NAME: 'api-cache'
+} as const;
+
+// Workbox runtime caching patterns
+export const API_CACHING_PATTERNS = {
+ V1_API: /^\/v1\/.*/,
+ STATIC_API: /^\/(health|props|models|tools|slots|cors-proxy).*/
+} as const;
+
+// SvelteKit PWA plugin options
+export const PWA_KIT_OPTIONS = {} as const;
+
+export const APPLE_META_TAGS = {
+ MOBILE_WEB_APP_CAPABLE: { name: 'apple-mobile-web-app-capable', content: 'yes' },
+ STATUS_BAR_STYLE: { name: 'apple-mobile-web-app-status-bar-style', content: 'black-translucent' },
+ MOBILE_WEB_APP_TITLE: { name: 'apple-mobile-web-app-title' }
+} as const;
+
+// Splash screen HTML link tag prefix used by generateSplashScreenLinks
+export const SPLASH_LINK = {
+ HTML: ' = {
+ [ReasoningEffort.LOW]: 512,
+ [ReasoningEffort.MEDIUM]: 2048,
+ [ReasoningEffort.HIGH]: 8192,
+ [ReasoningEffort.MAX]: -1 // unlimited
+};
diff --git a/llama.cpp/tools/ui/src/lib/constants/reasoning-effort.ts b/llama.cpp/tools/ui/src/lib/constants/reasoning-effort.ts
new file mode 100644
index 0000000000000000000000000000000000000000..d854e912a8510cd4e9debe64321a76daa4c2c1e7
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/reasoning-effort.ts
@@ -0,0 +1,21 @@
+import { ReasoningEffort } from '$lib/enums';
+import type { ReasoningEffortLevel } from '$lib/types';
+
+/**
+ * Reasoning effort UI labels.
+ * Keys match the ReasoningEffort enum values for type-safe lookups.
+ */
+export const REASONING_EFFORT_LABELS: Record = {
+ [ReasoningEffort.LOW]: 'Low',
+ [ReasoningEffort.MEDIUM]: 'Medium',
+ [ReasoningEffort.HIGH]: 'High',
+ [ReasoningEffort.MAX]: 'Max'
+};
+
+export const REASONING_EFFORT_LEVELS: ReasoningEffortLevel[] = [
+ { value: 'off', label: 'Off', isOff: true },
+ { value: ReasoningEffort.LOW, label: 'Low' },
+ { value: ReasoningEffort.MEDIUM, label: 'Medium' },
+ { value: ReasoningEffort.HIGH, label: 'High' },
+ { value: ReasoningEffort.MAX, label: 'Max', hasInfo: true }
+];
diff --git a/llama.cpp/tools/ui/src/lib/constants/recommended-mcp-servers.ts b/llama.cpp/tools/ui/src/lib/constants/recommended-mcp-servers.ts
new file mode 100644
index 0000000000000000000000000000000000000000..1fb3ba8c70e3c4975a9bbf0a4f354bac7db76a09
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/recommended-mcp-servers.ts
@@ -0,0 +1,35 @@
+import { DEFAULT_MCP_CONFIG } from './mcp';
+import type { RecommendedMCPServer } from '$lib/types';
+
+/**
+ * Pre-defined recommended MCP servers.
+ *
+ * Servers are enabled by default, but they are not turned on for individual
+ * conversations until the user explicitly enables them (so their tools are
+ * disabled by default).
+ */
+export const RECOMMENDED_MCP_SERVERS: RecommendedMCPServer[] = [
+ {
+ id: 'exa-web-search',
+ name: 'Exa Web Search',
+ description: 'Search the web and retrieve relevant content.',
+ url: 'https://mcp.exa.ai/mcp',
+ enabled: true,
+ requestTimeoutSeconds: DEFAULT_MCP_CONFIG.requestTimeoutSeconds
+ },
+ {
+ id: 'huggingface-mcp',
+ name: 'Hugging Face',
+ description:
+ 'Browse models, datasets, spaces and machine learning papers from the Hugging Face hub.',
+ url: 'https://huggingface.co/mcp',
+ enabled: true,
+ requestTimeoutSeconds: DEFAULT_MCP_CONFIG.requestTimeoutSeconds
+ }
+];
+
+export const RECOMMENDED_MCP_SERVER_IDS = new Set(
+ RECOMMENDED_MCP_SERVERS.map((server) => server.id)
+);
+
+export const RECOMMENDED_MCP_SERVERS_OPTIN_DIALOG_DELAY = 1000;
diff --git a/llama.cpp/tools/ui/src/lib/constants/routes.ts b/llama.cpp/tools/ui/src/lib/constants/routes.ts
new file mode 100644
index 0000000000000000000000000000000000000000..1adf0a600588d5db51cd5ea55d3a869cafd49ae8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/routes.ts
@@ -0,0 +1,29 @@
+export const NEW_CHAT_PARAM = 'new_chat';
+
+/** Settings section slugs — used for routes and navigation. */
+export const SETTINGS_SECTION_SLUGS = {
+ GENERAL: 'general',
+ DISPLAY: 'display',
+ SAMPLING: 'sampling',
+ PENALTIES: 'penalties',
+ AGENTIC: 'agentic',
+ DEVELOPER: 'developer',
+ MCP: 'mcp',
+ TOOLS: 'tools',
+ IMPORT_EXPORT: 'import-export'
+} as const;
+
+export const ROUTES = {
+ /** Root — start of the app. */
+ START: '#/',
+ /** New chat — root with new chat query param. */
+ NEW_CHAT: `?${NEW_CHAT_PARAM}=true#/`,
+ /** Chat base — for dynamic chat URLs use RouterService. */
+ CHAT: '#/chat',
+ /** MCP servers. */
+ MCP_SERVERS: '#/mcp-servers',
+ /** Settings base — for dynamic settings URLs use RouterService. */
+ SETTINGS: '#/settings',
+ /** Search — mobile-only full-page conversation search. */
+ SEARCH: '#/search'
+} as const;
diff --git a/llama.cpp/tools/ui/src/lib/constants/sandbox.ts b/llama.cpp/tools/ui/src/lib/constants/sandbox.ts
new file mode 100644
index 0000000000000000000000000000000000000000..30e769509e820018c933c64e6a7c1dfaf65a57dd
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/sandbox.ts
@@ -0,0 +1,39 @@
+import { JsonSchemaType, ToolCallType } from '$lib/enums';
+import type { OpenAIToolDefinition } from '$lib/types';
+
+export const SANDBOX_TOOL_NAME = 'run_javascript';
+
+export const SANDBOX_TIMEOUT_MS_DEFAULT = 10000;
+
+export const SANDBOX_TIMEOUT_MS_MAX = 30000;
+
+export const SANDBOX_OUTPUT_MAX_CHARS = 8192;
+
+export const SANDBOX_EMPTY_OUTPUT = '(no output)';
+
+export const SANDBOX_TRUNCATION_NOTICE = '[output truncated]';
+
+export const SANDBOX_TOOL_DEFINITION: OpenAIToolDefinition = {
+ type: ToolCallType.FUNCTION,
+ function: {
+ name: SANDBOX_TOOL_NAME,
+ description:
+ 'Execute JavaScript in a sandboxed browser worker (no DOM, no page access). ' +
+ 'Top level await is supported. Use console.log to print intermediate values; ' +
+ 'a top level return statement is captured as the result.',
+ parameters: {
+ type: JsonSchemaType.OBJECT,
+ properties: {
+ code: {
+ type: JsonSchemaType.STRING,
+ description: 'JavaScript source to execute'
+ },
+ timeout_ms: {
+ type: JsonSchemaType.NUMBER,
+ description: `Execution timeout in milliseconds, default ${SANDBOX_TIMEOUT_MS_DEFAULT}, max ${SANDBOX_TIMEOUT_MS_MAX}`
+ }
+ },
+ required: ['code']
+ }
+ }
+};
diff --git a/llama.cpp/tools/ui/src/lib/constants/settings-keys.ts b/llama.cpp/tools/ui/src/lib/constants/settings-keys.ts
new file mode 100644
index 0000000000000000000000000000000000000000..498a79c7396e36e1e52a5c3784df56958ff54659
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/settings-keys.ts
@@ -0,0 +1,78 @@
+/**
+ * Settings key constants for ChatSettings configuration.
+ *
+ * These keys correspond to properties in SettingsConfigType and are used
+ * in settings field configurations to ensure consistency.
+ */
+export const SETTINGS_KEYS = {
+ // General
+ THEME: 'theme',
+ API_KEY: 'apiKey',
+ SYSTEM_MESSAGE: 'systemMessage',
+ PASTE_LONG_TEXT_TO_FILE_LEN: 'pasteLongTextToFileLen',
+ COPY_TEXT_ATTACHMENTS_AS_PLAIN_TEXT: 'copyTextAttachmentsAsPlainText',
+ SEND_ON_ENTER: 'sendOnEnter',
+ ENABLE_CONTINUE_GENERATION: 'enableContinueGeneration',
+ PDF_AS_IMAGE: 'pdfAsImage',
+ ASK_FOR_TITLE_CONFIRMATION: 'askForTitleConfirmation',
+ TITLE_GENERATION_USE_FIRST_LINE: 'titleGenerationUseFirstLine',
+ TITLE_GENERATION_USE_LLM: 'titleGenerationUseLLM',
+ TITLE_GENERATION_PROMPT: 'titleGenerationPrompt',
+ MAX_IMAGE_RESOLUTION: 'maxImageMPixels',
+ // Display
+ SHOW_MESSAGE_STATS: 'showMessageStats',
+ SHOW_THOUGHT_IN_PROGRESS: 'showThoughtInProgress',
+ KEEP_STATS_VISIBLE: 'keepStatsVisible',
+ AUTO_MIC_ON_EMPTY: 'autoMicOnEmpty',
+ RENDER_USER_CONTENT_AS_MARKDOWN: 'renderUserContentAsMarkdown',
+ DISABLE_AUTO_SCROLL: 'disableAutoScroll',
+ ALWAYS_SHOW_SIDEBAR_ON_DESKTOP: 'alwaysShowSidebarOnDesktop',
+ FULL_HEIGHT_CODE_BLOCKS: 'fullHeightCodeBlocks',
+ SHOW_RAW_MODEL_NAMES: 'showRawModelNames',
+ SHOW_MODEL_QUANTIZATION: 'showModelQuantization',
+ SHOW_MODEL_TAGS: 'showModelTags',
+ SHOW_BUILD_VERSION: 'showBuildVersion',
+ SHOW_SYSTEM_MESSAGE: 'showSystemMessage',
+ RENDER_THINKING_AS_MARKDOWN: 'renderThinkingAsMarkdown',
+ // Sampling
+ TEMPERATURE: 'temperature',
+ DYNATEMP_RANGE: 'dynatemp_range',
+ DYNATEMP_EXPONENT: 'dynatemp_exponent',
+ TOP_K: 'top_k',
+ TOP_P: 'top_p',
+ MIN_P: 'min_p',
+ XTC_PROBABILITY: 'xtc_probability',
+ XTC_THRESHOLD: 'xtc_threshold',
+ TYP_P: 'typ_p',
+ MAX_TOKENS: 'max_tokens',
+ SAMPLERS: 'samplers',
+ BACKEND_SAMPLING: 'backend_sampling',
+ // Penalties
+ REPEAT_LAST_N: 'repeat_last_n',
+ REPEAT_PENALTY: 'repeat_penalty',
+ PRESENCE_PENALTY: 'presence_penalty',
+ FREQUENCY_PENALTY: 'frequency_penalty',
+ DRY_MULTIPLIER: 'dry_multiplier',
+ DRY_BASE: 'dry_base',
+ DRY_ALLOWED_LENGTH: 'dry_allowed_length',
+ DRY_PENALTY_LAST_N: 'dry_penalty_last_n',
+ // MCP
+ MCP_SERVERS: 'mcpServers',
+ MCP_REQUEST_TIMEOUT_SECONDS: 'mcpRequestTimeoutSeconds',
+ MCP_DEFAULT_SERVER_OVERRIDES: 'mcpDefaultServerOverrides',
+ AGENTIC_MAX_TURNS: 'agenticMaxTurns',
+ ALWAYS_SHOW_AGENTIC_TURNS: 'alwaysShowAgenticTurns',
+ AGENTIC_MAX_TOOL_PREVIEW_LINES: 'agenticMaxToolPreviewLines',
+ SHOW_TOOL_CALL_IN_PROGRESS: 'showToolCallInProgress',
+ // Performance
+ PRE_ENCODE_CONVERSATION: 'preEncodeConversation',
+ // Developer
+ DISABLE_REASONING_PARSING: 'disableReasoningParsing',
+ EXCLUDE_REASONING_FROM_CONTEXT: 'excludeReasoningFromContext',
+ ENABLE_THINKING: 'enableThinking',
+ SHOW_RAW_OUTPUT_SWITCH: 'showRawOutputSwitch',
+ // PY_INTERPRETER_ENABLED: 'pyInterpreterEnabled',
+ JS_SANDBOX_ENABLED: 'jsSandboxEnabled',
+ CUSTOM_JSON: 'customJson',
+ CUSTOM_CSS: 'customCss'
+} as const;
diff --git a/llama.cpp/tools/ui/src/lib/constants/settings-registry.ts b/llama.cpp/tools/ui/src/lib/constants/settings-registry.ts
new file mode 100644
index 0000000000000000000000000000000000000000..2047d0b43bf7faade993a51ca77e7b09955edf50
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/settings-registry.ts
@@ -0,0 +1,866 @@
+import { ColorMode } from '$lib/enums/ui.enums';
+import { SettingsFieldType } from '$lib/enums/settings.enums';
+import { SyncableParameterType } from '$lib/enums';
+import {
+ Funnel,
+ AlertTriangle,
+ Code,
+ Monitor,
+ ListRestart,
+ Sliders,
+ PencilRuler,
+ Database,
+ Monitor as MonitorIcon,
+ Sun,
+ Moon
+} from '@lucide/svelte';
+import type { Component } from 'svelte';
+import type {
+ SettingsConfigValue,
+ SyncableParameter,
+ SettingsEntry,
+ SettingsSectionTitle,
+ SettingsSectionEntry,
+ SettingsSection
+} from '$lib/types';
+import { CLI_FLAGS, DEFAULT_MCP_CONFIG } from '$lib/constants';
+import McpLogo from '$lib/components/app/mcp/McpLogo.svelte';
+import { SETTINGS_KEYS } from './settings-keys';
+import { ROUTES, SETTINGS_SECTION_SLUGS } from './routes';
+import { TITLE_GENERATION } from './title-generation';
+import { RECOMMENDED_MCP_SERVERS } from './recommended-mcp-servers';
+
+export const SETTINGS_SECTION_TITLES = {
+ GENERAL: 'General',
+ DISPLAY: 'Display',
+ SAMPLING: 'Sampling',
+ PENALTIES: 'Penalties',
+ AGENTIC: 'Agentic',
+ TOOLS: 'Tools',
+ MCP: 'MCP',
+ IMPORT_EXPORT: 'Import/Export',
+ DEVELOPER: 'Developer'
+} as const;
+
+const STANDALONE_SECTIONS: { title: SettingsSectionTitle; slug: string; icon: Component }[] = [
+ { title: SETTINGS_SECTION_TITLES.TOOLS, slug: SETTINGS_SECTION_SLUGS.TOOLS, icon: PencilRuler },
+ {
+ title: SETTINGS_SECTION_TITLES.IMPORT_EXPORT,
+ slug: SETTINGS_SECTION_SLUGS.IMPORT_EXPORT,
+ icon: Database
+ }
+];
+
+const COLOR_MODE_OPTIONS: Array<{ value: string; label: string; icon: Component }> = [
+ { value: ColorMode.SYSTEM, label: 'System', icon: MonitorIcon },
+ { value: ColorMode.LIGHT, label: 'Light', icon: Sun },
+ { value: ColorMode.DARK, label: 'Dark', icon: Moon }
+];
+
+const SETTINGS_REGISTRY: Record = {
+ [SETTINGS_SECTION_SLUGS.GENERAL]: {
+ title: SETTINGS_SECTION_TITLES.GENERAL,
+ slug: SETTINGS_SECTION_SLUGS.GENERAL,
+ icon: Sliders,
+ settings: [
+ {
+ key: SETTINGS_KEYS.THEME,
+ label: 'Theme',
+ help: 'Choose the color theme for the interface. You can choose between System (follows your device settings), Light, or Dark.',
+ defaultValue: ColorMode.SYSTEM,
+ type: SettingsFieldType.SELECT,
+ section: SETTINGS_SECTION_SLUGS.GENERAL,
+ options: COLOR_MODE_OPTIONS,
+ sync: { serverKey: SETTINGS_KEYS.THEME, paramType: SyncableParameterType.STRING }
+ },
+ {
+ key: SETTINGS_KEYS.API_KEY,
+ label: 'API Key',
+ help: `Set the API Key if you are using ${CLI_FLAGS.API_KEY} option for the server.`,
+ defaultValue: '',
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.GENERAL
+ },
+ {
+ key: SETTINGS_KEYS.SYSTEM_MESSAGE,
+ label: 'System Message',
+ help: 'The starting message that defines how model should behave.',
+ defaultValue: '',
+ type: SettingsFieldType.TEXTAREA,
+ section: SETTINGS_SECTION_SLUGS.GENERAL,
+ sync: {
+ serverKey: SETTINGS_KEYS.SYSTEM_MESSAGE,
+ paramType: SyncableParameterType.STRING
+ }
+ },
+ {
+ key: SETTINGS_KEYS.PASTE_LONG_TEXT_TO_FILE_LEN,
+ label: 'Paste long text to file length',
+ help: 'On pasting long text, it will be converted to a file. You can control the file length by setting the value of this parameter. Value 0 means disable.',
+ defaultValue: 2500,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.GENERAL,
+ sync: {
+ serverKey: SETTINGS_KEYS.PASTE_LONG_TEXT_TO_FILE_LEN,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.SEND_ON_ENTER,
+ label: 'Send message on Enter',
+ help: 'Use Enter to send messages and Shift + Enter for new lines. When disabled, use Ctrl/Cmd + Enter.',
+ defaultValue: true,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.GENERAL,
+ sync: {
+ serverKey: SETTINGS_KEYS.SEND_ON_ENTER,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.COPY_TEXT_ATTACHMENTS_AS_PLAIN_TEXT,
+ label: 'Copy text attachments as plain text',
+ help: 'When copying a message with text attachments, combine them into a single plain text string instead of a special format that can be pasted back as attachments.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.GENERAL,
+ sync: {
+ serverKey: SETTINGS_KEYS.COPY_TEXT_ATTACHMENTS_AS_PLAIN_TEXT,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.ENABLE_CONTINUE_GENERATION,
+ label: 'Enable "Continue" button',
+ help: 'Enable "Continue" button for assistant messages, including reasoning models.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.GENERAL,
+ isExperimental: true,
+ sync: {
+ serverKey: SETTINGS_KEYS.ENABLE_CONTINUE_GENERATION,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.PDF_AS_IMAGE,
+ label: 'Parse PDF as image',
+ help: 'Parse PDF as image instead of text. Automatically falls back to text processing for non-vision models.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.GENERAL,
+ sync: {
+ serverKey: SETTINGS_KEYS.PDF_AS_IMAGE,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.ASK_FOR_TITLE_CONFIRMATION,
+ label: 'Ask for confirmation before changing conversation title',
+ help: 'Ask for confirmation before automatically changing conversation title when editing the first message.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.GENERAL,
+ sync: {
+ serverKey: SETTINGS_KEYS.ASK_FOR_TITLE_CONFIRMATION,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.TITLE_GENERATION_USE_FIRST_LINE,
+ label: 'Use first non-empty line for conversation title',
+ help: 'Use only the first non-empty line of the prompt to generate the conversation title.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.GENERAL,
+ sync: {
+ serverKey: SETTINGS_KEYS.TITLE_GENERATION_USE_FIRST_LINE,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.TITLE_GENERATION_USE_LLM,
+ label: 'Use LLM to generate conversation title',
+ help: 'Use the LLM to automatically generate conversation titles based on the first message exchange.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.GENERAL,
+ isExperimental: true
+ },
+ {
+ key: SETTINGS_KEYS.TITLE_GENERATION_PROMPT,
+ label: 'LLM title generation prompt',
+ help: 'Optional template for the title generation prompt. Use {{USER}} for the user message and {{ASSISTANT}} for the assistant message.',
+ defaultValue: TITLE_GENERATION.DEFAULT_PROMPT,
+ type: SettingsFieldType.TEXTAREA,
+ section: SETTINGS_SECTION_SLUGS.GENERAL
+ },
+ {
+ key: SETTINGS_KEYS.MAX_IMAGE_RESOLUTION,
+ label: 'Maximum image resolution (megapixels)',
+ help: 'Images larger than this will be resized before sending to server. Set to 0 to disable.',
+ defaultValue: 0,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.GENERAL
+ }
+ ]
+ },
+ [SETTINGS_SECTION_SLUGS.DISPLAY]: {
+ title: SETTINGS_SECTION_TITLES.DISPLAY,
+ slug: SETTINGS_SECTION_SLUGS.DISPLAY,
+ icon: Monitor,
+ settings: [
+ {
+ key: SETTINGS_KEYS.SHOW_MESSAGE_STATS,
+ label: 'Show message generation statistics',
+ help: 'Display generation statistics (tokens/second, token count, duration) below each assistant message.',
+ defaultValue: true,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.SHOW_MESSAGE_STATS,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.SHOW_THOUGHT_IN_PROGRESS,
+ label: 'Show thought in progress',
+ help: 'Expand thought process by default when generating messages.',
+ defaultValue: true,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.SHOW_THOUGHT_IN_PROGRESS,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.SHOW_TOOL_CALL_IN_PROGRESS,
+ label: 'Show tool call in progress',
+ help: 'Automatically expand tool call details while executing and keep them expanded after completion.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.SHOW_TOOL_CALL_IN_PROGRESS,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.KEEP_STATS_VISIBLE,
+ label: 'Keep stats visible after generation',
+ help: 'Keep processing statistics visible after generation finishes.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.KEEP_STATS_VISIBLE,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.AUTO_MIC_ON_EMPTY,
+ label: 'Show microphone on empty input',
+ help: 'Automatically show microphone button instead of send button when textarea is empty for models with audio modality support.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ isExperimental: true,
+ sync: {
+ serverKey: SETTINGS_KEYS.AUTO_MIC_ON_EMPTY,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.RENDER_USER_CONTENT_AS_MARKDOWN,
+ label: 'Render user content as Markdown',
+ help: 'Render user messages using markdown formatting in the chat.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.RENDER_USER_CONTENT_AS_MARKDOWN,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.RENDER_THINKING_AS_MARKDOWN,
+ label: 'Render thinking as Markdown',
+ help: 'Render the reasoning/thinking block content as formatted Markdown instead of plain text.',
+ defaultValue: true,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.RENDER_THINKING_AS_MARKDOWN,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.FULL_HEIGHT_CODE_BLOCKS,
+ label: 'Use full height code blocks',
+ help: 'Always display code blocks at their full natural height, overriding any height limits.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.FULL_HEIGHT_CODE_BLOCKS,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.DISABLE_AUTO_SCROLL,
+ label: 'Disable automatic scroll',
+ help: 'Disable automatic scrolling while messages stream so you can control the viewport position manually.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.DISABLE_AUTO_SCROLL,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.ALWAYS_SHOW_SIDEBAR_ON_DESKTOP,
+ label: 'Always show sidebar on desktop',
+ help: 'Always keep the sidebar visible on desktop instead of auto-hiding it.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.ALWAYS_SHOW_SIDEBAR_ON_DESKTOP,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.SHOW_RAW_MODEL_NAMES,
+ label: 'Show raw model names',
+ help: 'Display full raw model identifiers (e.g. "ggml-org/GLM-4.7-Flash-GGUF:Q8_0") instead of parsed names with badges.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.SHOW_RAW_MODEL_NAMES,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.SHOW_MODEL_QUANTIZATION,
+ label: 'Show model quantization information',
+ help: 'Display quantization badges (e.g. Q8_0, Q4_K_M) next to model names throughout the interface.',
+ defaultValue: true,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.SHOW_MODEL_QUANTIZATION,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.SHOW_MODEL_TAGS,
+ label: 'Show model tags',
+ help: 'Display model tags (e.g. "vision", "reasoning") next to model names throughout the interface.',
+ defaultValue: true,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.SHOW_MODEL_TAGS,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.ALWAYS_SHOW_AGENTIC_TURNS,
+ label: 'Always show agentic turns in conversation',
+ help: 'Always expand and display agentic loop turns in conversation messages.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY,
+ sync: {
+ serverKey: SETTINGS_KEYS.ALWAYS_SHOW_AGENTIC_TURNS,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.SHOW_BUILD_VERSION,
+ label: 'Show build version information',
+ help: 'Display the current build version in the bottom-right corner of the interface.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DISPLAY
+ }
+ ]
+ },
+ [SETTINGS_SECTION_SLUGS.SAMPLING]: {
+ title: SETTINGS_SECTION_TITLES.SAMPLING,
+ slug: SETTINGS_SECTION_SLUGS.SAMPLING,
+ icon: Funnel,
+ settings: [
+ {
+ key: SETTINGS_KEYS.TEMPERATURE,
+ label: 'Temperature',
+ help: 'Controls the randomness of the generated text by affecting the probability distribution of the output tokens. Higher = more random, lower = more focused.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.SAMPLING,
+ sync: {
+ serverKey: SETTINGS_KEYS.TEMPERATURE,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.DYNATEMP_RANGE,
+ label: 'Dynamic temperature range',
+ help: 'Addon for the temperature sampler. The added value to the range of dynamic temperature, which adjusts probabilities by entropy of tokens.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.SAMPLING,
+ sync: {
+ serverKey: SETTINGS_KEYS.DYNATEMP_RANGE,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.DYNATEMP_EXPONENT,
+ label: 'Dynamic temperature exponent',
+ help: 'Addon for the temperature sampler. Smoothes out the probability redistribution based on the most probable token.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.SAMPLING,
+ sync: {
+ serverKey: SETTINGS_KEYS.DYNATEMP_EXPONENT,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.TOP_K,
+ label: 'Top K',
+ help: 'Keeps only k top tokens.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.SAMPLING,
+ sync: { serverKey: SETTINGS_KEYS.TOP_K, paramType: SyncableParameterType.NUMBER }
+ },
+ {
+ key: SETTINGS_KEYS.TOP_P,
+ label: 'Top P',
+ help: 'Limits tokens to those that together have a cumulative probability of at least p',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.SAMPLING,
+ sync: { serverKey: SETTINGS_KEYS.TOP_P, paramType: SyncableParameterType.NUMBER }
+ },
+ {
+ key: SETTINGS_KEYS.MIN_P,
+ label: 'Min P',
+ help: 'Limits tokens based on the minimum probability for a token to be considered, relative to the probability of the most likely token.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.SAMPLING,
+ sync: { serverKey: SETTINGS_KEYS.MIN_P, paramType: SyncableParameterType.NUMBER }
+ },
+ {
+ key: SETTINGS_KEYS.XTC_PROBABILITY,
+ label: 'XTC probability',
+ help: 'XTC sampler cuts out top tokens; this parameter controls the chance of cutting tokens at all. 0 disables XTC.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.SAMPLING,
+ sync: {
+ serverKey: SETTINGS_KEYS.XTC_PROBABILITY,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.XTC_THRESHOLD,
+ label: 'XTC threshold',
+ help: 'XTC sampler cuts out top tokens; this parameter controls the token probability that is required to cut that token.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.SAMPLING,
+ sync: {
+ serverKey: SETTINGS_KEYS.XTC_THRESHOLD,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.TYP_P,
+ label: 'Typical P',
+ help: 'Sorts and limits tokens based on the difference between log-probability and entropy.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.SAMPLING,
+ sync: { serverKey: SETTINGS_KEYS.TYP_P, paramType: SyncableParameterType.NUMBER }
+ },
+ {
+ key: SETTINGS_KEYS.MAX_TOKENS,
+ label: 'Max tokens',
+ help: 'The maximum number of token per output. Use -1 for infinite (no limit).',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.SAMPLING,
+ sync: {
+ serverKey: SETTINGS_KEYS.MAX_TOKENS,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.SAMPLERS,
+ label: 'Samplers',
+ help: 'The order at which samplers are applied, in simplified way. Default is "top_k;typ_p;top_p;min_p;temperature": top_k->typ_p->top_p->min_p->temperature',
+ defaultValue: '',
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.SAMPLING,
+ sync: { serverKey: SETTINGS_KEYS.SAMPLERS, paramType: SyncableParameterType.STRING }
+ },
+ {
+ key: SETTINGS_KEYS.BACKEND_SAMPLING,
+ label: 'Backend sampling',
+ help: 'Enable backend-based samplers. When enabled, supported samplers run on the accelerator backend for faster sampling.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.SAMPLING,
+ sync: {
+ serverKey: SETTINGS_KEYS.BACKEND_SAMPLING,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ }
+ ]
+ },
+ [SETTINGS_SECTION_SLUGS.PENALTIES]: {
+ title: SETTINGS_SECTION_TITLES.PENALTIES,
+ slug: SETTINGS_SECTION_SLUGS.PENALTIES,
+ icon: AlertTriangle,
+ settings: [
+ {
+ key: SETTINGS_KEYS.REPEAT_LAST_N,
+ label: 'Repeat last N',
+ help: 'Last n tokens to consider for penalizing repetition',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.PENALTIES,
+ sync: {
+ serverKey: SETTINGS_KEYS.REPEAT_LAST_N,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.REPEAT_PENALTY,
+ label: 'Repeat penalty',
+ help: 'Controls the repetition of token sequences in the generated text',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.PENALTIES,
+ sync: {
+ serverKey: SETTINGS_KEYS.REPEAT_PENALTY,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.PRESENCE_PENALTY,
+ label: 'Presence penalty',
+ help: 'Limits tokens based on whether they appear in the output or not.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.PENALTIES,
+ sync: {
+ serverKey: SETTINGS_KEYS.PRESENCE_PENALTY,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.FREQUENCY_PENALTY,
+ label: 'Frequency penalty',
+ help: 'Limits tokens based on how often they appear in the output.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.PENALTIES,
+ sync: {
+ serverKey: SETTINGS_KEYS.FREQUENCY_PENALTY,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.DRY_MULTIPLIER,
+ label: 'DRY multiplier',
+ help: 'DRY sampling reduces repetition in generated text even across long contexts. This parameter sets the DRY sampling multiplier.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.PENALTIES,
+ sync: {
+ serverKey: SETTINGS_KEYS.DRY_MULTIPLIER,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.DRY_BASE,
+ label: 'DRY base',
+ help: 'DRY sampling reduces repetition in generated text even across long contexts. This parameter sets the DRY sampling base value.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.PENALTIES,
+ sync: { serverKey: SETTINGS_KEYS.DRY_BASE, paramType: SyncableParameterType.NUMBER }
+ },
+ {
+ key: SETTINGS_KEYS.DRY_ALLOWED_LENGTH,
+ label: 'DRY allowed length',
+ help: 'DRY sampling reduces repetition in generated text even across long contexts. This parameter sets the allowed length for DRY sampling.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.PENALTIES,
+ sync: {
+ serverKey: SETTINGS_KEYS.DRY_ALLOWED_LENGTH,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.DRY_PENALTY_LAST_N,
+ label: 'DRY penalty last N',
+ help: 'DRY sampling reduces repetition in generated text even across long contexts. This parameter sets DRY penalty for the last n tokens.',
+ defaultValue: undefined,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.PENALTIES,
+ sync: {
+ serverKey: SETTINGS_KEYS.DRY_PENALTY_LAST_N,
+ paramType: SyncableParameterType.NUMBER
+ }
+ }
+ ]
+ },
+ [SETTINGS_SECTION_SLUGS.AGENTIC]: {
+ title: SETTINGS_SECTION_TITLES.AGENTIC,
+ slug: SETTINGS_SECTION_SLUGS.AGENTIC,
+ icon: ListRestart,
+ settings: [
+ {
+ key: SETTINGS_KEYS.AGENTIC_MAX_TURNS,
+ label: 'Agentic turns',
+ help: 'Maximum number of tool execution cycles before stopping (prevents infinite loops).',
+ defaultValue: 10,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.AGENTIC,
+ isPositiveInteger: true,
+ sync: {
+ serverKey: SETTINGS_KEYS.AGENTIC_MAX_TURNS,
+ paramType: SyncableParameterType.NUMBER
+ }
+ },
+ {
+ key: SETTINGS_KEYS.AGENTIC_MAX_TOOL_PREVIEW_LINES,
+ label: 'Max lines per tool preview',
+ help: 'Number of lines shown in tool output previews (last N lines). Only these previews and the final LLM response persist after the agentic loop completes.',
+ defaultValue: 25,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.AGENTIC,
+ isPositiveInteger: true,
+ sync: {
+ serverKey: SETTINGS_KEYS.AGENTIC_MAX_TOOL_PREVIEW_LINES,
+ paramType: SyncableParameterType.NUMBER
+ }
+ }
+ ]
+ },
+ [SETTINGS_SECTION_SLUGS.DEVELOPER]: {
+ title: SETTINGS_SECTION_TITLES.DEVELOPER,
+ slug: SETTINGS_SECTION_SLUGS.DEVELOPER,
+ icon: Code,
+ settings: [
+ {
+ key: SETTINGS_KEYS.PRE_ENCODE_CONVERSATION,
+ label: 'Pre-fill KV cache after response',
+ help: 'After each response, re-submit the conversation to pre-fill the server KV cache. Makes the next turn faster since the prompt is already encoded while you read the response.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DEVELOPER
+ },
+ {
+ key: SETTINGS_KEYS.DISABLE_REASONING_PARSING,
+ label: 'Disable reasoning content parsing',
+ help: 'Send reasoning_format=none so the server returns thinking tokens inline instead of extracting them into a separate field.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DEVELOPER
+ },
+ {
+ key: SETTINGS_KEYS.EXCLUDE_REASONING_FROM_CONTEXT,
+ label: 'Exclude reasoning from context',
+ help: 'Strip thinking from previous messages before sending. When off, thinking is sent back via the reasoning_content field so the model sees its own chain-of-thought across turns.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DEVELOPER,
+ sync: {
+ serverKey: SETTINGS_KEYS.EXCLUDE_REASONING_FROM_CONTEXT,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.ENABLE_THINKING,
+ label: 'Enable thinking',
+ help: 'Enable model thinking/reasoning for each request. When off, the model will skip the thinking phase and go straight to the response.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DEVELOPER
+ },
+ {
+ key: SETTINGS_KEYS.SHOW_RAW_OUTPUT_SWITCH,
+ label: 'Enable raw output toggle',
+ help: 'Show toggle button to display messages as plain text instead of Markdown-formatted content',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DEVELOPER,
+ sync: {
+ serverKey: SETTINGS_KEYS.SHOW_RAW_OUTPUT_SWITCH,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.JS_SANDBOX_ENABLED,
+ label: 'JavaScript sandbox tool',
+ help: 'Expose a run_javascript tool to the model. Code runs in a Web Worker inside a sandboxed iframe with an opaque origin, isolated from the WebUI and its API, with a hard timeout.',
+ defaultValue: false,
+ type: SettingsFieldType.CHECKBOX,
+ section: SETTINGS_SECTION_SLUGS.DEVELOPER
+ },
+ {
+ key: SETTINGS_KEYS.CUSTOM_JSON,
+ label: 'Custom JSON',
+ help: 'Custom JSON parameters to send to the API. Must be valid JSON format.',
+ defaultValue: '',
+ type: SettingsFieldType.TEXTAREA,
+ section: SETTINGS_SECTION_SLUGS.DEVELOPER
+ },
+ {
+ key: SETTINGS_KEYS.CUSTOM_CSS,
+ label: 'Custom CSS',
+ help: 'CSS injected into the page at runtime. Set it here, or ship it server side via the --ui-config customCss field.',
+ defaultValue: '',
+ type: SettingsFieldType.TEXTAREA,
+ section: SETTINGS_SECTION_SLUGS.DEVELOPER,
+ sync: {
+ serverKey: SETTINGS_KEYS.CUSTOM_CSS,
+ paramType: SyncableParameterType.STRING
+ }
+ }
+ ]
+ },
+ [SETTINGS_SECTION_SLUGS.MCP]: {
+ title: SETTINGS_SECTION_TITLES.MCP,
+ slug: SETTINGS_SECTION_SLUGS.MCP,
+ icon: McpLogo,
+ settings: [
+ {
+ key: SETTINGS_KEYS.MCP_REQUEST_TIMEOUT_SECONDS,
+ label: 'Request timeout (seconds)',
+ help: 'Default timeout for individual MCP tool calls. Can be overridden per server.',
+ defaultValue: DEFAULT_MCP_CONFIG.requestTimeoutSeconds,
+ type: SettingsFieldType.INPUT,
+ section: SETTINGS_SECTION_SLUGS.MCP,
+ isPositiveInteger: true
+ }
+ ]
+ }
+} as const;
+
+const NON_UI_SETTINGS: SettingsEntry[] = [
+ {
+ key: SETTINGS_KEYS.SHOW_SYSTEM_MESSAGE,
+ label: 'Show system message',
+ help: 'Display the system message at the top of each conversation.',
+ defaultValue: true,
+ type: SettingsFieldType.CHECKBOX,
+ sync: {
+ serverKey: SETTINGS_KEYS.SHOW_SYSTEM_MESSAGE,
+ paramType: SyncableParameterType.BOOLEAN
+ }
+ },
+ {
+ key: SETTINGS_KEYS.MCP_SERVERS,
+ label: 'MCP servers',
+ help: 'Configure MCP servers as a JSON list. Use the form in the MCP Client settings section to edit.',
+ defaultValue: JSON.stringify(RECOMMENDED_MCP_SERVERS),
+ type: SettingsFieldType.INPUT,
+ sync: { serverKey: SETTINGS_KEYS.MCP_SERVERS, paramType: SyncableParameterType.STRING }
+ },
+ {
+ key: SETTINGS_KEYS.MCP_DEFAULT_SERVER_OVERRIDES,
+ label: 'MCP default server overrides',
+ help: 'Per-server enable/disable defaults inherited by new chats. JSON-serialized list of {serverId, enabled} entries.',
+ defaultValue: '[]',
+ type: SettingsFieldType.INPUT
+ }
+ // {
+ // key: SETTINGS_KEYS.PY_INTERPRETER_ENABLED,
+ // label: 'Python interpreter enabled',
+ // help: 'Enable Python interpreter using Pyodide. Allows running Python code in markdown code blocks.',
+ // defaultValue: false,
+ // type: SettingsFieldType.CHECKBOX,
+ // isExperimental: true,
+ // sync: { serverKey: SETTINGS_KEYS.PY_INTERPRETER_ENABLED, paramType: SyncableParameterType.BOOLEAN }
+ // }
+];
+
+function getAllSettings(): SettingsEntry[] {
+ const result: SettingsEntry[] = [];
+ for (const section of Object.values(SETTINGS_REGISTRY)) {
+ result.push(...section.settings);
+ }
+ result.push(...NON_UI_SETTINGS);
+ return result;
+}
+
+/** Flat config object stored in localStorage. */
+export const SETTING_CONFIG_DEFAULT: Record = Object.fromEntries(
+ getAllSettings().map((s) => [s.key, s.defaultValue])
+) as Record;
+
+/** Help text for every setting (including non-UI). */
+export const SETTING_CONFIG_INFO: Record = Object.fromEntries(
+ getAllSettings().map((s) => [s.key, s.help])
+) as Record;
+
+/** Theme select options. */
+export const SETTINGS_COLOR_MODES_CONFIG = COLOR_MODE_OPTIONS;
+
+export type { SettingsSectionTitle } from '$lib/types';
+export type { SettingsSection } from '$lib/types';
+
+/** Sidebar sections + field configs (as consumed by UI). */
+export const SETTINGS_CHAT_SECTIONS: SettingsSection[] = [
+ ...Object.values(SETTINGS_REGISTRY).map((section) => ({
+ title: section.title,
+ slug: section.slug,
+ icon: section.icon,
+ fields: section.settings.map((s) => ({
+ key: s.key,
+ label: s.label,
+ type: s.type,
+ isExperimental: s.isExperimental,
+ isPositiveInteger: s.isPositiveInteger,
+ help: s.help,
+ options: s.options
+ }))
+ })),
+ ...STANDALONE_SECTIONS
+];
+
+/** INPUT-type settings whose value is a number. */
+export const NUMERIC_FIELDS = getAllSettings()
+ .filter((s) => s.type === SettingsFieldType.INPUT && typeof s.defaultValue !== 'string')
+ .map((s) => s.key) as readonly string[];
+
+/** Numeric fields clamped to ≥ 1 and rounded. */
+export const POSITIVE_INTEGER_FIELDS = getAllSettings()
+ .filter((s) => s.isPositiveInteger)
+ .map((s) => s.key) as readonly string[];
+
+/** Derived for the parameter sync service. */
+export const SYNCABLE_PARAMETERS: SyncableParameter[] = getAllSettings()
+ .filter((s) => s.sync !== undefined)
+ .map((s) => ({
+ key: s.key,
+ serverKey: s.sync!.serverKey,
+ type: s.sync!.paramType,
+ canSync: true
+ }));
+
+export const SETTINGS_FALLBACK_EXIT_ROUTE = ROUTES.START;
+
+export { SETTINGS_KEYS } from './settings-keys';
diff --git a/llama.cpp/tools/ui/src/lib/constants/sse.ts b/llama.cpp/tools/ui/src/lib/constants/sse.ts
new file mode 100644
index 0000000000000000000000000000000000000000..0eb4b6edeea2f03171ab11719041d3bdd90d1587
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/sse.ts
@@ -0,0 +1,16 @@
+/**
+ * Server-sent events wire format, shared by the chat stream and the
+ * /models/sse status feed (text/event-stream).
+ */
+
+// blank line between two events
+export const SSE_RECORD_SEPARATOR = '\n\n';
+
+// line break inside an event
+export const SSE_LINE_SEPARATOR = '\n';
+
+// data field prefix, the value follows after an optional space
+export const SSE_DATA_PREFIX = 'data:';
+
+// end-of-stream marker on the chat completion stream
+export const SSE_DONE_MARKER = '[DONE]';
diff --git a/llama.cpp/tools/ui/src/lib/constants/storage.ts b/llama.cpp/tools/ui/src/lib/constants/storage.ts
new file mode 100644
index 0000000000000000000000000000000000000000..d0c5f2effe5ed813a1673c0b4c5eff800b130249
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/storage.ts
@@ -0,0 +1,55 @@
+/**
+ * Storage-related constants (localStorage, IndexedDB).
+ *
+ * Centralized to ensure consistency across the app and simplify future
+ * name changes.
+ */
+
+/** Name prefix for all localStorage keys */
+export const STORAGE_APP_NAME = 'LlamaUi';
+
+/** Deprecated localStorage key prefix (old app name) */
+export const STORAGE_APP_NAME_DEPRECATED = 'LlamaCppWebui';
+
+/** @deprecated Deprecated IndexedDB name — will be removed after all users have migrated */
+export const DB_APP_NAME_DEPRECATED = 'LlamacppWebui';
+
+export const ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.alwaysAllowedTools`;
+export const CONFIG_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.config`;
+export const DISABLED_TOOLS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.disabledTools`;
+
+/** Disabled tools keyed by stable selection identity, no migration from the name based key */
+export const DISABLED_TOOL_KEYS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.disabledToolKeys`;
+export const FAVORITE_MODELS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.favoriteModels`;
+export const THINKING_ENABLED_DEFAULT_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.thinkingEnabledDefault`;
+export const REASONING_EFFORT_DEFAULT_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.reasoningEffortDefault`;
+/** Set when user has interacted with the MCP server recommendations dialog (checked servers, added custom server, or dismissed) */
+export const MCP_SERVERS_ADDED_TO_CHAT_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.mcpServersSetupDone`;
+export const USER_OVERRIDES_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.userOverrides`;
+
+/** Key prefix for per-conversation resumable stream state, conversationId is appended */
+export const STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX = `${STORAGE_APP_NAME}.streamResume.`;
+
+// Deprecated old key names (kept for backward compat while users migrate)
+/** @deprecated Use {@link ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY} instead */
+export const DEPRECATED_ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME_DEPRECATED}.alwaysAllowedTools`;
+/** @deprecated Use {@link CONFIG_LOCALSTORAGE_KEY} instead */
+export const DEPRECATED_CONFIG_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME_DEPRECATED}.config`;
+/** @deprecated Use {@link DISABLED_TOOLS_LOCALSTORAGE_KEY} instead */
+export const DEPRECATED_DISABLED_TOOLS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME_DEPRECATED}.disabledTools`;
+/** @deprecated Use {@link FAVORITE_MODELS_LOCALSTORAGE_KEY} instead */
+export const DEPRECATED_FAVORITE_MODELS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME_DEPRECATED}.favoriteModels`;
+/** @deprecated Use {@link USER_OVERRIDES_LOCALSTORAGE_KEY} instead */
+export const DEPRECATED_USER_OVERRIDES_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME_DEPRECATED}.userOverrides`;
+
+/** Build version stored in localStorage for non-PWA update detection */
+export const BUILD_VERSION_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.buildVersion`;
+
+/** Maps new keys to their deprecated fallback keys */
+export const NEW_TO_DEPRECATED_MAP: Record = {
+ [ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY]: DEPRECATED_ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY,
+ [CONFIG_LOCALSTORAGE_KEY]: DEPRECATED_CONFIG_LOCALSTORAGE_KEY,
+ [DISABLED_TOOLS_LOCALSTORAGE_KEY]: DEPRECATED_DISABLED_TOOLS_LOCALSTORAGE_KEY,
+ [FAVORITE_MODELS_LOCALSTORAGE_KEY]: DEPRECATED_FAVORITE_MODELS_LOCALSTORAGE_KEY,
+ [USER_OVERRIDES_LOCALSTORAGE_KEY]: DEPRECATED_USER_OVERRIDES_LOCALSTORAGE_KEY
+};
diff --git a/llama.cpp/tools/ui/src/lib/constants/stream.ts b/llama.cpp/tools/ui/src/lib/constants/stream.ts
new file mode 100644
index 0000000000000000000000000000000000000000..67951ee95301af6b0880f702accc9a12fdd96f2a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/stream.ts
@@ -0,0 +1,3 @@
+// grace window after a visibilitychange before we kick a reader whose socket likely died
+// while the tab was hidden. covers brief background pauses without thrashing live streams
+export const STREAM_VISIBILITY_KICK_MS = 3000;
diff --git a/llama.cpp/tools/ui/src/lib/constants/supported-file-types.ts b/llama.cpp/tools/ui/src/lib/constants/supported-file-types.ts
new file mode 100644
index 0000000000000000000000000000000000000000..cbe780aa57e7e623aff9076cb83c3cf0c849f3a3
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/supported-file-types.ts
@@ -0,0 +1,234 @@
+/**
+ * Comprehensive dictionary of all supported file types in llama-ui
+ * Organized by category with TypeScript enums for better type safety
+ */
+
+import {
+ FileExtensionAudio,
+ FileExtensionImage,
+ FileExtensionPdf,
+ FileExtensionText,
+ FileTypeAudio,
+ FileTypeImage,
+ FileTypePdf,
+ FileTypeText,
+ MimeTypeAudio,
+ MimeTypeVideo,
+ MimeTypeImage,
+ MimeTypeApplication,
+ MimeTypeText
+} from '$lib/enums';
+import { FileExtensionVideo, FileTypeVideo } from '$lib/enums/files.enums';
+
+// File type configuration using enums
+export const AUDIO_FILE_TYPES = {
+ [FileTypeAudio.MP3]: {
+ extensions: [FileExtensionAudio.MP3],
+ mimeTypes: [MimeTypeAudio.MP3_MPEG, MimeTypeAudio.MP3]
+ },
+ [FileTypeAudio.WAV]: {
+ extensions: [FileExtensionAudio.WAV],
+ mimeTypes: [MimeTypeAudio.WAV]
+ }
+} as const;
+
+export const VIDEO_FILE_TYPES = {
+ [FileTypeVideo.MP4]: {
+ extensions: [FileExtensionVideo.MP4],
+ mimeTypes: [MimeTypeVideo.MP4]
+ },
+ [FileTypeVideo.OGG]: {
+ extensions: [FileExtensionVideo.OGG],
+ mimeTypes: [MimeTypeVideo.OGG]
+ }
+} as const;
+
+export const IMAGE_FILE_TYPES = {
+ [FileTypeImage.JPEG]: {
+ extensions: [FileExtensionImage.JPG, FileExtensionImage.JPEG],
+ mimeTypes: [MimeTypeImage.JPEG]
+ },
+ [FileTypeImage.PNG]: {
+ extensions: [FileExtensionImage.PNG],
+ mimeTypes: [MimeTypeImage.PNG]
+ },
+ [FileTypeImage.GIF]: {
+ extensions: [FileExtensionImage.GIF],
+ mimeTypes: [MimeTypeImage.GIF]
+ },
+ [FileTypeImage.WEBP]: {
+ extensions: [FileExtensionImage.WEBP],
+ mimeTypes: [MimeTypeImage.WEBP]
+ },
+ [FileTypeImage.SVG]: {
+ extensions: [FileExtensionImage.SVG],
+ mimeTypes: [MimeTypeImage.SVG]
+ },
+ [FileTypeImage.HEIC]: {
+ extensions: [FileExtensionImage.HEIC, FileExtensionImage.HEIF],
+ mimeTypes: [MimeTypeImage.HEIC, MimeTypeImage.HEIF]
+ }
+} as const;
+
+export const PDF_FILE_TYPES = {
+ [FileTypePdf.PDF]: {
+ extensions: [FileExtensionPdf.PDF],
+ mimeTypes: [MimeTypeApplication.PDF]
+ }
+} as const;
+
+export const TEXT_FILE_TYPES = {
+ [FileTypeText.PLAIN_TEXT]: {
+ extensions: [FileExtensionText.TXT],
+ mimeTypes: [MimeTypeText.PLAIN]
+ },
+ [FileTypeText.MARKDOWN]: {
+ extensions: [FileExtensionText.MD],
+ mimeTypes: [MimeTypeText.MARKDOWN]
+ },
+ [FileTypeText.ASCIIDOC]: {
+ extensions: [FileExtensionText.ADOC],
+ mimeTypes: [MimeTypeText.ASCIIDOC]
+ },
+ [FileTypeText.JAVASCRIPT]: {
+ extensions: [FileExtensionText.JS],
+ mimeTypes: [MimeTypeText.JAVASCRIPT, MimeTypeText.JAVASCRIPT_APP]
+ },
+ [FileTypeText.TYPESCRIPT]: {
+ extensions: [FileExtensionText.TS],
+ mimeTypes: [MimeTypeText.TYPESCRIPT]
+ },
+ [FileTypeText.JSX]: {
+ extensions: [FileExtensionText.JSX],
+ mimeTypes: [MimeTypeText.JSX]
+ },
+ [FileTypeText.TSX]: {
+ extensions: [FileExtensionText.TSX],
+ mimeTypes: [MimeTypeText.TSX]
+ },
+ [FileTypeText.CSS]: {
+ extensions: [FileExtensionText.CSS],
+ mimeTypes: [MimeTypeText.CSS]
+ },
+ [FileTypeText.HTML]: {
+ extensions: [FileExtensionText.HTML, FileExtensionText.HTM],
+ mimeTypes: [MimeTypeText.HTML]
+ },
+ [FileTypeText.JSON]: {
+ extensions: [FileExtensionText.JSON],
+ mimeTypes: [MimeTypeText.JSON]
+ },
+ [FileTypeText.XML]: {
+ extensions: [FileExtensionText.XML],
+ mimeTypes: [MimeTypeText.XML_TEXT, MimeTypeText.XML_APP]
+ },
+ [FileTypeText.YAML]: {
+ extensions: [FileExtensionText.YAML, FileExtensionText.YML],
+ mimeTypes: [MimeTypeText.YAML_TEXT, MimeTypeText.YAML_APP]
+ },
+ [FileTypeText.CSV]: {
+ extensions: [FileExtensionText.CSV],
+ mimeTypes: [MimeTypeText.CSV]
+ },
+ [FileTypeText.LOG]: {
+ extensions: [FileExtensionText.LOG],
+ mimeTypes: [MimeTypeText.PLAIN]
+ },
+ [FileTypeText.PYTHON]: {
+ extensions: [FileExtensionText.PY],
+ mimeTypes: [MimeTypeText.PYTHON]
+ },
+ [FileTypeText.JAVA]: {
+ extensions: [FileExtensionText.JAVA],
+ mimeTypes: [MimeTypeText.JAVA]
+ },
+ [FileTypeText.CPP]: {
+ extensions: [
+ FileExtensionText.CPP,
+ FileExtensionText.C,
+ FileExtensionText.H,
+ FileExtensionText.HPP
+ ],
+ mimeTypes: [MimeTypeText.CPP_SRC, MimeTypeText.CPP_HDR, MimeTypeText.C_SRC, MimeTypeText.C_HDR]
+ },
+ [FileTypeText.PHP]: {
+ extensions: [FileExtensionText.PHP],
+ mimeTypes: [MimeTypeText.PHP]
+ },
+ [FileTypeText.RUBY]: {
+ extensions: [FileExtensionText.RB],
+ mimeTypes: [MimeTypeText.RUBY]
+ },
+ [FileTypeText.GO]: {
+ extensions: [FileExtensionText.GO],
+ mimeTypes: [MimeTypeText.GO]
+ },
+ [FileTypeText.RUST]: {
+ extensions: [FileExtensionText.RS],
+ mimeTypes: [MimeTypeText.RUST]
+ },
+ [FileTypeText.SHELL]: {
+ extensions: [FileExtensionText.SH, FileExtensionText.BAT],
+ mimeTypes: [MimeTypeText.SHELL, MimeTypeText.BAT]
+ },
+ [FileTypeText.SQL]: {
+ extensions: [FileExtensionText.SQL],
+ mimeTypes: [MimeTypeText.SQL]
+ },
+ [FileTypeText.R]: {
+ extensions: [FileExtensionText.R],
+ mimeTypes: [MimeTypeText.R]
+ },
+ [FileTypeText.SCALA]: {
+ extensions: [FileExtensionText.SCALA],
+ mimeTypes: [MimeTypeText.SCALA]
+ },
+ [FileTypeText.KOTLIN]: {
+ extensions: [FileExtensionText.KT],
+ mimeTypes: [MimeTypeText.KOTLIN]
+ },
+ [FileTypeText.SWIFT]: {
+ extensions: [FileExtensionText.SWIFT],
+ mimeTypes: [MimeTypeText.SWIFT]
+ },
+ [FileTypeText.DART]: {
+ extensions: [FileExtensionText.DART],
+ mimeTypes: [MimeTypeText.DART]
+ },
+ [FileTypeText.VUE]: {
+ extensions: [FileExtensionText.VUE],
+ mimeTypes: [MimeTypeText.VUE]
+ },
+ [FileTypeText.SVELTE]: {
+ extensions: [FileExtensionText.SVELTE],
+ mimeTypes: [MimeTypeText.SVELTE]
+ },
+ [FileTypeText.LATEX]: {
+ extensions: [FileExtensionText.TEX],
+ mimeTypes: [MimeTypeText.LATEX, MimeTypeText.TEX, MimeTypeText.TEX_APP]
+ },
+ [FileTypeText.BIBTEX]: {
+ extensions: [FileExtensionText.BIB],
+ mimeTypes: [MimeTypeText.BIBTEX]
+ },
+ [FileTypeText.CUDA]: {
+ extensions: [FileExtensionText.CU, FileExtensionText.CUH],
+ mimeTypes: [MimeTypeText.CUDA]
+ },
+ [FileTypeText.VULKAN]: {
+ extensions: [FileExtensionText.COMP],
+ mimeTypes: [MimeTypeText.PLAIN]
+ },
+ [FileTypeText.HASKELL]: {
+ extensions: [FileExtensionText.HS],
+ mimeTypes: [MimeTypeText.HASKELL]
+ },
+ [FileTypeText.CSHARP]: {
+ extensions: [FileExtensionText.CS],
+ mimeTypes: [MimeTypeText.CSHARP]
+ },
+ [FileTypeText.PROPERTIES]: {
+ extensions: [FileExtensionText.PROPERTIES],
+ mimeTypes: [MimeTypeText.PROPERTIES]
+ }
+} as const;
diff --git a/llama.cpp/tools/ui/src/lib/constants/svg-blocks.ts b/llama.cpp/tools/ui/src/lib/constants/svg-blocks.ts
new file mode 100644
index 0000000000000000000000000000000000000000..ccca9376c68f52bade2e22825322b40c47c63535
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/svg-blocks.ts
@@ -0,0 +1,49 @@
+export const SVG_WRAPPER_CLASS = 'svg-block-wrapper';
+export const SVG_SCROLL_CONTAINER_CLASS = 'svg-scroll-container';
+export const SVG_BLOCK_CLASS = 'svg-block';
+
+export const SVG_LANGUAGE = 'svg';
+export const XML_LANGUAGE = 'xml';
+export const SVG_TAG_PREFIX = ' stays scoped to that root and can not reach the page.
+ */
+export const SVG_SANITIZE_CONFIG = {
+ USE_PROFILES: { svg: true, svgFilters: true },
+ FORBID_TAGS: ['foreignObject', 'script']
+};
+
+/**
+ * Shadow root style for an inline svg block. Mirrors the centered, padded
+ * sizing the light dom used before the svg moved behind a shadow boundary.
+ */
+export const SVG_INLINE_SHADOW_STYLE =
+ ':host{display:block;width:100%;text-align:center}svg{display:block;margin:0 auto;width:auto;height:auto;max-width:100%;max-height:70vh;min-height:8rem;padding:3rem 1rem}';
+
+/**
+ * Shadow root style for the zoom dialog svg. Lets the svg grow past its
+ * intrinsic size so pan and zoom have room to work.
+ */
+export const SVG_DIALOG_SHADOW_STYLE =
+ ':host{display:inline-block}svg{min-height:min(50vh,12rem);min-width:min(80vw,20rem);max-width:none;max-height:none;height:auto;width:auto;display:block}';
diff --git a/llama.cpp/tools/ui/src/lib/constants/table-html-restorer.ts b/llama.cpp/tools/ui/src/lib/constants/table-html-restorer.ts
new file mode 100644
index 0000000000000000000000000000000000000000..e5d5b120114206fef3fef0ac8f5b2dbd9d1b772d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/table-html-restorer.ts
@@ -0,0 +1,20 @@
+/**
+ * Matches , , tags (case-insensitive).
+ * Used to detect line breaks in table cell text content.
+ */
+export const BR_PATTERN = / /gi;
+
+/**
+ * Matches a complete block.
+ * Captures the inner content (group 1) for further extraction.
+ * Case-insensitive, allows multiline content.
+ */
+export const LIST_PATTERN = /^([\s\S]*)<\/ul>$/i;
+
+/**
+ * Matches individual ... elements within a list.
+ * Captures the inner content (group 1) of each list item.
+ * Non-greedy to handle multiple consecutive items.
+ * Case-insensitive, allows multiline content.
+ */
+export const LI_PATTERN = /([\s\S]*?)<\/li>/gi;
diff --git a/llama.cpp/tools/ui/src/lib/constants/title-generation.ts b/llama.cpp/tools/ui/src/lib/constants/title-generation.ts
new file mode 100644
index 0000000000000000000000000000000000000000..48ca2217a27d5f35a918c4c4c2e815abe27e2db1
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/title-generation.ts
@@ -0,0 +1,9 @@
+/* Title generation constants */
+export const TITLE_GENERATION = {
+ MIN_LENGTH: 3,
+ FALLBACK: 'New Chat',
+ DEFAULT_PROMPT:
+ 'Based on the following interaction, generate a short, concise title (maximum 6-8 words) that captures the main topic. Return ONLY the title text, nothing else. Do not use quotes.\n\nUser: {{USER}}\n\nAssistant: {{ASSISTANT}}\n\nTitle:',
+ PREFIX_PATTERN: /^(Title:|Subject:|Topic:)\s*/i,
+ QUOTE_PATTERN: /^["]|["]$/g
+} as const;
diff --git a/llama.cpp/tools/ui/src/lib/constants/tools.ts b/llama.cpp/tools/ui/src/lib/constants/tools.ts
new file mode 100644
index 0000000000000000000000000000000000000000..4d9385f9f5ca12d0bf8726dc13f4915e552ae599
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/tools.ts
@@ -0,0 +1,13 @@
+import { ToolSource } from '$lib/enums/tools.enums';
+
+export const TOOL_GROUP_LABELS = {
+ [ToolSource.BUILTIN]: 'Built-in',
+ [ToolSource.CUSTOM]: 'JSON Schema',
+ [ToolSource.FRONTEND]: 'Browser'
+} as const;
+
+export const TOOL_SERVER_LABELS = {
+ [ToolSource.BUILTIN]: 'Built-in Tools',
+ [ToolSource.CUSTOM]: 'Custom Tools',
+ [ToolSource.FRONTEND]: 'Browser Tools'
+} as const;
diff --git a/llama.cpp/tools/ui/src/lib/constants/tooltip-config.ts b/llama.cpp/tools/ui/src/lib/constants/tooltip-config.ts
new file mode 100644
index 0000000000000000000000000000000000000000..ad76ab35226615e8586535bdb3aabc64af860c21
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/tooltip-config.ts
@@ -0,0 +1 @@
+export const TOOLTIP_DELAY_DURATION = 500;
diff --git a/llama.cpp/tools/ui/src/lib/constants/ui.ts b/llama.cpp/tools/ui/src/lib/constants/ui.ts
new file mode 100644
index 0000000000000000000000000000000000000000..f2d3e95e01c648ef5dfaf5e42293b19f2a3c05df
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/ui.ts
@@ -0,0 +1,37 @@
+import { Search, Settings, SquarePen } from '@lucide/svelte';
+import McpLogo from '$lib/components/app/mcp/McpLogo.svelte';
+import type { Component } from 'svelte';
+import { ROUTES } from './routes';
+
+export const FORK_TREE_DEPTH_PADDING = 8;
+export const SYSTEM_MESSAGE_PLACEHOLDER = 'System message';
+
+export const ICON_STRIP_TRANSITION_DURATION = 150;
+export const ICON_STRIP_TRANSITION_DELAY_MULTIPLIER = 50;
+
+export interface DesktopIconStripItem {
+ icon: Component;
+ tooltip: string;
+ route?: string;
+ activeRouteId?: string;
+ activeRoutePrefix?: string;
+ activeUrlIncludes?: string;
+ keys?: string[];
+}
+
+export const SIDEBAR_ACTIONS_ITEMS: DesktopIconStripItem[] = [
+ { icon: SquarePen, tooltip: 'New chat', route: ROUTES.NEW_CHAT, keys: ['shift', 'cmd', 'o'] },
+ { icon: Search, tooltip: 'Search', keys: ['cmd', 'k'] },
+ {
+ icon: McpLogo,
+ tooltip: 'MCP Servers',
+ route: ROUTES.MCP_SERVERS,
+ activeRouteId: '/mcp-servers'
+ },
+ {
+ icon: Settings,
+ tooltip: 'Settings',
+ route: `${ROUTES.SETTINGS}/general`,
+ activeUrlIncludes: '#/settings'
+ }
+];
diff --git a/llama.cpp/tools/ui/src/lib/constants/uri-template.ts b/llama.cpp/tools/ui/src/lib/constants/uri-template.ts
new file mode 100644
index 0000000000000000000000000000000000000000..9b44d6ed375615b5af4525a369d803ced91f6740
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/uri-template.ts
@@ -0,0 +1,60 @@
+/**
+ * URI Template constants for RFC 6570 template processing.
+ */
+
+/** URI scheme separator */
+export const URI_SCHEME_SEPARATOR = '://';
+
+/** Regex to match template expressions like {var}, {+var}, {#var}, {/var} */
+export const TEMPLATE_EXPRESSION_REGEX = /\{([+#./;?&]?)([^}]+)\}/g;
+
+/** RFC 6570 URI template operators */
+export const URI_TEMPLATE_OPERATORS = {
+ /** Simple string expansion (default) */
+ SIMPLE: '',
+ /** Reserved expansion */
+ RESERVED: '+',
+ /** Fragment expansion */
+ FRAGMENT: '#',
+ /** Path segment expansion */
+ PATH_SEGMENT: '/',
+ /** Label expansion */
+ LABEL: '.',
+ /** Path-style parameters */
+ PATH_PARAM: ';',
+ /** Form-style query */
+ FORM_QUERY: '?',
+ /** Form-style query continuation */
+ FORM_CONTINUATION: '&'
+} as const;
+
+/** URI template separators used in expansion */
+export const URI_TEMPLATE_SEPARATORS = {
+ /** Comma separator for list expansion */
+ COMMA: ',',
+ /** Slash separator for path segments */
+ SLASH: '/',
+ /** Period separator for label expansion */
+ PERIOD: '.',
+ /** Semicolon separator for path parameters */
+ SEMICOLON: ';',
+ /** Question mark prefix for query string */
+ QUERY_PREFIX: '?',
+ /** Ampersand prefix for query continuation */
+ QUERY_CONTINUATION: '&'
+} as const;
+
+/** Maximum number of leading slashes to strip during URI normalization */
+export const MAX_LEADING_SLASHES_TO_STRIP = 3;
+
+/** Regex to strip explode modifier (*) from variable names */
+export const VARIABLE_EXPLODE_MODIFIER_REGEX = /[*]$/;
+
+/** Regex to strip prefix modifier (:N) from variable names */
+export const VARIABLE_PREFIX_MODIFIER_REGEX = /:[\d]+$/;
+
+/** Regex to strip one or more leading slashes */
+export const LEADING_SLASHES_REGEX = /^\/+/;
+
+/** Regex to match base64-encoded image URIs (format: "data:image/[media type];base64,[data]")*/
+export const BASE64_IMAGE_URI_REGEX = /^data:(image\/[a-z0-9.\-+]+);base64/;
diff --git a/llama.cpp/tools/ui/src/lib/constants/url.ts b/llama.cpp/tools/ui/src/lib/constants/url.ts
new file mode 100644
index 0000000000000000000000000000000000000000..0afb9deccb8a3adb4862ae3fbc70d6670a792dec
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/url.ts
@@ -0,0 +1,186 @@
+const STD = ['com', 'net', 'org', 'gov', 'edu'] as const;
+
+const STD_MIL = [...STD, 'mil'] as const;
+
+const ccTLD_PREFIXES: Record = {
+ // --- Standard 5 only ---
+ ar: STD,
+ bd: STD,
+ bg: STD,
+ cn: STD_MIL,
+ eg: STD,
+ gr: STD,
+ hk: STD,
+ hr: STD,
+ lk: STD,
+ mx: STD_MIL,
+ my: STD_MIL,
+ ng: STD,
+ ph: STD,
+ pk: STD,
+ pl: STD,
+ ro: STD,
+ ru: STD,
+ sa: STD,
+ si: STD,
+ tr: STD,
+ tw: STD,
+ ua: STD,
+ ve: STD,
+
+ au: [...STD_MIL, 'id', 'asn', 'csiro'],
+ br: [
+ ...STD_MIL,
+ 'art',
+ 'eco',
+ 'eng',
+ 'inf',
+ 'med',
+ 'psi',
+ 'tmp',
+ 'etc',
+ 'adm',
+ 'adv',
+ 'arq',
+ 'bio',
+ 'bmd',
+ 'cim',
+ 'cng',
+ 'cnt',
+ 'coop',
+ 'ecn',
+ 'esp',
+ 'far',
+ 'fm',
+ 'fnd',
+ 'fot',
+ 'fst',
+ 'g12',
+ 'ggf',
+ 'imb',
+ 'ind',
+ 'jor',
+ 'jus',
+ 'leg',
+ 'lel',
+ 'mat',
+ 'mp',
+ 'mus',
+ 'not',
+ 'ntr',
+ 'odo',
+ 'ppg',
+ 'pro',
+ 'psc',
+ 'qsl',
+ 'rec',
+ 'slg',
+ 'srv',
+ 'trd',
+ 'tur',
+ 'tv',
+ 'vet',
+ 'vlog',
+ 'wiki',
+ 'zlg'
+ ],
+ id: [...STD_MIL, 'co', 'go', 'or', 'web', 'sch'],
+ in: [...STD_MIL, 'co', 'gen', 'ind', 'firm', 'ernet', 'nic'],
+ kr: [...STD_MIL, 'co', 'go', 'or', 'ac', 're'],
+ nz: [
+ ...STD_MIL,
+ 'co',
+ 'gen',
+ 'geek',
+ 'kiwi',
+ 'maori',
+ 'school',
+ 'govt',
+ 'health',
+ 'iwi',
+ 'parliament'
+ ],
+ sg: [...STD, 'per'],
+ th: ['co', 'go', 'or', 'in', 'ac', 'mi', 'net'],
+
+ ae: ['co', 'net', 'org', 'gov', 'ac', 'sch'],
+ hu: ['co', 'net', 'org', 'gov', 'edu'],
+ il: ['co', 'net', 'org', 'gov', 'ac', 'muni'],
+ jp: ['ac', 'ad', 'co', 'ed', 'go', 'gr', 'lg', 'ne', 'or'],
+ ke: ['co', 'or', 'ne', 'go', 'ac', 'sc'],
+ rs: ['co', 'net', 'org', 'gov', 'edu'],
+ uk: ['co', 'org', 'net', 'ac', 'gov', 'mil', 'nhs', 'police', 'mod', 'ltd', 'plc', 'me', 'sch'],
+ za: ['co', 'org', 'net', 'web', 'law', 'mil']
+};
+
+const WILDCARD_BASES: Record = {
+ br: ['nom', 'blog'],
+ jp: [
+ 'kobe',
+ 'kyoto',
+ 'nagoya',
+ 'osaka',
+ 'sapporo',
+ 'sendai',
+ 'tokyo',
+ 'yokohama',
+ 'aichi',
+ 'akita',
+ 'aomori',
+ 'chiba',
+ 'ehime',
+ 'fukui',
+ 'fukuoka',
+ 'fukushima',
+ 'gifu',
+ 'gunma',
+ 'hiroshima',
+ 'hokkaido',
+ 'hyogo',
+ 'ibaraki',
+ 'ishikawa',
+ 'iwate',
+ 'kagawa',
+ 'kagoshima',
+ 'kanagawa',
+ 'kochi',
+ 'kumamoto',
+ 'mie',
+ 'miyagi',
+ 'miyazaki',
+ 'nagano',
+ 'nara',
+ 'niigata',
+ 'oita',
+ 'okayama',
+ 'okinawa',
+ 'saga',
+ 'saitama',
+ 'shiga',
+ 'shimane',
+ 'shizuoka',
+ 'tochigi',
+ 'tokushima',
+ 'tottori',
+ 'toyama',
+ 'wakayama',
+ 'yamagata',
+ 'yamaguchi',
+ 'yamanashi'
+ ]
+};
+
+function buildSuffixSet(suffixes: Record): Set {
+ const set = new Set();
+
+ for (const [tld, parts] of Object.entries(suffixes)) {
+ for (const part of parts) {
+ set.add(`${part}.${tld}`);
+ }
+ }
+
+ return set;
+}
+
+export const TWO_PART_PUBLIC_SUFFIXES = buildSuffixSet(ccTLD_PREFIXES);
+export const WILDCARD_PUBLIC_SUFFIXES = buildSuffixSet(WILDCARD_BASES);
diff --git a/llama.cpp/tools/ui/src/lib/constants/viewport.ts b/llama.cpp/tools/ui/src/lib/constants/viewport.ts
new file mode 100644
index 0000000000000000000000000000000000000000..26e202cfea80c425a13daf986f8ed538488e05d4
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/constants/viewport.ts
@@ -0,0 +1 @@
+export const DEFAULT_MOBILE_BREAKPOINT = 768;
diff --git a/llama.cpp/tools/ui/src/lib/contexts/chat-actions.context.ts b/llama.cpp/tools/ui/src/lib/contexts/chat-actions.context.ts
new file mode 100644
index 0000000000000000000000000000000000000000..e9050fa27fb3ddd236ffa5815279cd13e1cbc156
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/contexts/chat-actions.context.ts
@@ -0,0 +1,39 @@
+import { getContext, setContext } from 'svelte';
+import { CONTEXT_KEY_CHAT_ACTIONS } from '$lib/constants';
+
+export interface ChatActionsContext {
+ copy: (message: DatabaseMessage) => void;
+ delete: (message: DatabaseMessage) => void;
+ navigateToSibling: (siblingId: string) => void;
+ editWithBranching: (
+ message: DatabaseMessage,
+ newContent: string,
+ newExtras?: DatabaseMessageExtra[]
+ ) => void;
+ editWithReplacement: (
+ message: DatabaseMessage,
+ newContent: string,
+ shouldBranch: boolean
+ ) => void;
+ editUserMessagePreserveResponses: (
+ message: DatabaseMessage,
+ newContent: string,
+ newExtras?: DatabaseMessageExtra[]
+ ) => void;
+ regenerateWithBranching: (message: DatabaseMessage, modelOverride?: string) => void;
+ continueAssistantMessage: (message: DatabaseMessage) => void;
+ forkConversation: (
+ message: DatabaseMessage,
+ options: { name: string; includeAttachments: boolean }
+ ) => void;
+}
+
+const CHAT_ACTIONS_KEY = Symbol.for(CONTEXT_KEY_CHAT_ACTIONS);
+
+export function setChatActionsContext(ctx: ChatActionsContext): ChatActionsContext {
+ return setContext(CHAT_ACTIONS_KEY, ctx);
+}
+
+export function getChatActionsContext(): ChatActionsContext {
+ return getContext(CHAT_ACTIONS_KEY);
+}
diff --git a/llama.cpp/tools/ui/src/lib/contexts/chat-settings-config.context.ts b/llama.cpp/tools/ui/src/lib/contexts/chat-settings-config.context.ts
new file mode 100644
index 0000000000000000000000000000000000000000..35941e09bd5cd82773993c4e023c87cac6a4f764
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/contexts/chat-settings-config.context.ts
@@ -0,0 +1,20 @@
+import { getContext, setContext } from 'svelte';
+import { CONTEXT_KEY_CHAT_SETTINGS_CONFIG } from '$lib/constants';
+
+export interface ChatSettingsConfigContext {
+ readonly localConfig: SettingsConfigType;
+ handleConfigChange: (key: string, value: string | boolean) => void;
+ handleThemeChange: (theme: string) => void;
+}
+
+const CHAT_SETTINGS_CONFIG_KEY = Symbol.for(CONTEXT_KEY_CHAT_SETTINGS_CONFIG);
+
+export function setChatSettingsConfigContext(
+ ctx: ChatSettingsConfigContext
+): ChatSettingsConfigContext {
+ return setContext(CHAT_SETTINGS_CONFIG_KEY, ctx);
+}
+
+export function getChatSettingsConfigContext(): ChatSettingsConfigContext {
+ return getContext(CHAT_SETTINGS_CONFIG_KEY);
+}
diff --git a/llama.cpp/tools/ui/src/lib/contexts/index.ts b/llama.cpp/tools/ui/src/lib/contexts/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..01cd1d4b70abbc8d0ca15ef1e7ecbfdc6c34e80e
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/contexts/index.ts
@@ -0,0 +1,25 @@
+export {
+ getMessageEditContext,
+ setMessageEditContext,
+ type MessageEditContext,
+ type MessageEditState,
+ type MessageEditActions
+} from './message-edit.context';
+
+export {
+ getChatActionsContext,
+ setChatActionsContext,
+ type ChatActionsContext
+} from './chat-actions.context';
+
+export {
+ getChatSettingsConfigContext,
+ setChatSettingsConfigContext,
+ type ChatSettingsConfigContext
+} from './chat-settings-config.context';
+
+export {
+ getProcessingInfoContext,
+ setProcessingInfoContext,
+ type ProcessingInfoContext
+} from './processing-info.context';
diff --git a/llama.cpp/tools/ui/src/lib/contexts/message-edit.context.ts b/llama.cpp/tools/ui/src/lib/contexts/message-edit.context.ts
new file mode 100644
index 0000000000000000000000000000000000000000..b6231f940e9b1fe3a925fd8ba8833460f83cc441
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/contexts/message-edit.context.ts
@@ -0,0 +1,51 @@
+import { getContext, setContext } from 'svelte';
+import { CONTEXT_KEY_MESSAGE_EDIT } from '$lib/constants';
+import { MessageRole } from '$lib/enums';
+
+export interface MessageEditState {
+ readonly isEditing: boolean;
+ readonly editedContent: string;
+ readonly editedExtras: DatabaseMessageExtra[];
+ readonly editedUploadedFiles: ChatUploadedFile[];
+ readonly originalContent: string;
+ readonly originalExtras: DatabaseMessageExtra[];
+ readonly showSaveOnlyOption: boolean;
+ readonly showBranchAfterEditOption: boolean;
+ readonly shouldBranchAfterEdit: boolean;
+ readonly messageRole: MessageRole;
+ readonly rawEditContent?: string;
+}
+
+export interface MessageEditActions {
+ setContent: (content: string) => void;
+ setExtras: (extras: DatabaseMessageExtra[]) => void;
+ setUploadedFiles: (files: ChatUploadedFile[]) => void;
+ save: () => void;
+ saveOnly: () => void;
+ cancel: () => void;
+ startEdit: () => void;
+}
+
+export interface AssistantEditActions {
+ setShouldBranchAfterEdit: (value: boolean) => void;
+}
+
+export type MessageEditContext = MessageEditState &
+ MessageEditActions &
+ Partial;
+
+const MESSAGE_EDIT_KEY = Symbol.for(CONTEXT_KEY_MESSAGE_EDIT);
+
+/**
+ * Sets the message edit context. Call this in the parent component (ChatMessage.svelte).
+ */
+export function setMessageEditContext(ctx: MessageEditContext): MessageEditContext {
+ return setContext(MESSAGE_EDIT_KEY, ctx);
+}
+
+/**
+ * Gets the message edit context. Call this in child components.
+ */
+export function getMessageEditContext(): MessageEditContext {
+ return getContext(MESSAGE_EDIT_KEY);
+}
diff --git a/llama.cpp/tools/ui/src/lib/contexts/processing-info.context.ts b/llama.cpp/tools/ui/src/lib/contexts/processing-info.context.ts
new file mode 100644
index 0000000000000000000000000000000000000000..0cf43336f1db2ac62e422124f6716886ad7ff356
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/contexts/processing-info.context.ts
@@ -0,0 +1,16 @@
+import { getContext, setContext } from 'svelte';
+import { CONTEXT_KEY_PROCESSING_INFO } from '$lib/constants';
+
+export interface ProcessingInfoContext {
+ readonly showProcessingInfo: boolean;
+}
+
+const PROCESSING_INFO_KEY = Symbol.for(CONTEXT_KEY_PROCESSING_INFO);
+
+export function setProcessingInfoContext(ctx: ProcessingInfoContext): ProcessingInfoContext {
+ return setContext(PROCESSING_INFO_KEY, ctx);
+}
+
+export function getProcessingInfoContext(): ProcessingInfoContext {
+ return getContext(PROCESSING_INFO_KEY);
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/agentic.enums.ts b/llama.cpp/tools/ui/src/lib/enums/agentic.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..9ad7b4f1a87c19a021f963a4195440917c21a3ac
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/agentic.enums.ts
@@ -0,0 +1,27 @@
+/**
+ * OpenAI-compatible tool call type.
+ */
+export enum ToolCallType {
+ FUNCTION = 'function'
+}
+
+/**
+ * Types of sections in agentic content display.
+ */
+export enum AgenticSectionType {
+ TEXT = 'text',
+ TOOL_CALL = 'tool_call',
+ TOOL_CALL_PENDING = 'tool_call_pending',
+ TOOL_CALL_STREAMING = 'tool_call_streaming',
+ REASONING = 'reasoning',
+ REASONING_PENDING = 'reasoning_pending'
+}
+
+/**
+ * How a Continue click on an assistant message resumes generation.
+ */
+export enum ContinueIntentKind {
+ APPEND_TEXT = 'append_text',
+ RERUN_TURN = 'rerun_turn',
+ NEXT_TURN = 'next_turn'
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/attachment.enums.ts b/llama.cpp/tools/ui/src/lib/enums/attachment.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..1c3258cb19da8c918e993256e9aa42c3b0028aa0
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/attachment.enums.ts
@@ -0,0 +1,56 @@
+/**
+ * Attachment type enum for database message extras
+ */
+export enum AttachmentType {
+ AUDIO = 'AUDIO',
+ IMAGE = 'IMAGE',
+ VIDEO = 'VIDEO',
+ MCP_PROMPT = 'MCP_PROMPT',
+ MCP_RESOURCE = 'MCP_RESOURCE',
+ PDF = 'PDF',
+ TEXT = 'TEXT',
+ LEGACY_CONTEXT = 'context' // Legacy attachment type for backward compatibility
+}
+
+/**
+ * Unique identifiers for attachment menu items in the chat form action dropdowns.
+ * Used to select which file upload or attachment action is triggered.
+ */
+export enum AttachmentMenuItemId {
+ IMAGES = 'images',
+ AUDIO = 'audio',
+ VIDEO = 'video',
+ TEXT = 'text',
+ PDF = 'pdf',
+ SYSTEM_MESSAGE = 'system-message',
+ MCP_PROMPT = 'mcp-prompt',
+ MCP_RESOURCES = 'mcp-resources'
+}
+
+/**
+ * Defines when an attachment menu item should be enabled.
+ */
+export enum AttachmentItemEnabledWhen {
+ ALWAYS = 'always',
+ HAS_VISION_MODALITY = 'hasVisionModality',
+ HAS_AUDIO_MODALITY = 'hasAudioModality',
+ HAS_VIDEO_MODALITY = 'hasVideoModality'
+}
+
+/**
+ * Defines the callback action triggered when an attachment menu item is clicked.
+ */
+export enum AttachmentAction {
+ FILE_UPLOAD = 'onFileUpload',
+ SYSTEM_PROMPT_CLICK = 'onSystemPromptClick',
+ MCP_PROMPT_CLICK = 'onMcpPromptClick',
+ MCP_RESOURCES_CLICK = 'onMcpResourcesClick'
+}
+
+/**
+ * Visibility conditions for attachment menu items.
+ */
+export enum AttachmentItemVisibleWhen {
+ HAS_MCP_PROMPTS_SUPPORT = 'hasMcpPromptsSupport',
+ HAS_MCP_RESOURCES_SUPPORT = 'hasMcpResourcesSupport'
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/chat.enums.ts b/llama.cpp/tools/ui/src/lib/enums/chat.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..278e4af84e184816bf443c2cd16b04eec052ef78
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/chat.enums.ts
@@ -0,0 +1,74 @@
+export enum ChatMessageStatsView {
+ GENERATION = 'generation',
+ READING = 'reading',
+ TOOLS = 'tools',
+ SUMMARY = 'summary'
+}
+
+/**
+ * Connection state of a streamed completion, drives the resume status indicator.
+ */
+export enum StreamConnectionState {
+ STREAMING = 'streaming',
+ RESUMING = 'resuming',
+ LOST = 'lost'
+}
+
+/**
+ * Reasoning format options for API requests.
+ */
+export enum ReasoningFormat {
+ NONE = 'none',
+ AUTO = 'auto'
+}
+
+/**
+ * Message roles for chat messages.
+ */
+export enum MessageRole {
+ USER = 'user',
+ ASSISTANT = 'assistant',
+ SYSTEM = 'system',
+ TOOL = 'tool'
+}
+
+/**
+ * Message types for different content kinds.
+ */
+export enum MessageType {
+ ROOT = 'root',
+ TEXT = 'text',
+ THINK = 'think',
+ SYSTEM = 'system'
+}
+
+/**
+ * Content part types for API chat message content.
+ */
+export enum ContentPartType {
+ TEXT = 'text',
+ IMAGE_URL = 'image_url',
+ INPUT_AUDIO = 'input_audio',
+ INPUT_VIDEO = 'input_video'
+}
+
+/**
+ * Error dialog types for displaying server/timeout errors.
+ */
+export enum ErrorDialogType {
+ TIMEOUT = 'timeout',
+ SERVER = 'server'
+}
+
+export enum ConversationSelectionMode {
+ EXPORT = 'export',
+ IMPORT = 'import'
+}
+
+/**
+ * PDF view mode options for previewing PDF attachments.
+ */
+export enum PdfViewMode {
+ TEXT = 'text',
+ PAGES = 'pages'
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/files.enums.ts b/llama.cpp/tools/ui/src/lib/enums/files.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..a908b899657e947e514118ba840711be091a41c2
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/files.enums.ts
@@ -0,0 +1,266 @@
+/**
+ * Comprehensive dictionary of all supported file types in llama-ui
+ * Organized by category with TypeScript enums for better type safety
+ */
+
+// File type category enum
+export enum FileTypeCategory {
+ IMAGE = 'image',
+ AUDIO = 'audio',
+ VIDEO = 'video',
+ PDF = 'pdf',
+ TEXT = 'text'
+}
+
+/**
+ * Special file types for internal use (not MIME types)
+ */
+export enum SpecialFileType {
+ MCP_PROMPT = 'mcp-prompt'
+}
+
+// Specific file type enums for each category
+export enum FileTypeImage {
+ JPEG = 'jpeg',
+ PNG = 'png',
+ GIF = 'gif',
+ WEBP = 'webp',
+ SVG = 'svg',
+ HEIC = 'heic',
+ HEIF = 'heif'
+}
+
+export enum FileTypeAudio {
+ MP3 = 'mp3',
+ WAV = 'wav',
+ WEBM = 'webm'
+}
+
+export enum FileTypeVideo {
+ MP4 = 'mp4',
+ OGG = 'ogg'
+}
+
+export enum FileTypePdf {
+ PDF = 'pdf'
+}
+
+export enum FileTypeText {
+ PLAIN_TEXT = 'plainText',
+ MARKDOWN = 'md',
+ ASCIIDOC = 'asciidoc',
+ JAVASCRIPT = 'js',
+ TYPESCRIPT = 'ts',
+ JSX = 'jsx',
+ TSX = 'tsx',
+ CSS = 'css',
+ HTML = 'html',
+ JSON = 'json',
+ XML = 'xml',
+ YAML = 'yaml',
+ CSV = 'csv',
+ LOG = 'log',
+ PYTHON = 'python',
+ JAVA = 'java',
+ CPP = 'cpp',
+ PHP = 'php',
+ RUBY = 'ruby',
+ GO = 'go',
+ RUST = 'rust',
+ SHELL = 'shell',
+ SQL = 'sql',
+ R = 'r',
+ SCALA = 'scala',
+ KOTLIN = 'kotlin',
+ SWIFT = 'swift',
+ DART = 'dart',
+ VUE = 'vue',
+ SVELTE = 'svelte',
+ LATEX = 'latex',
+ BIBTEX = 'bibtex',
+ CUDA = 'cuda',
+ VULKAN = 'vulkan',
+ HASKELL = 'haskell',
+ CSHARP = 'csharp',
+ PROPERTIES = 'properties'
+}
+
+// File extension enums
+export enum FileExtensionImage {
+ JPG = '.jpg',
+ JPEG = '.jpeg',
+ PNG = '.png',
+ GIF = '.gif',
+ WEBP = '.webp',
+ SVG = '.svg',
+ HEIC = '.heic',
+ HEIF = '.heif'
+}
+
+export enum FileExtensionAudio {
+ MP3 = '.mp3',
+ WAV = '.wav'
+}
+
+export enum FileExtensionVideo {
+ MP4 = '.mp4',
+ OGG = '.ogg'
+}
+
+export enum FileExtensionPdf {
+ PDF = '.pdf'
+}
+
+export enum FileExtensionText {
+ TXT = '.txt',
+ MD = '.md',
+ ADOC = '.adoc',
+ JS = '.js',
+ TS = '.ts',
+ JSX = '.jsx',
+ TSX = '.tsx',
+ CSS = '.css',
+ HTML = '.html',
+ HTM = '.htm',
+ JSON = '.json',
+ JSONL = '.jsonl',
+ ZIP = '.zip',
+ XML = '.xml',
+ YAML = '.yaml',
+ YML = '.yml',
+ CSV = '.csv',
+ LOG = '.log',
+ PY = '.py',
+ JAVA = '.java',
+ CPP = '.cpp',
+ C = '.c',
+ H = '.h',
+ PHP = '.php',
+ RB = '.rb',
+ GO = '.go',
+ RS = '.rs',
+ SH = '.sh',
+ BAT = '.bat',
+ SQL = '.sql',
+ R = '.r',
+ SCALA = '.scala',
+ KT = '.kt',
+ SWIFT = '.swift',
+ DART = '.dart',
+ VUE = '.vue',
+ SVELTE = '.svelte',
+ TEX = '.tex',
+ BIB = '.bib',
+ CU = '.cu',
+ CUH = '.cuh',
+ COMP = '.comp',
+ HPP = '.hpp',
+ HS = '.hs',
+ PROPERTIES = '.properties',
+ CS = '.cs'
+}
+
+// MIME type prefixes and includes for content detection
+export enum MimeTypePrefix {
+ IMAGE = 'image/',
+ TEXT = 'text'
+}
+
+export enum MimeTypeIncludes {
+ JSON = 'json',
+ JAVASCRIPT = 'javascript',
+ TYPESCRIPT = 'typescript'
+}
+
+// URI patterns for content detection
+export enum UriPattern {
+ DATABASE_KEYWORD = 'database',
+ DATABASE_SCHEME = 'db://'
+}
+
+// MIME type enums
+export enum MimeTypeApplication {
+ PDF = 'application/pdf',
+ OCTET_STREAM = 'application/octet-stream',
+ ZIP = 'application/zip'
+}
+
+export enum MimeTypeAudio {
+ MP3_MPEG = 'audio/mpeg',
+ MP3 = 'audio/mp3',
+ MP4 = 'audio/mp4',
+ WAV = 'audio/wav',
+ WAVE = 'audio/wave',
+ X_WAV = 'audio/x-wav',
+ X_WAVE = 'audio/x-wave',
+ VND_WAVE = 'audio/vnd.wave',
+ X_PN_WAV = 'audio/x-pn-wav',
+ WEBM = 'audio/webm',
+ WEBM_OPUS = 'audio/webm;codecs=opus'
+}
+
+export enum MimeTypeVideo {
+ MP4 = 'video/mp4',
+ OGG = 'video/ogg'
+}
+
+export enum MimeTypeImage {
+ JPEG = 'image/jpeg',
+ JPG = 'image/jpg',
+ PNG = 'image/png',
+ GIF = 'image/gif',
+ WEBP = 'image/webp',
+ SVG = 'image/svg+xml',
+ ICO = 'image/x-icon',
+ ICO_MICROSOFT = 'image/vnd.microsoft.icon',
+ HEIC = 'image/heic',
+ HEIF = 'image/heif'
+}
+
+export enum MimeTypeText {
+ PLAIN = 'text/plain',
+ MARKDOWN = 'text/markdown',
+ ASCIIDOC = 'text/asciidoc',
+ JAVASCRIPT = 'text/javascript',
+ JAVASCRIPT_APP = 'application/javascript',
+ TYPESCRIPT = 'text/typescript',
+ JSX = 'text/jsx',
+ TSX = 'text/tsx',
+ CSS = 'text/css',
+ HTML = 'text/html',
+ JSON = 'application/json',
+ JSONL = 'application/jsonl',
+ XML_TEXT = 'text/xml',
+ XML_APP = 'application/xml',
+ YAML_TEXT = 'text/yaml',
+ YAML_APP = 'application/yaml',
+ CSV = 'text/csv',
+ PYTHON = 'text/x-python',
+ JAVA = 'text/x-java-source',
+ CPP_HDR = 'text/x-c++hdr',
+ CPP_SRC = 'text/x-c++src',
+ CSHARP = 'text/x-csharp',
+ HASKELL = 'text/x-haskell',
+ C_SRC = 'text/x-csrc',
+ C_HDR = 'text/x-chdr',
+ PHP = 'text/x-php',
+ RUBY = 'text/x-ruby',
+ GO = 'text/x-go',
+ RUST = 'text/x-rust',
+ SHELL = 'text/x-shellscript',
+ BAT = 'application/x-bat',
+ SQL = 'text/x-sql',
+ R = 'text/x-r',
+ SCALA = 'text/x-scala',
+ KOTLIN = 'text/x-kotlin',
+ SWIFT = 'text/x-swift',
+ DART = 'text/x-dart',
+ VUE = 'text/x-vue',
+ SVELTE = 'text/x-svelte',
+ TEX = 'text/x-tex',
+ TEX_APP = 'application/x-tex',
+ LATEX = 'application/x-latex',
+ BIBTEX = 'text/x-bibtex',
+ CUDA = 'text/x-cuda',
+ PROPERTIES = 'text/properties'
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/index.ts b/llama.cpp/tools/ui/src/lib/enums/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..d8e47958bb04e254f13b3e78ce1b75b2f6419c45
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/index.ts
@@ -0,0 +1,68 @@
+export {
+ AttachmentType,
+ AttachmentMenuItemId,
+ AttachmentItemEnabledWhen,
+ AttachmentAction,
+ AttachmentItemVisibleWhen
+} from './attachment.enums';
+
+export { AgenticSectionType, ContinueIntentKind, ToolCallType } from './agentic.enums';
+
+export {
+ ChatMessageStatsView,
+ StreamConnectionState,
+ ContentPartType,
+ ConversationSelectionMode,
+ ErrorDialogType,
+ MessageRole,
+ MessageType,
+ PdfViewMode,
+ ReasoningFormat
+} from './chat.enums';
+
+export { ReasoningEffort } from './reasoning-effort.enums';
+
+export {
+ FileTypeCategory,
+ FileTypeImage,
+ FileTypeAudio,
+ FileTypePdf,
+ FileTypeText,
+ FileExtensionImage,
+ FileExtensionAudio,
+ FileExtensionPdf,
+ FileExtensionText,
+ MimeTypePrefix,
+ MimeTypeIncludes,
+ UriPattern,
+ MimeTypeApplication,
+ MimeTypeAudio,
+ MimeTypeVideo,
+ MimeTypeImage,
+ MimeTypeText,
+ SpecialFileType
+} from './files.enums';
+
+export {
+ MCPConnectionPhase,
+ MCPLogLevel,
+ MCPTransportType,
+ HealthCheckStatus,
+ MCPContentType,
+ MCPRefType,
+ JsonSchemaType
+} from './mcp.enums';
+
+export { ModelModality } from './model.enums';
+
+export { ServerRole, ServerModelStatus, ServerModelsSseEventType } from './server.enums';
+
+export { ParameterSource, SyncableParameterType, SettingsFieldType } from './settings.enums';
+
+export { ColorMode, HtmlInputType, McpPromptVariant, TooltipSide, UrlProtocol } from './ui.enums';
+
+export { KeyboardKey } from './keyboard.enums';
+
+export { ToolSource, ToolPermissionDecision, ToolResponseField } from './tools.enums';
+
+export { SplashOrientation } from './splash.enums';
diff --git a/llama.cpp/tools/ui/src/lib/enums/keyboard.enums.ts b/llama.cpp/tools/ui/src/lib/enums/keyboard.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..46cd4a77614f704adb86e473d286f43ec4e584fb
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/keyboard.enums.ts
@@ -0,0 +1,19 @@
+/**
+ * Keyboard key names for event handling
+ */
+export enum KeyboardKey {
+ ENTER = 'Enter',
+ ESCAPE = 'Escape',
+ ARROW_UP = 'ArrowUp',
+ ARROW_DOWN = 'ArrowDown',
+ ARROW_LEFT = 'ArrowLeft',
+ ARROW_RIGHT = 'ArrowRight',
+ TAB = 'Tab',
+ D_LOWER = 'd',
+ D_UPPER = 'D',
+ E_UPPER = 'E',
+ K_LOWER = 'k',
+ O_LOWER = 'o',
+ O_UPPER = 'O',
+ SPACE = ' '
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/mcp.enums.ts b/llama.cpp/tools/ui/src/lib/enums/mcp.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..3d9a2070dc541b43e95aa67f0a31cfb67f481e4a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/mcp.enums.ts
@@ -0,0 +1,68 @@
+/**
+ * Connection lifecycle phases for MCP protocol
+ */
+export enum MCPConnectionPhase {
+ IDLE = 'idle',
+ TRANSPORT_CREATING = 'transport_creating',
+ TRANSPORT_READY = 'transport_ready',
+ INITIALIZING = 'initializing',
+ CAPABILITIES_EXCHANGED = 'capabilities_exchanged',
+ LISTING_TOOLS = 'listing_tools',
+ CONNECTED = 'connected',
+ ERROR = 'error',
+ DISCONNECTED = 'disconnected'
+}
+
+/**
+ * Log level for connection events
+ */
+export enum MCPLogLevel {
+ INFO = 'info',
+ WARN = 'warn',
+ ERROR = 'error'
+}
+
+/**
+ * Transport types for MCP connections
+ */
+export enum MCPTransportType {
+ WEBSOCKET = 'websocket',
+ STREAMABLE_HTTP = 'streamable_http',
+ SSE = 'sse'
+}
+
+/**
+ * Health check status for MCP servers
+ */
+export enum HealthCheckStatus {
+ IDLE = 'idle',
+ CONNECTING = 'connecting',
+ SUCCESS = 'success',
+ ERROR = 'error'
+}
+
+/**
+ * Content types for MCP tool results
+ */
+export enum MCPContentType {
+ TEXT = 'text',
+ IMAGE = 'image',
+ RESOURCE = 'resource'
+}
+
+/**
+ * JSON Schema types used in MCP tool definitions
+ */
+export enum JsonSchemaType {
+ OBJECT = 'object',
+ STRING = 'string',
+ NUMBER = 'number'
+}
+
+/**
+ * Reference types for MCP completions
+ */
+export enum MCPRefType {
+ PROMPT = 'ref/prompt',
+ RESOURCE = 'ref/resource'
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/model.enums.ts b/llama.cpp/tools/ui/src/lib/enums/model.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..7aa469947e2b0d405619f374bdbca70f1bdca2da
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/model.enums.ts
@@ -0,0 +1,6 @@
+export enum ModelModality {
+ TEXT = 'TEXT',
+ AUDIO = 'AUDIO',
+ VISION = 'VISION',
+ VIDEO = 'VIDEO'
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/reasoning-effort.enums.ts b/llama.cpp/tools/ui/src/lib/enums/reasoning-effort.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..dadb0c7264e8728b8b37843869330b94ed06683a
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/reasoning-effort.enums.ts
@@ -0,0 +1,10 @@
+/**
+ * Reasoning effort levels for thinking models.
+ * These values are sent to the server and mapped to token budgets.
+ */
+export enum ReasoningEffort {
+ LOW = 'low',
+ MEDIUM = 'medium',
+ HIGH = 'high',
+ MAX = 'max'
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/server.enums.ts b/llama.cpp/tools/ui/src/lib/enums/server.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..446af84be7089e620004bf9b7b52e44f4cbfe3a0
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/server.enums.ts
@@ -0,0 +1,35 @@
+/**
+ * Server role enum - used for single/multi-model mode
+ */
+export enum ServerRole {
+ /** Single model mode - server running with a specific model loaded */
+ MODEL = 'model',
+ /** Router mode - server managing multiple model instances */
+ ROUTER = 'router'
+}
+
+/**
+ * Model status enum - matches tools/server/server-models.h from C++ server
+ * Used as the `value` field in the status object from /models endpoint
+ */
+export enum ServerModelStatus {
+ UNLOADED = 'unloaded',
+ LOADING = 'loading',
+ LOADED = 'loaded',
+ SLEEPING = 'sleeping',
+ FAILED = 'failed'
+}
+
+/**
+ * /models/sse event type enum - discriminates the records broadcast on the
+ * model status feed in ROUTER mode. Matches the event names emitted by
+ * tools/server/server-models.cpp from the C++ server.
+ */
+export enum ServerModelsSseEventType {
+ STATUS_CHANGE = 'status_change',
+ MODEL_STATUS = 'model_status',
+ STATUS_UPDATE = 'status_update',
+ MODELS_RELOAD = 'models_reload',
+ MODEL_REMOVE = 'model_remove',
+ DOWNLOAD_PROGRESS = 'download_progress'
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/settings.enums.ts b/llama.cpp/tools/ui/src/lib/enums/settings.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..f17f219762014bf9b24c3627e3f65991ef8eb339
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/settings.enums.ts
@@ -0,0 +1,26 @@
+/**
+ * Parameter source - indicates whether a parameter uses default or custom value
+ */
+export enum ParameterSource {
+ DEFAULT = 'default',
+ CUSTOM = 'custom'
+}
+
+/**
+ * Syncable parameter type - data types for parameters that can be synced with server
+ */
+export enum SyncableParameterType {
+ NUMBER = 'number',
+ STRING = 'string',
+ BOOLEAN = 'boolean'
+}
+
+/**
+ * Settings field type - defines the input type for settings fields
+ */
+export enum SettingsFieldType {
+ INPUT = 'input',
+ TEXTAREA = 'textarea',
+ CHECKBOX = 'checkbox',
+ SELECT = 'select'
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/splash.enums.ts b/llama.cpp/tools/ui/src/lib/enums/splash.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..7efa89299fea4f2e5e764c626054d1052d520ede
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/splash.enums.ts
@@ -0,0 +1,7 @@
+/**
+ * Splash screen orientation for iOS apple-touch-startup-image
+ */
+export enum SplashOrientation {
+ PORTRAIT = 'portrait',
+ LANDSCAPE = 'landscape'
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/tools.enums.ts b/llama.cpp/tools/ui/src/lib/enums/tools.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..120c1d471d64b01f916331499597d32948cc41f6
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/tools.enums.ts
@@ -0,0 +1,18 @@
+export enum ToolSource {
+ BUILTIN = 'builtin',
+ MCP = 'mcp',
+ CUSTOM = 'custom',
+ FRONTEND = 'frontend'
+}
+
+export enum ToolPermissionDecision {
+ ALWAYS = 'always',
+ ALWAYS_SERVER = 'always_server',
+ ONCE = 'once',
+ DENY = 'deny'
+}
+
+export enum ToolResponseField {
+ PLAIN_TEXT = 'plain_text_response',
+ ERROR = 'error'
+}
diff --git a/llama.cpp/tools/ui/src/lib/enums/ui.enums.ts b/llama.cpp/tools/ui/src/lib/enums/ui.enums.ts
new file mode 100644
index 0000000000000000000000000000000000000000..82996379428e429edba0bc777eea0ffcd7c8f5ac
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/enums/ui.enums.ts
@@ -0,0 +1,35 @@
+export enum ColorMode {
+ LIGHT = 'light',
+ DARK = 'dark',
+ SYSTEM = 'system'
+}
+
+export enum TooltipSide {
+ TOP = 'top',
+ RIGHT = 'right',
+ BOTTOM = 'bottom',
+ LEFT = 'left'
+}
+
+/**
+ * MCP prompt display variant
+ */
+export enum McpPromptVariant {
+ MESSAGE = 'message',
+ ATTACHMENT = 'attachment'
+}
+
+/**
+ * URL prefixes for protocol detection
+ */
+export enum UrlProtocol {
+ DATA = 'data:',
+ HTTP = 'http:',
+ HTTPS = 'https:',
+ WEBSOCKET = 'ws:',
+ WEBSOCKET_SECURE = 'wss:'
+}
+
+export enum HtmlInputType {
+ FILE = 'file'
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-attachment-menu.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-attachment-menu.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..0940ce7b6b1880c23ae8eda09f460b637365eb0e
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-attachment-menu.svelte.ts
@@ -0,0 +1,82 @@
+import { page } from '$app/state';
+import { AttachmentAction } from '$lib/enums';
+
+export interface AttachmentModalityFlags {
+ hasVisionModality: boolean;
+ hasAudioModality: boolean;
+ hasVideoModality: boolean;
+ hasMcpPromptsSupport: boolean;
+ hasMcpResourcesSupport: boolean;
+}
+
+export interface AttachmentActionCallbacks {
+ onFileUpload?: () => void;
+ onSystemPromptClick?: () => void;
+ onMcpPromptClick?: () => void;
+ onMcpResourcesClick?: () => void;
+}
+
+export interface UseAttachmentMenuReturn {
+ readonly callbacks: Record void>;
+ isItemEnabled(enabledWhen: string | undefined): boolean;
+ isItemVisible(visibleWhen: string | undefined): boolean;
+ getSystemMessageTooltip(): string;
+}
+
+/**
+ * useAttachmentMenu - Shared logic for attachment menu components.
+ *
+ * Encapsulates the modality-flag checks and callback wrapping that is
+ * identical across the desktop dropdown (`ChatFormActionAddDropdown`)
+ * and the mobile sheet (`ChatFormActionAddSheet`).
+ *
+ * @param getFlags - Getter returning the current modality capability flags.
+ * @param getCallbacks - Getter returning the raw action callbacks from props.
+ * @param close - Function that dismisses the hosting UI element (dropdown / sheet).
+ */
+export function useAttachmentMenu(
+ getFlags: () => AttachmentModalityFlags,
+ getCallbacks: () => AttachmentActionCallbacks,
+ close: () => void
+): UseAttachmentMenuReturn {
+ const modalityFlags = $derived(getFlags());
+
+ const callbacks = $derived.by(() => {
+ const cbs = getCallbacks();
+ const wrap = (fn?: () => void) => () => {
+ close();
+ fn?.();
+ };
+ return {
+ [AttachmentAction.FILE_UPLOAD]: wrap(cbs.onFileUpload),
+ [AttachmentAction.SYSTEM_PROMPT_CLICK]: wrap(cbs.onSystemPromptClick),
+ [AttachmentAction.MCP_PROMPT_CLICK]: wrap(cbs.onMcpPromptClick),
+ [AttachmentAction.MCP_RESOURCES_CLICK]: wrap(cbs.onMcpResourcesClick)
+ };
+ });
+
+ function isItemEnabled(enabledWhen: string | undefined): boolean {
+ if (!enabledWhen || enabledWhen === 'always') return true;
+ return !!modalityFlags[enabledWhen as keyof AttachmentModalityFlags];
+ }
+
+ function isItemVisible(visibleWhen: string | undefined): boolean {
+ if (!visibleWhen) return true;
+ return !!modalityFlags[visibleWhen as keyof AttachmentModalityFlags];
+ }
+
+ function getSystemMessageTooltip(): string {
+ return !page.params.id
+ ? 'Add custom system message for a new conversation'
+ : 'Inject custom system message at the beginning of the conversation';
+ }
+
+ return {
+ get callbacks() {
+ return callbacks;
+ },
+ isItemEnabled,
+ isItemVisible,
+ getSystemMessageTooltip
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-auto-scroll.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-auto-scroll.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..8107f08d088eaf85ae538c68d59fb3fff0d674bc
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-auto-scroll.svelte.ts
@@ -0,0 +1,217 @@
+import { AUTO_SCROLL_AT_BOTTOM_THRESHOLD, AUTO_SCROLL_INTERVAL } from '$lib/constants';
+
+export interface AutoScrollOptions {
+ disabled?: boolean;
+}
+
+/**
+ * Creates an auto-scroll controller for a scrollable container.
+ *
+ * Features:
+ * - Auto-scrolls to bottom during streaming/loading
+ * - Stops auto-scroll when user manually scrolls up
+ * - Resumes auto-scroll when user scrolls back to bottom
+ */
+export class AutoScrollController {
+ private _autoScrollEnabled = $state(true);
+ private _userScrolledUp = $state(false);
+ private _lastScrollTop = $state(0);
+ private _scrollInterval: ReturnType | undefined;
+ private _container: HTMLElement | undefined;
+ private _disabled: boolean;
+ private _mutationObserver: MutationObserver | null = null;
+ private _rafPending = false;
+ private _observerEnabled = false;
+ constructor(options: AutoScrollOptions = {}) {
+ this._disabled = options.disabled ?? false;
+ }
+
+ get autoScrollEnabled(): boolean {
+ return this._autoScrollEnabled;
+ }
+
+ get userScrolledUp(): boolean {
+ return this._userScrolledUp;
+ }
+
+ /**
+ * Binds the controller to a scrollable container element.
+ */
+ setContainer(container: HTMLElement | undefined): void {
+ this._doStopObserving();
+ this._container = container;
+
+ if (this._observerEnabled && container && !this._disabled) {
+ this._doStartObserving();
+ }
+ }
+
+ /**
+ * Updates the disabled state.
+ */
+ setDisabled(disabled: boolean): void {
+ if (this._disabled === disabled) return;
+ this._disabled = disabled;
+ if (disabled) {
+ this._autoScrollEnabled = false;
+ this.stopInterval();
+ this._doStopObserving();
+ } else if (this._observerEnabled && this._container && !this._mutationObserver) {
+ this._doStartObserving();
+ }
+ }
+
+ /**
+ * Handles scroll events to detect user scroll direction and toggle auto-scroll.
+ */
+ handleScroll(): void {
+ if (this._disabled || !this._container) return;
+
+ const { scrollTop, scrollHeight, clientHeight } = this._container;
+ const distanceFromBottom = scrollHeight - clientHeight - scrollTop;
+ const isScrollingUp = scrollTop < this._lastScrollTop;
+ const isAtBottom = distanceFromBottom < AUTO_SCROLL_AT_BOTTOM_THRESHOLD;
+
+ if (isScrollingUp && !isAtBottom) {
+ this._userScrolledUp = true;
+ this._autoScrollEnabled = false;
+ } else if (isAtBottom && this._userScrolledUp) {
+ this._userScrolledUp = false;
+ this._autoScrollEnabled = true;
+ }
+
+ this._lastScrollTop = scrollTop;
+ }
+
+ /**
+ * Scrolls the container to the bottom.
+ */
+ scrollToBottom(behavior: ScrollBehavior = 'smooth'): void {
+ if (this._disabled || !this._container) return;
+ this._container.scrollTo({ top: this._container.scrollHeight, behavior });
+ }
+
+ /**
+ * Enables auto-scroll (e.g., when user sends a message).
+ */
+ enable(): void {
+ if (this._disabled) return;
+ this._userScrolledUp = false;
+ this._autoScrollEnabled = true;
+ }
+
+ /**
+ * Resets scroll state when switching conversations.
+ */
+ resetScrollState(): void {
+ this._userScrolledUp = false;
+ this._autoScrollEnabled = !this._disabled;
+ if (this._container) {
+ this._lastScrollTop = this._container.scrollTop;
+ }
+ }
+
+ /**
+ * Starts the auto-scroll interval for continuous scrolling during streaming.
+ */
+ startInterval(): void {
+ if (this._disabled || this._scrollInterval) return;
+
+ this._scrollInterval = setInterval(() => {
+ this.scrollToBottom();
+ }, AUTO_SCROLL_INTERVAL);
+ }
+
+ /**
+ * Stops the auto-scroll interval.
+ */
+ stopInterval(): void {
+ if (this._scrollInterval) {
+ clearInterval(this._scrollInterval);
+ this._scrollInterval = undefined;
+ }
+ }
+
+ /**
+ * Updates the auto-scroll interval based on streaming state.
+ * Call this in a $effect to automatically manage the interval.
+ */
+ updateInterval(isStreaming: boolean): void {
+ if (this._disabled) {
+ this.stopInterval();
+ return;
+ }
+
+ if (isStreaming && this._autoScrollEnabled) {
+ if (!this._scrollInterval) {
+ this.startInterval();
+ }
+ } else {
+ this.stopInterval();
+ }
+ }
+
+ /**
+ * Cleans up resources. Call this in onDestroy or when the component unmounts.
+ */
+ destroy(): void {
+ this.stopInterval();
+ this._doStopObserving();
+ }
+
+ /**
+ * Starts a MutationObserver on the container that auto-scrolls to bottom
+ * on content changes. More responsive than interval-based polling.
+ */
+ startObserving(): void {
+ this._observerEnabled = true;
+
+ if (this._container && !this._disabled && !this._mutationObserver) {
+ this._doStartObserving();
+ }
+ }
+
+ /**
+ * Stops the MutationObserver.
+ */
+ stopObserving(): void {
+ this._observerEnabled = false;
+ this._doStopObserving();
+ }
+
+ private _doStartObserving(): void {
+ if (!this._container || this._mutationObserver) return;
+
+ this._mutationObserver = new MutationObserver(() => {
+ if (!this._autoScrollEnabled || this._rafPending) return;
+ this._rafPending = true;
+ requestAnimationFrame(() => {
+ this._rafPending = false;
+ if (this._autoScrollEnabled && this._container) {
+ this._container.scrollTop = this._container.scrollHeight;
+ }
+ });
+ });
+
+ this._mutationObserver.observe(this._container, {
+ childList: true,
+ subtree: true,
+ characterData: true
+ });
+ }
+
+ private _doStopObserving(): void {
+ if (this._mutationObserver) {
+ this._mutationObserver.disconnect();
+ this._mutationObserver = null;
+ }
+ this._rafPending = false;
+ }
+}
+
+/**
+ * Creates a new AutoScrollController instance.
+ */
+export function createAutoScrollController(options: AutoScrollOptions = {}): AutoScrollController {
+ return new AutoScrollController(options);
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-chat-screen-active-model.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-chat-screen-active-model.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..de75e9fefcaea4c1e79856949e24573bdbb11910
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-chat-screen-active-model.svelte.ts
@@ -0,0 +1,100 @@
+/**
+ * Active model resolution and capability detection for the ChatScreen.
+ *
+ * Picks the model that should be used for the current view
+ * (router: user-selected or conversation fallback; non-router: first
+ * available option), and reactively tracks which modalities (vision /
+ * audio / video) it supports — fetching model props from the server on
+ * demand if they aren't cached yet.
+ */
+
+import { modelsStore, modelOptions, selectedModelId } from '$lib/stores/models.svelte';
+import { isRouterMode } from '$lib/stores/server.svelte';
+import { chatStore } from '$lib/stores/chat.svelte';
+import { activeMessages } from '$lib/stores/conversations.svelte';
+
+export function useChatScreenActiveModel() {
+ const isRouter = $derived(isRouterMode());
+ const conversationModel = $derived(
+ chatStore.getConversationModel(activeMessages() as DatabaseMessage[])
+ );
+
+ const activeModelId = $derived.by(() => {
+ const options = modelOptions();
+
+ if (!isRouter) {
+ return options.length > 0 ? options[0].model : null;
+ }
+
+ const selectedId = selectedModelId();
+ if (selectedId) {
+ const model = options.find((m) => m.id === selectedId);
+ if (model) return model.model;
+ }
+
+ if (conversationModel) {
+ const model = options.find((m) => m.model === conversationModel);
+ if (model) return model.model;
+ }
+
+ return null;
+ });
+
+ let modelPropsVersion = $state(0);
+
+ $effect(() => {
+ if (activeModelId) {
+ const cached = modelsStore.getModelProps(activeModelId);
+ if (!cached) {
+ modelsStore.fetchModelProps(activeModelId).then(() => {
+ modelPropsVersion++;
+ });
+ }
+ }
+ });
+
+ const hasAudioModality = $derived.by(() => {
+ if (activeModelId) {
+ void modelPropsVersion;
+ return modelsStore.modelSupportsAudio(activeModelId);
+ }
+ return false;
+ });
+
+ const hasVideoModality = $derived.by(() => {
+ if (activeModelId) {
+ void modelPropsVersion;
+ return modelsStore.modelSupportsVideo(activeModelId);
+ }
+ return false;
+ });
+
+ const hasVisionModality = $derived.by(() => {
+ if (activeModelId) {
+ void modelPropsVersion;
+ return modelsStore.modelSupportsVision(activeModelId);
+ }
+ return false;
+ });
+
+ return {
+ get isRouter() {
+ return isRouter;
+ },
+ get conversationModel() {
+ return conversationModel;
+ },
+ get activeModelId() {
+ return activeModelId;
+ },
+ get hasAudioModality() {
+ return hasAudioModality;
+ },
+ get hasVideoModality() {
+ return hasVideoModality;
+ },
+ get hasVisionModality() {
+ return hasVisionModality;
+ }
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-chat-screen-drag-and-drop.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-chat-screen-drag-and-drop.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..3f292b4e1d955fffe7186fd56eb42991ff6c62fa
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-chat-screen-drag-and-drop.svelte.ts
@@ -0,0 +1,72 @@
+/**
+ * Drag-and-drop state machine for the ChatScreen.
+ *
+ * Tracks pointer enter/leave nesting so the overlay stays visible while the
+ * cursor traverses child elements, then routes the dropped files either to
+ * the active message-edit handler (if a message is being edited) or to the
+ * caller's onDrop callback.
+ */
+
+import { getAddFilesHandler, isEditing } from '$lib/stores/chat.svelte';
+
+interface UseChatScreenDragAndDropOptions {
+ /** Called when the user drops files and no message is being edited. */
+ onDrop: (files: File[]) => void;
+}
+
+export function useChatScreenDragAndDrop(options: UseChatScreenDragAndDropOptions) {
+ let dragCounter = $state(0);
+ let isDragOver = $state(false);
+
+ function handleDragEnter(event: DragEvent) {
+ event.preventDefault();
+ dragCounter++;
+ if (event.dataTransfer?.types.includes('Files')) {
+ isDragOver = true;
+ }
+ }
+
+ function handleDragLeave(event: DragEvent) {
+ event.preventDefault();
+ dragCounter--;
+ if (dragCounter === 0) {
+ isDragOver = false;
+ }
+ }
+
+ function handleDragOver(event: DragEvent) {
+ event.preventDefault();
+ }
+
+ async function handleDrop(event: DragEvent) {
+ event.preventDefault();
+ isDragOver = false;
+ dragCounter = 0;
+
+ if (!event.dataTransfer?.files) return;
+
+ const files = Array.from(event.dataTransfer.files);
+
+ if (isEditing()) {
+ const handler = getAddFilesHandler();
+ if (handler) {
+ handler(files);
+ return;
+ }
+ }
+
+ options.onDrop(files);
+ }
+
+ return {
+ get isDragOver() {
+ return isDragOver;
+ },
+ dragHandlers: {
+ dragenter: handleDragEnter,
+ dragleave: handleDragLeave,
+ dragover: handleDragOver,
+ drop: handleDrop
+ }
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-chat-screen-file-upload.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-chat-screen-file-upload.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..f1ab9472184e99892925061fd2488d39bcff8aa7
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-chat-screen-file-upload.svelte.ts
@@ -0,0 +1,104 @@
+/**
+ * File upload lifecycle for the ChatScreen form.
+ *
+ * Owns the queue of processed `ChatUploadedFile`, the rejection-by-capability
+ * dialog state, and the dual-layer validation pipeline (general format +
+ * model modality). The caller provides the active model's capabilities and ID
+ * as reactive getters so validation tracks the model in real time.
+ */
+
+import { processFilesToChatUploaded } from '$lib/utils/browser-only';
+import { isFileTypeSupported, filterFilesByModalities } from '$lib/utils';
+
+interface UseChatScreenFileUploadOptions {
+ capabilities: () => { hasVision: boolean; hasAudio: boolean; hasVideo: boolean };
+ activeModelId: () => string | null | undefined;
+}
+
+export interface FileErrorData {
+ generallyUnsupported: File[];
+ modalityUnsupported: File[];
+ modalityReasons: Record;
+ supportedTypes: string[];
+}
+
+export function useChatScreenFileUpload(options: UseChatScreenFileUploadOptions) {
+ let uploadedFiles = $state([]);
+ let showFileErrorDialog = $state(false);
+ let fileErrorData = $state({
+ generallyUnsupported: [],
+ modalityUnsupported: [],
+ modalityReasons: {},
+ supportedTypes: []
+ });
+
+ async function processFiles(files: File[]) {
+ const generallySupported: File[] = [];
+ const generallyUnsupported: File[] = [];
+
+ for (const file of files) {
+ if (isFileTypeSupported(file.name, file.type)) {
+ generallySupported.push(file);
+ } else {
+ generallyUnsupported.push(file);
+ }
+ }
+
+ const { supportedFiles, unsupportedFiles, modalityReasons } = filterFilesByModalities(
+ generallySupported,
+ options.capabilities()
+ );
+
+ const allUnsupportedFiles = [...generallyUnsupported, ...unsupportedFiles];
+
+ if (allUnsupportedFiles.length > 0) {
+ const supportedTypes: string[] = ['text files', 'PDFs'];
+ const caps = options.capabilities();
+ if (caps.hasVision) supportedTypes.push('images');
+ if (caps.hasAudio) supportedTypes.push('audio files');
+ if (caps.hasVideo) supportedTypes.push('video files');
+
+ fileErrorData = {
+ generallyUnsupported,
+ modalityUnsupported: unsupportedFiles,
+ modalityReasons,
+ supportedTypes
+ };
+ showFileErrorDialog = true;
+ }
+
+ if (supportedFiles.length > 0) {
+ const processed = await processFilesToChatUploaded(
+ supportedFiles,
+ options.activeModelId() ?? undefined
+ );
+ uploadedFiles = [...uploadedFiles, ...processed];
+ }
+ }
+
+ function handleFileUpload(files: File[]) {
+ return processFiles(files);
+ }
+
+ function handleFileRemove(fileId: string) {
+ uploadedFiles = uploadedFiles.filter((f) => f.id !== fileId);
+ }
+
+ return {
+ get uploadedFiles() {
+ return uploadedFiles;
+ },
+ set uploadedFiles(value) {
+ uploadedFiles = value;
+ },
+ get showFileErrorDialog() {
+ return showFileErrorDialog;
+ },
+ set showFileErrorDialog(value) {
+ showFileErrorDialog = value;
+ },
+ fileErrorData,
+ handleFileUpload,
+ handleFileRemove
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-chat-screen-scroll.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-chat-screen-scroll.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..cecf208bdec6f4088451f0470be2d03b5720a0a0
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-chat-screen-scroll.svelte.ts
@@ -0,0 +1,47 @@
+/**
+ * Scroll container binding and navigation guard for the ChatScreen.
+ *
+ * Binds the `AutoScrollController` to `document.documentElement`, exposes
+ * the container for programmatic scrolling, and flags an `isNavigating`
+ * window during route changes so the controller can reset without its
+ * scroll handler seeing spurious events from layout shifts.
+ */
+
+import { afterNavigate, beforeNavigate } from '$app/navigation';
+import type { AutoScrollController } from './use-auto-scroll.svelte';
+
+export function useChatScreenScroll(autoScroll: AutoScrollController) {
+ let chatScrollContainer: HTMLElement | undefined = $state();
+ let isNavigating = $state(false);
+
+ function handleScroll(event: UIEvent) {
+ // Ignore scroll events caused by navigation layout changes or by our own
+ // programmatic scrolls so they don't accidentally disable auto-scroll.
+ if (isNavigating || !event.isTrusted) return;
+ autoScroll.handleScroll();
+ }
+
+ beforeNavigate(() => {
+ isNavigating = true;
+ autoScroll.resetScrollState();
+ });
+
+ afterNavigate(() => {
+ setTimeout(() => {
+ isNavigating = false;
+ autoScroll.resetScrollState();
+ }, 10);
+ });
+
+ $effect(() => {
+ chatScrollContainer = document.documentElement;
+ autoScroll.setContainer(chatScrollContainer);
+ });
+
+ return {
+ get chatScrollContainer() {
+ return chatScrollContainer;
+ },
+ handleScroll
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-draft-messages.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-draft-messages.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..11305b20552037d4936babf4adb6dc09ae652f27
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-draft-messages.svelte.ts
@@ -0,0 +1,45 @@
+import { onMount } from 'svelte';
+import { afterNavigate, beforeNavigate } from '$app/navigation';
+import { draftMessagesStore } from '$lib/stores/draft-messages.svelte';
+
+interface UseDraftMessagesOptions {
+ getChatId: () => string | undefined;
+ getMessage: () => string;
+ getFiles: () => ChatUploadedFile[];
+ setMessage: (message: string) => void;
+ setFiles: (files: ChatUploadedFile[]) => void;
+ getInitialMessage: () => string;
+}
+
+export function useDraftMessages(options: UseDraftMessagesOptions) {
+ onMount(() => {
+ const chatId = options.getChatId();
+ const draft = draftMessagesStore.getDraftMessage(chatId);
+
+ if ((draft.message || draft.files.length > 0) && !options.getInitialMessage()) {
+ options.setMessage(draft.message);
+ options.setFiles(draft.files);
+ }
+ });
+
+ beforeNavigate(() => {
+ const chatId = options.getChatId();
+ draftMessagesStore.saveDraftMessage(chatId, options.getMessage(), options.getFiles());
+ });
+
+ afterNavigate((navigation) => {
+ if (navigation?.from != null) {
+ const chatId = options.getChatId();
+ const draft = draftMessagesStore.getDraftMessage(chatId);
+ options.setMessage(draft.message);
+ options.setFiles(draft.files);
+ }
+ });
+
+ function clearDraft() {
+ const chatId = options.getChatId();
+ draftMessagesStore.clearDraftMessage(chatId);
+ }
+
+ return { clearDraft };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-keyboard-shortcuts.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-keyboard-shortcuts.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..05966a1a1d127d59670bdd99e3ec1228dae83257
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-keyboard-shortcuts.svelte.ts
@@ -0,0 +1,60 @@
+import { goto } from '$app/navigation';
+import { KeyboardKey } from '$lib/enums';
+import { ROUTES } from '$lib/constants/routes';
+
+interface KeyboardShortcutsCallbacks {
+ activateSearchMode?: () => void;
+ editActiveConversation?: () => void;
+ onSearchActivated?: () => void;
+ deleteActiveConversation?: () => void;
+ navigateToPrevConversation?: () => void;
+ navigateToNextConversation?: () => void;
+}
+
+export function useKeyboardShortcuts(callbacks: KeyboardShortcutsCallbacks) {
+ function handleKeydown(event: KeyboardEvent) {
+ const isCmdOrCtrl = event.metaKey || event.ctrlKey;
+
+ if (isCmdOrCtrl && event.key === KeyboardKey.K_LOWER) {
+ event.preventDefault();
+ callbacks.activateSearchMode?.();
+ callbacks.onSearchActivated?.();
+ }
+
+ if (
+ isCmdOrCtrl &&
+ event.shiftKey &&
+ (event.key === KeyboardKey.O_LOWER || event.key === KeyboardKey.O_UPPER)
+ ) {
+ event.preventDefault();
+
+ goto(ROUTES.NEW_CHAT);
+ }
+
+ if (event.shiftKey && isCmdOrCtrl && event.key === KeyboardKey.E_UPPER) {
+ event.preventDefault();
+ callbacks.editActiveConversation?.();
+ }
+
+ if (
+ isCmdOrCtrl &&
+ event.shiftKey &&
+ (event.key === KeyboardKey.D_LOWER || event.key === KeyboardKey.D_UPPER)
+ ) {
+ event.preventDefault();
+ callbacks.deleteActiveConversation?.();
+ }
+
+ if (isCmdOrCtrl && event.shiftKey && event.key === KeyboardKey.ARROW_UP) {
+ event.preventDefault();
+ callbacks.navigateToPrevConversation?.();
+ }
+
+ if (isCmdOrCtrl && event.shiftKey && event.key === KeyboardKey.ARROW_DOWN) {
+ event.preventDefault();
+ callbacks.navigateToNextConversation?.();
+ }
+ }
+
+ return { handleKeydown };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-mcp-recommendations.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-mcp-recommendations.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..c8a85fa8f7d5c9cc3182a342d0c4d023560b97a5
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-mcp-recommendations.svelte.ts
@@ -0,0 +1,85 @@
+import { browser } from '$app/environment';
+import {
+ MCP_SERVERS_ADDED_TO_CHAT_LOCALSTORAGE_KEY,
+ RECOMMENDED_MCP_SERVER_IDS,
+ RECOMMENDED_MCP_SERVERS_OPTIN_DIALOG_DELAY
+} from '$lib/constants';
+import { mcpStore } from '$lib/stores/mcp.svelte';
+
+/**
+ * First-run opt-in dialog for the recommended MCP servers.
+ *
+ * Owns the dismissed / open / trigger-timeout state and the effect that
+ * schedules the dialog. Reads opt-in status and the configured server list
+ * from `mcpStore`, so callers don't need to recompute on their side.
+ */
+export function useMcpRecommendations() {
+ let dismissed = $state(
+ browser && localStorage.getItem(MCP_SERVERS_ADDED_TO_CHAT_LOCALSTORAGE_KEY) === 'true'
+ );
+ let open = $state(false);
+ let checked = $state(false);
+ let triggerTimeout: ReturnType | null = null;
+
+ function dismiss() {
+ if (browser) {
+ localStorage.setItem(MCP_SERVERS_ADDED_TO_CHAT_LOCALSTORAGE_KEY, 'true');
+ }
+ dismissed = true;
+ open = false;
+ if (triggerTimeout) {
+ clearTimeout(triggerTimeout);
+ triggerTimeout = null;
+ }
+ }
+
+ function handleOpenChange(next: boolean) {
+ open = next;
+ if (!next) dismiss();
+ }
+
+ $effect(() => {
+ if (!browser) return;
+
+ if (open || dismissed) {
+ if (triggerTimeout) {
+ clearTimeout(triggerTimeout);
+ triggerTimeout = null;
+ }
+ return;
+ }
+
+ // Already evaluated once this session; leave any pending trigger alone so
+ // it can still fire later. Setting `checked = true` below re-runs this
+ // effect, and we must not wipe the timeout that was just scheduled.
+ if (checked) return;
+
+ if (mcpStore.optedInRecommendationIds.size > 0) {
+ checked = true;
+ return;
+ }
+
+ const hasRecommendations = mcpStore
+ .getServers()
+ .some((server) => RECOMMENDED_MCP_SERVER_IDS.has(server.id));
+
+ if (hasRecommendations) {
+ triggerTimeout = setTimeout(() => {
+ open = true;
+ }, RECOMMENDED_MCP_SERVERS_OPTIN_DIALOG_DELAY);
+ }
+
+ checked = true;
+ });
+
+ return {
+ get open() {
+ return open;
+ },
+ get dismissed() {
+ return dismissed;
+ },
+ dismiss,
+ handleOpenChange
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-message-edit-context.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-message-edit-context.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..71d1b66f8d3f22f038ce8ae2b9cd3657c4632233
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-message-edit-context.svelte.ts
@@ -0,0 +1,99 @@
+import { setMessageEditContext } from '$lib/contexts';
+import { MessageRole } from '$lib/enums';
+import { parseFilesToMessageExtras } from '$lib/utils/convert-files-to-extra';
+
+interface UseMessageEditContextOptions {
+ getContent: () => string;
+ getExtras: () => DatabaseMessageExtra[];
+ showSaveOnlyOption?: boolean;
+ onSave: (content: string, extras?: DatabaseMessageExtra[]) => void;
+}
+
+export function useMessageEditContext(options: UseMessageEditContextOptions) {
+ let isEditing = $state(false);
+ let editedContent = $state('');
+ let editedExtras = $state([]);
+ let editedUploadedFiles = $state([]);
+
+ function handleEdit() {
+ editedContent = options.getContent();
+ editedExtras = [...options.getExtras()];
+ editedUploadedFiles = [];
+ isEditing = true;
+ }
+
+ async function handleSaveEdit() {
+ const trimmed = editedContent.trim();
+ if (!trimmed && editedExtras.length === 0 && editedUploadedFiles.length === 0) return;
+
+ let finalExtras: DatabaseMessageExtra[] = $state.snapshot(editedExtras);
+ if (editedUploadedFiles.length > 0) {
+ const plainFiles = $state.snapshot(editedUploadedFiles);
+ const result = await parseFilesToMessageExtras(plainFiles);
+ const newExtras = result?.extras || [];
+ finalExtras = [...finalExtras, ...newExtras];
+ }
+
+ options.onSave(trimmed, finalExtras.length > 0 ? finalExtras : undefined);
+ isEditing = false;
+ }
+
+ function handleCancelEdit() {
+ isEditing = false;
+ }
+
+ setMessageEditContext({
+ get isEditing() {
+ return isEditing;
+ },
+ get editedContent() {
+ return editedContent;
+ },
+ get editedExtras() {
+ return editedExtras;
+ },
+ get editedUploadedFiles() {
+ return editedUploadedFiles;
+ },
+ get originalContent() {
+ return options.getContent();
+ },
+ get originalExtras() {
+ return options.getExtras();
+ },
+ get showSaveOnlyOption() {
+ return options.showSaveOnlyOption ?? false;
+ },
+ get showBranchAfterEditOption() {
+ return false;
+ },
+ get shouldBranchAfterEdit() {
+ return false;
+ },
+ get messageRole() {
+ return MessageRole.USER;
+ },
+ setContent: (c: string) => {
+ editedContent = c;
+ },
+ setExtras: (e: DatabaseMessageExtra[]) => {
+ editedExtras = e;
+ },
+ setUploadedFiles: (f: ChatUploadedFile[]) => {
+ editedUploadedFiles = f;
+ },
+ save: handleSaveEdit,
+ saveOnly: handleSaveEdit,
+ cancel: handleCancelEdit,
+ startEdit: handleEdit
+ });
+
+ return {
+ get isEditing() {
+ return isEditing;
+ },
+ handleEdit,
+ handleSaveEdit,
+ handleCancelEdit
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-models-selector.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-models-selector.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..9b3be15c03baa4e7254750a2f6991d8519b4c191
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-models-selector.svelte.ts
@@ -0,0 +1,273 @@
+import { onMount } from 'svelte';
+import {
+ modelsStore,
+ modelOptions,
+ modelsLoading,
+ modelsUpdating,
+ selectedModelId,
+ singleModelName
+} from '$lib/stores/models.svelte';
+import { isRouterMode } from '$lib/stores/server.svelte';
+import { filterModelOptions, groupModelOptions } from '$lib/components/app/models/utils';
+import type { ModelOption } from '$lib/types/models';
+
+export interface UseModelsSelectorOptions {
+ currentModel: () => string | null;
+ useGlobalSelection?: () => boolean;
+ onModelChange?: () =>
+ | ((modelId: string, modelName: string) => Promise | boolean | void)
+ | undefined;
+ onOpenChange?: (open: boolean) => void;
+}
+
+export interface UseModelsSelectorReturn {
+ readonly options: ModelOption[];
+ readonly loading: boolean;
+ readonly updating: boolean;
+ readonly activeId: string | null;
+ readonly isRouter: boolean;
+ readonly serverModel: string | null;
+ readonly isHighlightedCurrentModelActive: boolean;
+ readonly isCurrentModelInCache: boolean;
+ readonly filteredOptions: ModelOption[];
+ readonly groupedFilteredOptions: ReturnType;
+ readonly isLoadingModel: boolean;
+ readonly searchTerm: string;
+ readonly showModelDialog: boolean;
+ readonly infoModelId: string | null;
+ setSearchTerm(value: string): void;
+ setShowModelDialog(value: boolean): void;
+ handleInfoClick(modelName: string): void;
+ handleSelect(modelId: string): Promise;
+ handleOpenChange(open: boolean): void;
+ isFavorite(model: string): boolean;
+ getDisplayOption(): ModelOption | undefined;
+}
+
+/**
+ * Shared reactive state and logic for model selection.
+ *
+ * Used by both the desktop dropdown (`ModelsSelectorDropdown`)
+ * and the mobile sheet (`ModelsSelectorSheet`) to avoid
+ * duplicating store derivations, selection handling, and model loading.
+ */
+export function useModelsSelector(opts: UseModelsSelectorOptions): UseModelsSelectorReturn {
+ const options = $derived(
+ modelOptions().filter((option) => {
+ const modelProps = modelsStore.getModelProps(option.model);
+
+ return modelProps?.ui !== false;
+ })
+ );
+ const loading = $derived(modelsLoading());
+ const updating = $derived(modelsUpdating());
+ const activeId = $derived(selectedModelId());
+ const isRouter = $derived(isRouterMode());
+ const serverModel = $derived(singleModelName());
+
+ const currentModel = $derived(opts.currentModel());
+ const onModelChange = $derived(opts.onModelChange?.());
+
+ const isHighlightedCurrentModelActive = $derived.by(() => {
+ if (!isRouter || !currentModel) return false;
+ const currentOption = options.find((option) => option.model === currentModel);
+ return currentOption ? currentOption.id === activeId : false;
+ });
+
+ const isCurrentModelInCache = $derived.by(() => {
+ if (!isRouter || !currentModel) return true;
+ return options.some((option) => option.model === currentModel);
+ });
+
+ let isLoadingModel = $state(false);
+ let searchTerm = $state('');
+ let showModelDialog = $state(false);
+ let infoModelId = $state(null);
+ const filteredOptions = $derived(filterModelOptions(options, searchTerm));
+ const groupedFilteredOptions = $derived(
+ groupModelOptions(filteredOptions, modelsStore.favoriteModelIds, (m) =>
+ modelsStore.isModelLoaded(m)
+ )
+ );
+
+ function handleInfoClick(modelName: string) {
+ infoModelId = modelName;
+ showModelDialog = true;
+ }
+
+ onMount(() => {
+ modelsStore.fetch().catch((error) => {
+ console.error('Unable to load models:', error);
+ });
+ });
+
+ function handleOpenChange(open: boolean) {
+ if (loading || updating) return;
+
+ if (isRouter) {
+ searchTerm = '';
+
+ if (open) {
+ modelsStore.fetchRouterModels().then(() => {
+ modelsStore.fetchModalitiesForLoadedModels();
+ });
+ }
+
+ opts.onOpenChange?.(open);
+ } else {
+ showModelDialog = open;
+ }
+ }
+
+ async function handleSelect(modelId: string) {
+ const option = options.find((opt) => opt.id === modelId);
+ if (!option) return;
+
+ let shouldCloseMenu = true;
+
+ if (onModelChange) {
+ const result = await onModelChange(option.id, option.model);
+
+ if (result === false) {
+ shouldCloseMenu = false;
+ }
+ } else {
+ await modelsStore.selectModelById(option.id);
+ }
+
+ if (shouldCloseMenu) {
+ handleOpenChange(false);
+
+ requestAnimationFrame(() => {
+ const textarea = document.querySelector(
+ '[data-slot="chat-form"] textarea'
+ );
+
+ textarea?.focus({ preventScroll: true });
+ });
+ }
+
+ if (!onModelChange && isRouter && !modelsStore.isModelLoaded(option.model)) {
+ isLoadingModel = true;
+
+ modelsStore
+ .loadModel(option.model)
+ .catch((error) => console.error('Failed to load model:', error))
+ .finally(() => (isLoadingModel = false));
+ }
+ }
+
+ function getDisplayOption(): ModelOption | undefined {
+ if (!isRouter) {
+ const displayModel = serverModel || currentModel;
+
+ if (displayModel) {
+ return {
+ id: serverModel ? 'current' : 'offline-current',
+ model: displayModel,
+ name: displayModel.split('/').pop() || displayModel,
+ capabilities: []
+ };
+ }
+
+ return undefined;
+ }
+
+ if (currentModel) {
+ if (!isCurrentModelInCache) {
+ return {
+ id: 'not-in-cache',
+ model: currentModel,
+ name: currentModel.split('/').pop() || currentModel,
+ capabilities: []
+ };
+ }
+
+ return options.find((option) => option.model === currentModel);
+ }
+
+ if (activeId) {
+ return options.find((option) => option.id === activeId);
+ }
+
+ return undefined;
+ }
+
+ return {
+ get options() {
+ return options;
+ },
+
+ get loading() {
+ return loading;
+ },
+
+ get updating() {
+ return updating;
+ },
+
+ get activeId() {
+ return activeId;
+ },
+
+ get isRouter() {
+ return isRouter;
+ },
+
+ get serverModel() {
+ return serverModel;
+ },
+
+ get isHighlightedCurrentModelActive() {
+ return isHighlightedCurrentModelActive;
+ },
+
+ get isCurrentModelInCache() {
+ return isCurrentModelInCache;
+ },
+
+ get filteredOptions() {
+ return filteredOptions;
+ },
+
+ get groupedFilteredOptions() {
+ return groupedFilteredOptions;
+ },
+
+ get isLoadingModel() {
+ return isLoadingModel;
+ },
+
+ get searchTerm() {
+ return searchTerm;
+ },
+
+ get showModelDialog() {
+ return showModelDialog;
+ },
+
+ get infoModelId() {
+ return infoModelId;
+ },
+
+ setSearchTerm(value: string) {
+ searchTerm = value;
+ },
+
+ setShowModelDialog(value: boolean) {
+ showModelDialog = value;
+ },
+
+ handleInfoClick,
+
+ handleSelect,
+
+ handleOpenChange,
+
+ isFavorite(model: string) {
+ return modelsStore.favoriteModelIds.has(model);
+ },
+
+ getDisplayOption
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-processing-state.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-processing-state.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..f280319723f6faa7dfe1c9a8660f0845a0a25960
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-processing-state.svelte.ts
@@ -0,0 +1,325 @@
+import { activeProcessingState } from '$lib/stores/chat.svelte';
+import { config } from '$lib/stores/settings.svelte';
+import { STATS_UNITS } from '$lib/constants';
+import type { ApiProcessingState, LiveProcessingStats, LiveGenerationStats } from '$lib/types';
+
+export interface UseProcessingStateReturn {
+ readonly processingState: ApiProcessingState | null;
+ getProcessingDetails(): string[];
+ getTechnicalDetails(): string[];
+ getProcessingMessage(): string;
+ getPromptProgressText(): string | null;
+ getLiveProcessingStats(): LiveProcessingStats | null;
+ getLiveGenerationStats(): LiveGenerationStats | null;
+ shouldShowDetails(): boolean;
+ startMonitoring(): void;
+ stopMonitoring(): void;
+}
+
+/**
+ * useProcessingState - Reactive processing state hook
+ *
+ * This hook provides reactive access to the processing state of the server.
+ * It directly reads from chatStore's reactive state and provides
+ * formatted processing details for UI display.
+ *
+ * **Features:**
+ * - Real-time processing state via direct reactive state binding
+ * - Context and output token tracking
+ * - Tokens per second calculation
+ * - Automatic updates when streaming data arrives
+ * - Supports multiple concurrent conversations
+ *
+ * @returns Hook interface with processing state and control methods
+ */
+export function useProcessingState(): UseProcessingStateReturn {
+ let isMonitoring = $state(false);
+ let lastKnownState = $state(null);
+ let lastKnownProcessingStats = $state(null);
+
+ // Derive processing state reactively from chatStore's direct state
+ const processingState = $derived.by(() => {
+ if (!isMonitoring) {
+ return lastKnownState;
+ }
+ // Read directly from the reactive state export
+ return activeProcessingState();
+ });
+
+ // Track last known state for keepStatsVisible functionality
+ $effect(() => {
+ if (processingState && isMonitoring) {
+ lastKnownState = processingState;
+ }
+ });
+
+ // Track last known processing stats for when promptProgress disappears
+ $effect(() => {
+ if (processingState?.promptProgress) {
+ const { processed, total, time_ms, cache } = processingState.promptProgress;
+ const actualProcessed = processed - cache;
+ const actualTotal = total - cache;
+
+ if (actualProcessed > 0 && time_ms > 0) {
+ const tokensPerSecond = actualProcessed / (time_ms / 1000);
+ lastKnownProcessingStats = {
+ tokensProcessed: actualProcessed,
+ totalTokens: actualTotal,
+ timeMs: time_ms,
+ tokensPerSecond
+ };
+ }
+ }
+ });
+
+ function getETASecs(done: number, total: number, elapsedMs: number): number | undefined {
+ const elapsedSecs = elapsedMs / 1000;
+ const progressETASecs =
+ done === 0 || elapsedSecs < 0.5
+ ? undefined // can be the case for the 0% progress report
+ : elapsedSecs * (total / done - 1);
+ return progressETASecs;
+ }
+
+ function startMonitoring(): void {
+ if (isMonitoring) return;
+ isMonitoring = true;
+ }
+
+ function stopMonitoring(): void {
+ if (!isMonitoring) return;
+ isMonitoring = false;
+
+ // Only clear last known state if keepStatsVisible is disabled
+ const currentConfig = config();
+ if (!currentConfig.keepStatsVisible) {
+ lastKnownState = null;
+ lastKnownProcessingStats = null;
+ }
+ }
+
+ function getProcessingMessage(): string {
+ if (!processingState) {
+ return 'Processing...';
+ }
+
+ switch (processingState.status) {
+ case 'initializing':
+ return 'Initializing...';
+ case 'preparing':
+ if (processingState.progressPercent !== undefined) {
+ return `Processing (${processingState.progressPercent}%)`;
+ }
+ return 'Preparing response...';
+ case 'generating':
+ return '';
+ default:
+ return 'Processing...';
+ }
+ }
+
+ function getProcessingDetails(): string[] {
+ // Use current processing state or fall back to last known state
+ const stateToUse = processingState || lastKnownState;
+ if (!stateToUse) {
+ return [];
+ }
+
+ const details: string[] = [];
+
+ // Show prompt processing progress with ETA during preparation phase
+ if (stateToUse.promptProgress) {
+ const { processed, total, time_ms, cache } = stateToUse.promptProgress;
+ const actualProcessed = processed - cache;
+ const actualTotal = total - cache;
+
+ if (actualProcessed < actualTotal && actualProcessed > 0) {
+ const percent = Math.round((actualProcessed / actualTotal) * 100);
+ const eta = getETASecs(actualProcessed, actualTotal, time_ms);
+
+ if (eta !== undefined) {
+ const etaSecs = Math.ceil(eta);
+ details.push(`Processing ${percent}% (ETA: ${etaSecs}s)`);
+ } else {
+ details.push(`Processing ${percent}%`);
+ }
+ }
+ }
+
+ // Always show context info when we have valid data
+ if (
+ typeof stateToUse.contextTotal === 'number' &&
+ stateToUse.contextUsed >= 0 &&
+ stateToUse.contextTotal > 0
+ ) {
+ const contextPercent = Math.round((stateToUse.contextUsed / stateToUse.contextTotal) * 100);
+
+ details.push(
+ `Context: ${stateToUse.contextUsed}/${stateToUse.contextTotal} (${contextPercent}%)`
+ );
+ }
+
+ if (stateToUse.outputTokensUsed > 0) {
+ // Handle infinite max_tokens (-1) case
+ if (stateToUse.outputTokensMax <= 0) {
+ details.push(`Output: ${stateToUse.outputTokensUsed}/∞`);
+ } else {
+ const outputPercent = Math.round(
+ (stateToUse.outputTokensUsed / stateToUse.outputTokensMax) * 100
+ );
+
+ details.push(
+ `Output: ${stateToUse.outputTokensUsed}/${stateToUse.outputTokensMax} (${outputPercent}%)`
+ );
+ }
+ }
+
+ if (stateToUse.tokensPerSecond && stateToUse.tokensPerSecond > 0) {
+ details.push(`${stateToUse.tokensPerSecond.toFixed(1)} ${STATS_UNITS.TOKENS_PER_SECOND}`);
+ }
+
+ if (stateToUse.speculative) {
+ details.push('Speculative decoding enabled');
+ }
+
+ return details;
+ }
+
+ /**
+ * Returns technical details without the progress message (for bottom bar)
+ */
+ function getTechnicalDetails(): string[] {
+ const stateToUse = processingState || lastKnownState;
+ if (!stateToUse) {
+ return [];
+ }
+
+ const details: string[] = [];
+
+ // Always show context info when we have valid data
+ if (
+ typeof stateToUse.contextTotal === 'number' &&
+ stateToUse.contextUsed >= 0 &&
+ stateToUse.contextTotal > 0
+ ) {
+ const contextPercent = Math.round((stateToUse.contextUsed / stateToUse.contextTotal) * 100);
+
+ details.push(
+ `Context: ${stateToUse.contextUsed}/${stateToUse.contextTotal} (${contextPercent}%)`
+ );
+ }
+
+ if (stateToUse.outputTokensUsed > 0) {
+ // Handle infinite max_tokens (-1) case
+ if (stateToUse.outputTokensMax <= 0) {
+ details.push(`Output: ${stateToUse.outputTokensUsed}/∞`);
+ } else {
+ const outputPercent = Math.round(
+ (stateToUse.outputTokensUsed / stateToUse.outputTokensMax) * 100
+ );
+
+ details.push(
+ `Output: ${stateToUse.outputTokensUsed}/${stateToUse.outputTokensMax} (${outputPercent}%)`
+ );
+ }
+ }
+
+ if (stateToUse.tokensPerSecond && stateToUse.tokensPerSecond > 0) {
+ details.push(`${stateToUse.tokensPerSecond.toFixed(1)} ${STATS_UNITS.TOKENS_PER_SECOND}`);
+ }
+
+ if (stateToUse.speculative) {
+ details.push('Speculative decoding enabled');
+ }
+
+ return details;
+ }
+
+ function shouldShowDetails(): boolean {
+ return processingState !== null && processingState.status !== 'idle';
+ }
+
+ /**
+ * Returns a short progress message with percent
+ */
+ function getPromptProgressText(): string | null {
+ if (!processingState?.promptProgress) return null;
+
+ const { processed, total, cache } = processingState.promptProgress;
+
+ const actualProcessed = processed - cache;
+ const actualTotal = total - cache;
+ const percent = Math.round((actualProcessed / actualTotal) * 100);
+ const eta = getETASecs(actualProcessed, actualTotal, processingState.promptProgress.time_ms);
+
+ if (eta !== undefined) {
+ const etaSecs = Math.ceil(eta);
+ return `Processing ${percent}% (ETA: ${etaSecs}s)`;
+ }
+
+ return `Processing ${percent}%`;
+ }
+
+ /**
+ * Returns live processing statistics for display (prompt processing phase)
+ * Returns last known stats when promptProgress becomes unavailable
+ */
+ function getLiveProcessingStats(): LiveProcessingStats | null {
+ if (processingState?.promptProgress) {
+ const { processed, total, time_ms, cache } = processingState.promptProgress;
+
+ const actualProcessed = processed - cache;
+ const actualTotal = total - cache;
+
+ if (actualProcessed > 0 && time_ms > 0) {
+ const tokensPerSecond = actualProcessed / (time_ms / 1000);
+
+ return {
+ tokensProcessed: actualProcessed,
+ totalTokens: actualTotal,
+ timeMs: time_ms,
+ tokensPerSecond
+ };
+ }
+ }
+
+ // Return last known stats if promptProgress is no longer available
+ return lastKnownProcessingStats;
+ }
+
+ /**
+ * Returns live generation statistics for display (token generation phase)
+ */
+ function getLiveGenerationStats(): LiveGenerationStats | null {
+ if (!processingState) return null;
+
+ const { tokensDecoded, tokensPerSecond } = processingState;
+
+ if (tokensDecoded <= 0) return null;
+
+ // Calculate time from tokens and speed
+ const timeMs =
+ tokensPerSecond && tokensPerSecond > 0 ? (tokensDecoded / tokensPerSecond) * 1000 : 0;
+
+ return {
+ tokensGenerated: tokensDecoded,
+ timeMs,
+ tokensPerSecond: tokensPerSecond || 0
+ };
+ }
+
+ return {
+ get processingState() {
+ return processingState;
+ },
+ getProcessingDetails,
+ getTechnicalDetails,
+ getProcessingMessage,
+ getPromptProgressText,
+ getLiveProcessingStats,
+ getLiveGenerationStats,
+ shouldShowDetails,
+ startMonitoring,
+ stopMonitoring
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-pwa.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-pwa.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..e1f46e1bc2771e128037124374e9fd76b5be5cef
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-pwa.svelte.ts
@@ -0,0 +1,82 @@
+import { browser } from '$app/environment';
+import { useRegisterSW } from 'virtual:pwa-register/svelte';
+import { versionStore } from '$lib/stores/version.svelte';
+import { BUILD_VERSION_LOCALSTORAGE_KEY } from '$lib/constants/storage';
+import { SW_CONFIG } from '$lib/constants/pwa';
+
+/**
+ * Hook for PWA service worker registration, update polling, and build version mismatch detection.
+ *
+ * Combines two concerns that always belong together:
+ * 1. SW registration with periodic polling for updates
+ * 2. localStorage-based version tracking for non-PWA users
+ */
+export function usePwa() {
+ let swCheckInterval: ReturnType | null = null;
+ let needRefreshByStorage = $state(false);
+
+ const {
+ // offlineReady, // to do - add installation banners for iOS
+ needRefresh: pwaNeedRefresh,
+ updateServiceWorker
+ } = useRegisterSW({
+ onRegisteredSW(swUrl: string, r: ServiceWorkerRegistration | undefined) {
+ if (swCheckInterval) {
+ clearInterval(swCheckInterval);
+ }
+ swCheckInterval = setInterval(async () => {
+ if (!r || r.installing || !navigator?.onLine) return;
+
+ try {
+ const resp = await fetch(swUrl, {
+ cache: SW_CONFIG.UPDATE_FETCH_OPTIONS.CACHE,
+ headers: {
+ cache: SW_CONFIG.UPDATE_FETCH_OPTIONS.HEADERS.CACHE,
+ 'cache-control': SW_CONFIG.UPDATE_FETCH_OPTIONS.HEADERS.CACHE_CONTROL
+ }
+ });
+ if (resp?.status === 200) {
+ await r.update();
+ }
+ } catch (e) {
+ console.error(e);
+ }
+ }, SW_CONFIG.CHECK_INTERVAL_MS);
+ },
+ onRegisterError(error: unknown) {
+ console.error('[PWA] SW registration error:', error);
+ }
+ });
+
+ // Detect version mismatch via localStorage.
+ // _app/version.json is SvelteKit's native version file for PWA cache invalidation.
+ // This comparison detects server upgrades for non-PWA users.
+ $effect(() => {
+ if (!browser) return;
+ // PWA pages update via the service worker path; the storage check is the non-PWA fallback only
+ if (navigator.serviceWorker?.controller) return;
+
+ const currentVersion = versionStore.value;
+ if (!currentVersion) return;
+
+ try {
+ const storedVersion = localStorage.getItem(BUILD_VERSION_LOCALSTORAGE_KEY);
+ needRefreshByStorage = !!storedVersion && storedVersion !== currentVersion;
+ localStorage.setItem(BUILD_VERSION_LOCALSTORAGE_KEY, currentVersion);
+ } catch {
+ needRefreshByStorage = false;
+ }
+ });
+
+ return {
+ /** Writable that is true when a PWA service worker update is available */
+ get needRefresh() {
+ return pwaNeedRefresh;
+ },
+ updateServiceWorker,
+ /** Version mismatch detected via localStorage (non-PWA users) */
+ get needRefreshByStorage() {
+ return needRefreshByStorage;
+ }
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-scroll-carousel.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-scroll-carousel.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..e4c75d236504bd6ca84b49a4fc3b31174178018d
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-scroll-carousel.svelte.ts
@@ -0,0 +1,61 @@
+export function useScrollCarousel() {
+ let canScrollLeft = $state(false);
+ let canScrollRight = $state(false);
+ let scrollContainer = $state();
+
+ function scrollToCenter(element: HTMLElement) {
+ if (!scrollContainer) return;
+
+ const containerRect = scrollContainer.getBoundingClientRect();
+ const elementRect = element.getBoundingClientRect();
+
+ const elementCenter = elementRect.left + elementRect.width / 2;
+ const containerCenter = containerRect.left + containerRect.width / 2;
+ const scrollOffset = elementCenter - containerCenter;
+
+ scrollContainer.scrollBy({ left: scrollOffset, behavior: 'smooth' });
+ }
+
+ function scrollLeft() {
+ if (!scrollContainer) return;
+ scrollContainer.scrollBy({ left: -250, behavior: 'smooth' });
+ }
+
+ function scrollRight() {
+ if (!scrollContainer) return;
+ scrollContainer.scrollBy({ left: 250, behavior: 'smooth' });
+ }
+
+ function updateScrollButtons() {
+ if (!scrollContainer) return;
+
+ const { scrollLeft: sl, scrollWidth, clientWidth } = scrollContainer;
+ canScrollLeft = sl > 0;
+ canScrollRight = sl < scrollWidth - clientWidth - 1;
+ }
+
+ $effect(() => {
+ if (scrollContainer) {
+ updateScrollButtons();
+ }
+ });
+
+ return {
+ get canScrollLeft() {
+ return canScrollLeft;
+ },
+ get canScrollRight() {
+ return canScrollRight;
+ },
+ get scrollContainer() {
+ return scrollContainer;
+ },
+ set scrollContainer(el: HTMLDivElement | undefined) {
+ scrollContainer = el;
+ },
+ scrollToCenter,
+ scrollLeft,
+ scrollRight,
+ updateScrollButtons
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-settings-navigation.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-settings-navigation.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..3cbcaaeda5f3904894058d9d3a553ffd85e57909
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-settings-navigation.svelte.ts
@@ -0,0 +1,46 @@
+import { page } from '$app/state';
+import { beforeNavigate } from '$app/navigation';
+import { settingsReferrer } from '$lib/stores/settings-referrer.svelte';
+import { ROUTES } from '$lib/constants/routes';
+
+export interface ChatSettings {
+ reset: () => void;
+}
+
+export function useSettingsNavigation() {
+ const subroute = $state({
+ activePanel: 'chat' as 'chat' | 'settings' | 'mcp',
+ chatSettingsRef: undefined as ChatSettings | undefined
+ });
+
+ const isSettingsRoute = $derived(!!page.route.id?.startsWith('/settings'));
+
+ beforeNavigate(({ to, from }) => {
+ if (to?.route?.id?.startsWith('/settings') && !from?.route?.id?.startsWith('/settings')) {
+ settingsReferrer.url = window.location.hash || ROUTES.START;
+ }
+ });
+
+ $effect(() => {
+ if (subroute.activePanel === 'settings' && subroute.chatSettingsRef) {
+ subroute.chatSettingsRef.reset();
+ }
+ });
+
+ // Return to chat when navigating to a new route
+ $effect(() => {
+ void page.url;
+
+ subroute.activePanel = 'chat';
+ });
+
+ return {
+ get panel() {
+ return subroute;
+ },
+
+ get isSettingsRoute() {
+ return isSettingsRoute;
+ }
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-throttle.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-throttle.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..0795519787b8e539fc9ecaf56cc2ded6af4a2016
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-throttle.svelte.ts
@@ -0,0 +1,32 @@
+/**
+ * Creates a reactive throttle key that increments when `getValue()` changes
+ * and the throttle window has elapsed since the last increment.
+ *
+ * Useful for throttling animations that should not fire on every rapid update.
+ *
+ * @param getValue - A reactive getter for the value to watch
+ * @param ms - Throttle window in milliseconds
+ * @returns A reactive number that increments when the throttled value changes
+ */
+export function useThrottle(getValue: () => string | undefined, ms: number) {
+ let key = $state(0);
+ let throttleEnd = $state(0);
+ let lastValue: string | undefined = getValue();
+
+ $effect(() => {
+ const value = getValue();
+ if (value === lastValue) return;
+ const now = Date.now();
+ if (now >= throttleEnd) {
+ lastValue = value;
+ key++;
+ throttleEnd = now + ms;
+ }
+ });
+
+ return {
+ get key() {
+ return key;
+ }
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/hooks/use-tools-panel.svelte.ts b/llama.cpp/tools/ui/src/lib/hooks/use-tools-panel.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..9f99d91d9eb66cd4c71ca3bc55005c1a8278ad0c
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/hooks/use-tools-panel.svelte.ts
@@ -0,0 +1,123 @@
+import { CLI_FLAGS } from '$lib/constants';
+import { SvelteSet } from 'svelte/reactivity';
+import { ToolSource } from '$lib/enums';
+import { conversationsStore } from '$lib/stores/conversations.svelte';
+import { mcpStore } from '$lib/stores/mcp.svelte';
+import { toolsStore } from '$lib/stores/tools.svelte';
+import type { ToolGroup } from '$lib/types';
+
+export interface UseToolsPanelReturn {
+ readonly expandedGroups: SvelteSet;
+ readonly groups: ToolGroup[];
+ readonly activeGroups: ToolGroup[];
+ readonly totalToolCount: number;
+ readonly noToolsInfoMessage: string | null;
+ isGroupChecked(group: ToolGroup): boolean;
+ getEnabledToolCount(group: ToolGroup): number;
+ getFavicon(group: ToolGroup): string | null;
+ isGroupDisabled(group: ToolGroup): boolean;
+ toggleGroupExpanded(label: string): void;
+ /** Toggle all tools in a group by label (avoids stale group object references). */
+ toggleGroupByLabel(label: string): void;
+ handleOpen(): void;
+}
+
+/**
+ * Shared reactive state and helpers for the tools panel UI.
+ *
+ * Used by both the desktop dropdown (`ChatFormActionAddToolsSubmenu`)
+ * and the mobile sheet (`ChatFormActionAddSheet`) to avoid
+ * duplicating group filtering, checked-state derivation, and favicon logic.
+ */
+export function useToolsPanel(): UseToolsPanelReturn {
+ const expandedGroups = new SvelteSet();
+
+ const groups = $derived(toolsStore.toolGroups);
+ const activeGroups = $derived(
+ groups.filter(
+ (g) =>
+ g.source !== ToolSource.MCP ||
+ !g.serverId ||
+ conversationsStore.isMcpServerEnabledForChat(g.serverId)
+ )
+ );
+ const totalToolCount = $derived(activeGroups.reduce((n, g) => n + g.tools.length, 0));
+ const noToolsInfoMessage = $derived.by(() => {
+ if (toolsStore.loading) return null;
+ if (toolsStore.toolGroups.length > 0) return null;
+ // Tools endpoint is unreachable (404) — server started without --tools
+ if (toolsStore.isToolsEndpointUnreachable) {
+ return `To enable Built-In Tools you need to run llama-server with ${CLI_FLAGS.TOOLS} all or ${CLI_FLAGS.TOOLS} flag. To see MCP Tools you need to add / enable MCP Server(s).`;
+ }
+ // Other errors — return null so UI shows "Failed to load tools"
+ if (toolsStore.error) return null;
+ return `To enable Built-In Tools you need to run llama-server with ${CLI_FLAGS.TOOLS} all or ${CLI_FLAGS.TOOLS} flag. To see MCP Tools you need to add / enable MCP Server(s).`;
+ });
+
+ function isGroupChecked(group: ToolGroup): boolean {
+ return toolsStore.isGroupFullyEnabled(group);
+ }
+
+ function getEnabledToolCount(group: ToolGroup): number {
+ return group.tools.filter((tool) => toolsStore.isToolEnabled(tool.key)).length;
+ }
+
+ function getFavicon(group: ToolGroup): string | null {
+ if (group.source !== ToolSource.MCP || !group.serverId) return null;
+
+ return mcpStore.getServerFavicon(group.serverId);
+ }
+
+ function isGroupDisabled(group: ToolGroup): boolean {
+ return (
+ group.source === ToolSource.MCP &&
+ !!group.serverId &&
+ !conversationsStore.isMcpServerEnabledForChat(group.serverId)
+ );
+ }
+
+ function toggleGroupExpanded(label: string): void {
+ if (expandedGroups.has(label)) {
+ expandedGroups.delete(label);
+ } else {
+ expandedGroups.add(label);
+ }
+ }
+
+ function toggleGroupByLabel(label: string): void {
+ // Find current group by label to get up-to-date tool references
+ const group = activeGroups.find((g) => g.label === label);
+ if (!group) return;
+ toolsStore.toggleGroup(group);
+ }
+
+ function handleOpen(): void {
+ if (toolsStore.builtinTools.length === 0 && !toolsStore.loading) {
+ toolsStore.fetchBuiltinTools();
+ }
+ mcpStore.runHealthChecksForServers(mcpStore.getServersSorted().filter((s) => s.enabled));
+ }
+
+ return {
+ expandedGroups,
+ get groups() {
+ return groups;
+ },
+ get activeGroups() {
+ return activeGroups;
+ },
+ get totalToolCount() {
+ return totalToolCount;
+ },
+ get noToolsInfoMessage() {
+ return noToolsInfoMessage;
+ },
+ isGroupChecked,
+ getEnabledToolCount,
+ getFavicon,
+ isGroupDisabled,
+ toggleGroupExpanded,
+ toggleGroupByLabel,
+ handleOpen
+ };
+}
diff --git a/llama.cpp/tools/ui/src/lib/services/chat.service.ts b/llama.cpp/tools/ui/src/lib/services/chat.service.ts
new file mode 100644
index 0000000000000000000000000000000000000000..dbd0a94f10ed7677d9f4aac64165233ed6e16911
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/chat.service.ts
@@ -0,0 +1,1490 @@
+import { getAuthHeaders, getJsonHeaders } from '$lib/utils/api-headers';
+import { formatAttachmentText } from '$lib/utils/formatters';
+import { isAbortError } from '$lib/utils/abort';
+import { streamIdentity } from '$lib/utils/stream-identity';
+import {
+ ATTACHMENT_LABEL_PDF_FILE,
+ ATTACHMENT_LABEL_MCP_PROMPT,
+ ATTACHMENT_LABEL_MCP_RESOURCE,
+ LEGACY_AGENTIC_REGEX,
+ REASONING_EFFORT_TOKENS,
+ SETTINGS_KEYS,
+ API_CHAT,
+ API_SLOTS,
+ CONTROL_ACTION,
+ SSE_LINE_SEPARATOR,
+ SSE_DATA_PREFIX,
+ SSE_DONE_MARKER,
+ STREAM_VISIBILITY_KICK_MS,
+ STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX,
+ API_STREAM
+} from '$lib/constants';
+import {
+ AttachmentType,
+ ContentPartType,
+ FileTypeAudio,
+ MessageRole,
+ MimeTypeAudio,
+ ReasoningFormat,
+ StreamConnectionState
+} from '$lib/enums';
+import type {
+ ApiChatMessageContentPart,
+ ApiChatMessageData,
+ ApiChatCompletionToolCall,
+ ApiStreamSession
+} from '$lib/types/api';
+import type {
+ AudioInputFormat,
+ DatabaseMessageExtraMcpPrompt,
+ DatabaseMessageExtraMcpResource
+} from '$lib/types';
+import { modelsStore } from '$lib/stores/models.svelte';
+import { settingsStore } from '../stores/settings.svelte';
+import { capImageDataURLSize } from '../utils/cap-img-size';
+
+function getAudioInputFormat(mimeType: string): AudioInputFormat {
+ const normalizedMimeType = mimeType.trim().toLowerCase();
+
+ if (
+ normalizedMimeType === MimeTypeAudio.WAV ||
+ normalizedMimeType === MimeTypeAudio.WAVE ||
+ normalizedMimeType === MimeTypeAudio.X_WAV ||
+ normalizedMimeType === MimeTypeAudio.X_WAVE ||
+ normalizedMimeType === MimeTypeAudio.VND_WAVE ||
+ normalizedMimeType === MimeTypeAudio.X_PN_WAV
+ ) {
+ return FileTypeAudio.WAV;
+ }
+
+ return FileTypeAudio.MP3;
+}
+
+interface ResumableStreamState {
+ bytesReceived: number;
+ updatedAt: number;
+
+ // model frozen at POST time, lets a reload rebuild the exact conv::model identity the
+ // server keyed the session under. null when the POST carried no explicit model
+ model?: string | null;
+}
+
+function streamStorageKey(conversationId: string): string {
+ return STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX + conversationId;
+}
+
+export class ChatService {
+ /**
+ *
+ *
+ * Title Generation
+ *
+ *
+ */
+
+ /**
+ * Sends a streaming chat completion request for generating a chat title.
+ * Delegates to `sendMessage` for fetch, SSE parsing, and error handling.
+ *
+ * @param message - The single message to send (a user message containing the title generation prompt)
+ * @param model - Optional model name to use (required in ROUTER mode)
+ * @param signal - Optional AbortSignal to cancel the request
+ * @returns {Promise} The aggregated title text, or empty string if request failed
+ * @static
+ */
+ static async generateTitle(
+ message: ApiChatMessageData,
+ model?: string | null,
+ signal?: AbortSignal
+ ): Promise {
+ let titleResponse = '';
+ try {
+ await ChatService.sendMessage(
+ [message],
+ {
+ model: model || undefined,
+ stream: true,
+ custom: { chat_template_kwargs: { enable_thinking: false } },
+ onChunk: (chunk: string) => {
+ titleResponse += chunk;
+ }
+ },
+ undefined,
+ signal
+ );
+ } catch {
+ return '';
+ }
+ return titleResponse;
+ }
+
+ /**
+ *
+ *
+ * Messaging
+ *
+ *
+ */
+
+ /**
+ * Sends a chat completion request to the llama-server.
+ * Supports both streaming and non-streaming responses with comprehensive parameter configuration.
+ * Automatically converts database messages with attachments to the appropriate API format.
+ *
+ * @param messages - Array of chat messages to send to the API (supports both ApiChatMessageData and DatabaseMessage with attachments)
+ * @param options - Configuration options for the chat completion request. See `SettingsChatServiceOptions` type for details.
+ * @returns {Promise} that resolves to the complete response string (non-streaming) or void (streaming)
+ * @throws {Error} if the request fails or is aborted
+ */
+ static async sendMessage(
+ messages: ApiChatMessageData[] | (DatabaseMessage & { extra?: DatabaseMessageExtra[] })[],
+ options: SettingsChatServiceOptions = {},
+ conversationId?: string,
+ signal?: AbortSignal
+ ): Promise {
+ const {
+ stream,
+ onChunk,
+ onComplete,
+ onError,
+ onConnectionState,
+ onReasoningChunk,
+ onToolCallChunk,
+ onModel,
+ onCompletionId,
+ onTimings,
+ // Tools for function calling
+ tools,
+ // Generation parameters
+ temperature,
+ max_tokens,
+ // Sampling parameters
+ dynatemp_range,
+ dynatemp_exponent,
+ top_k,
+ top_p,
+ min_p,
+ xtc_probability,
+ xtc_threshold,
+ typ_p,
+ // Penalty parameters
+ repeat_last_n,
+ repeat_penalty,
+ presence_penalty,
+ frequency_penalty,
+ dry_multiplier,
+ dry_base,
+ dry_allowed_length,
+ dry_penalty_last_n,
+ // Other parameters
+ samplers,
+ backend_sampling,
+ custom,
+ timings_per_token,
+ // Config options
+ disableReasoningParsing,
+ excludeReasoningFromContext,
+ enableThinking,
+ reasoningEffort,
+ continueFinalMessage
+ } = options;
+
+ const normalizedMessages: ApiChatMessageData[] = (
+ await Promise.all(
+ messages.map((msg) => {
+ if ('id' in msg && 'convId' in msg && 'timestamp' in msg) {
+ const dbMsg = msg as DatabaseMessage & { extra?: DatabaseMessageExtra[] };
+
+ return ChatService.convertDbMessageToApiChatMessageData(dbMsg);
+ } else {
+ return msg as ApiChatMessageData;
+ }
+ })
+ )
+ ).filter((msg: { role: ChatRole; content: string | ApiChatMessageContentPart[] }) => {
+ // Filter out empty system messages
+ if (msg.role === MessageRole.SYSTEM) {
+ const content = typeof msg.content === 'string' ? msg.content : '';
+
+ return content.trim().length > 0;
+ }
+
+ return true;
+ });
+
+ // Filter out image attachments if the model doesn't support vision
+ if (options.model && !modelsStore.modelSupportsVision(options.model)) {
+ normalizedMessages.forEach((msg) => {
+ if (Array.isArray(msg.content)) {
+ msg.content = msg.content.filter((part: ApiChatMessageContentPart) => {
+ if (part.type === ContentPartType.IMAGE_URL) {
+ console.info(
+ `[ChatService] Skipping image attachment in message history (model "${options.model}" does not support vision)`
+ );
+
+ return false;
+ }
+
+ return true;
+ });
+ // If only text remains and it's a single part, simplify to string
+ if (
+ msg.content.length === 1 &&
+ msg.content[0].type === ContentPartType.TEXT &&
+ typeof msg.content[0].text === 'string'
+ ) {
+ msg.content = msg.content[0].text;
+ }
+ }
+ });
+ }
+
+ const requestBody: ApiChatCompletionRequest = {
+ messages: normalizedMessages.map((msg: ApiChatMessageData) => {
+ const mapped: ApiChatCompletionRequest['messages'][0] = {
+ role: msg.role,
+ content: msg.content,
+ tool_calls: msg.tool_calls,
+ tool_call_id: msg.tool_call_id
+ };
+ // Include reasoning_content from the dedicated field
+ if (!excludeReasoningFromContext && msg.reasoning_content) {
+ mapped.reasoning_content = msg.reasoning_content;
+ }
+ return mapped;
+ }),
+ stream,
+ return_progress: stream ? true : undefined,
+ sse_ping_interval: stream ? 1 : undefined,
+ tools: tools && tools.length > 0 ? tools : undefined
+ };
+
+ // Include model in request if provided (required in ROUTER mode)
+ if (options.model) {
+ requestBody.model = options.model;
+ }
+
+ requestBody.reasoning_format = disableReasoningParsing
+ ? ReasoningFormat.NONE
+ : ReasoningFormat.AUTO;
+
+ const reasoningBudgetTokens =
+ enableThinking && reasoningEffort ? (REASONING_EFFORT_TOKENS[reasoningEffort] ?? -1) : -1;
+
+ requestBody.chat_template_kwargs = {
+ ...(requestBody.chat_template_kwargs ?? {}),
+ enable_thinking: enableThinking
+ };
+
+ if (reasoningBudgetTokens >= 0) {
+ requestBody.thinking_budget_tokens = reasoningBudgetTokens;
+ }
+
+ // arms the budget sampler so reasoning can be ended at runtime via the control endpoint
+ requestBody.reasoning_control = true;
+
+ if (continueFinalMessage) {
+ requestBody.continue_final_message = true;
+ requestBody.add_generation_prompt = false;
+ }
+
+ if (temperature !== undefined) requestBody.temperature = temperature;
+ if (max_tokens !== undefined) {
+ // Set max_tokens to -1 (infinite) when explicitly configured as 0 or null
+ requestBody.max_tokens = max_tokens !== null && max_tokens !== 0 ? max_tokens : -1;
+ }
+
+ if (dynatemp_range !== undefined) requestBody.dynatemp_range = dynatemp_range;
+ if (dynatemp_exponent !== undefined) requestBody.dynatemp_exponent = dynatemp_exponent;
+ if (top_k !== undefined) requestBody.top_k = top_k;
+ if (top_p !== undefined) requestBody.top_p = top_p;
+ if (min_p !== undefined) requestBody.min_p = min_p;
+ if (xtc_probability !== undefined) requestBody.xtc_probability = xtc_probability;
+ if (xtc_threshold !== undefined) requestBody.xtc_threshold = xtc_threshold;
+ if (typ_p !== undefined) requestBody.typ_p = typ_p;
+
+ if (repeat_last_n !== undefined) requestBody.repeat_last_n = repeat_last_n;
+ if (repeat_penalty !== undefined) requestBody.repeat_penalty = repeat_penalty;
+ if (presence_penalty !== undefined) requestBody.presence_penalty = presence_penalty;
+ if (frequency_penalty !== undefined) requestBody.frequency_penalty = frequency_penalty;
+ if (dry_multiplier !== undefined) requestBody.dry_multiplier = dry_multiplier;
+ if (dry_base !== undefined) requestBody.dry_base = dry_base;
+ if (dry_allowed_length !== undefined) requestBody.dry_allowed_length = dry_allowed_length;
+ if (dry_penalty_last_n !== undefined) requestBody.dry_penalty_last_n = dry_penalty_last_n;
+
+ if (samplers !== undefined) {
+ requestBody.samplers =
+ typeof samplers === 'string'
+ ? samplers.split(';').filter((s: string) => s.trim())
+ : samplers;
+ }
+
+ if (backend_sampling !== undefined) requestBody.backend_sampling = backend_sampling;
+
+ if (timings_per_token !== undefined) requestBody.timings_per_token = timings_per_token;
+
+ if (custom) {
+ try {
+ const customParams = typeof custom === 'string' ? JSON.parse(custom) : custom;
+ Object.assign(requestBody, customParams);
+ } catch (error) {
+ console.warn('Failed to parse custom parameters:', error);
+ }
+ }
+
+ try {
+ const headers: Record = { ...getJsonHeaders() };
+ // tag streaming requests with the conversation id, this single header is the opt in for the
+ // server side replay buffer and powers discoverActiveStream on tab reopen. with an explicit
+ // model the ::model suffix keeps the per model session distinct
+ if (stream && conversationId) {
+ headers['X-Conversation-Id'] = streamIdentity(conversationId, options.model);
+ }
+ const response = await fetch(API_CHAT.COMPLETIONS, {
+ method: 'POST',
+ headers,
+ body: JSON.stringify(requestBody),
+ signal
+ });
+
+ if (!response.ok) {
+ const error = await ChatService.parseErrorResponse(response);
+
+ if (onError) {
+ onError(error);
+ }
+
+ throw error;
+ }
+
+ if (stream) {
+ await ChatService.handleStreamResponse(
+ response,
+ onChunk,
+ onComplete,
+ onError,
+ onReasoningChunk,
+ onToolCallChunk,
+ onModel,
+ onCompletionId,
+ onTimings,
+ conversationId,
+ signal,
+ onConnectionState,
+ options.model
+ );
+
+ return;
+ } else {
+ return ChatService.handleNonStreamResponse(
+ response,
+ onComplete,
+ onError,
+ onToolCallChunk,
+ onModel
+ );
+ }
+ } catch (error) {
+ if (isAbortError(error)) {
+ console.log('Chat completion request was aborted');
+ return;
+ }
+
+ let userFriendlyError: Error;
+
+ if (error instanceof Error) {
+ if (error.name === 'TypeError' && error.message.includes('fetch')) {
+ userFriendlyError = new Error(
+ 'Unable to connect to server - please check if the server is running'
+ );
+ userFriendlyError.name = 'NetworkError';
+ } else if (error.message.includes('ECONNREFUSED')) {
+ userFriendlyError = new Error('Connection refused - server may be offline');
+ userFriendlyError.name = 'NetworkError';
+ } else if (error.message.includes('ETIMEDOUT')) {
+ userFriendlyError = new Error('Request timed out - the server took too long to respond');
+ userFriendlyError.name = 'TimeoutError';
+ } else {
+ userFriendlyError = error;
+ }
+ } else {
+ userFriendlyError = new Error('Unknown error occurred while sending message');
+ }
+
+ console.error('Error in sendMessage:', error);
+
+ if (onError) {
+ onError(userFriendlyError);
+ }
+
+ throw userFriendlyError;
+ }
+ }
+
+ /**
+ * Checks whether all server slots are currently idle (not processing any requests).
+ * Queries the /slots endpoint (requires --slots flag on the server).
+ * Returns true if all slots are idle, false if any is processing.
+ * If the endpoint is unavailable or errors out, returns true (best-effort fallback).
+ *
+ * @param signal - Optional AbortSignal to cancel the request if needed
+ * @param model - Optional model name to check slots for (required in ROUTER mode)
+ * @returns {Promise} Promise that resolves to true if all slots are idle, false if any is processing
+ */
+ static async areAllSlotsIdle(model?: string | null, signal?: AbortSignal): Promise {
+ try {
+ const url = model ? `${API_SLOTS.LIST}?model=${encodeURIComponent(model)}` : API_SLOTS.LIST;
+ const res = await fetch(url, { signal });
+ if (!res.ok) return true;
+
+ const slots: { is_processing: boolean }[] = await res.json();
+ return slots.every((s) => !s.is_processing);
+ } catch {
+ return true;
+ }
+ }
+
+ /**
+ * Ends the current reasoning block of a running completion, targeted by its
+ * chat completion id (streamed back as `id`). Matching the completion rather
+ * than a slot index avoids a TOCTOU: a finished completion simply matches
+ * nothing server side. The model is carried so the router forwards to the
+ * right child, single model ignores it. Returns true on success.
+ */
+ static async stopReasoning(completionId: string, model?: string | null): Promise {
+ if (!completionId) {
+ console.error(
+ 'stopReasoning: no completion id for the active message, cannot target the running completion'
+ );
+ return false;
+ }
+
+ const body: Record = {
+ id: completionId,
+ action: CONTROL_ACTION.END_REASONING
+ };
+ if (model) body.model = model;
+
+ try {
+ const res = await fetch(API_CHAT.CONTROL, {
+ method: 'POST',
+ headers: getJsonHeaders(),
+ body: JSON.stringify(body)
+ });
+
+ const data = await res.json().catch(() => null);
+ if (!res.ok || data?.success !== true) {
+ console.error('stopReasoning: control request failed', {
+ status: res.status,
+ completionId,
+ response: data
+ });
+ return false;
+ }
+ return true;
+ } catch (error) {
+ console.error('stopReasoning: control request threw', { completionId, error });
+ return false;
+ }
+ }
+
+ /**
+ * Sends a fire-and-forget request to pre-encode the conversation in the server's KV cache.
+ * After a response completes, this re-submits the full conversation
+ * using n_predict=0 and stream=false so the server processes the prompt without generating tokens.
+ * This warms the cache for the next turn, making it faster.
+ *
+ * When excludeReasoningFromContext is true, reasoning content is stripped from the messages
+ * to match what sendMessage would send on the next turn (avoiding cache misses).
+ * When false, reasoning_content is preserved so the cached prompt matches the next request.
+ *
+ * @param messages - The full conversation including the latest assistant response
+ * @param model - Optional model name (required in ROUTER mode)
+ * @param excludeReasoning - Whether to strip reasoning content (should match excludeReasoningFromContext setting)
+ * @param signal - Optional AbortSignal to cancel the pre-encode request
+ */
+ static async cancelServerStream(conversationId: string, model?: string | null): Promise {
+ if (!conversationId) return;
+ try {
+ const id = streamIdentity(conversationId, model);
+ await fetch(`${API_STREAM.BASE}/${encodeURIComponent(id)}`, {
+ method: 'DELETE',
+ headers: getAuthHeaders()
+ });
+ } catch (e) {
+ console.warn('cancelServerStream failed:', e);
+ }
+ }
+
+ /**
+ * Pick the running session to splice into when discoverActiveStream lists candidates for a
+ * conversation. Finalized sessions are not candidates: their final content was already written
+ * to the DB by the original onComplete handler, so attaching to them would replay a buffer that
+ * may not match what the DB holds. A continue session's buffer holds only the appended deltas,
+ * not the pre continue prefix, so replaying it as a fresh generation would erase the original.
+ *
+ * Among running sessions we tie break on the most recent started_at, which covers the case of
+ * multiple inferences left running on the same conversation.
+ */
+ static selectActiveStream(
+ sessions: ApiStreamSession[] | null | undefined
+ ): ApiStreamSession | null {
+ if (!Array.isArray(sessions) || sessions.length === 0) {
+ return null;
+ }
+ const running = sessions.filter((s) => !s.is_done);
+ if (running.length === 0) {
+ return null;
+ }
+ return running.reduce((best, cur) => (cur.started_at > best.started_at ? cur : best));
+ }
+
+ // persist the running byte count and the frozen model for a conversation, a later visit
+ // resumes the SSE replay at the right offset under the same conv::model identity
+ static saveStreamState(
+ conversationId: string,
+ bytesReceived: number,
+ model?: string | null
+ ): void {
+ if (!conversationId) return;
+ try {
+ const state: ResumableStreamState = {
+ bytesReceived,
+ updatedAt: Date.now(),
+ model: model ?? null
+ };
+ localStorage.setItem(streamStorageKey(conversationId), JSON.stringify(state));
+ } catch {
+ // localStorage may be full or disabled, silently ignore
+ }
+ }
+
+ static getStreamState(conversationId: string): ResumableStreamState | null {
+ if (!conversationId) return null;
+ try {
+ const raw = localStorage.getItem(streamStorageKey(conversationId));
+ if (!raw) return null;
+ const parsed = JSON.parse(raw) as ResumableStreamState;
+ if (!parsed || typeof parsed.bytesReceived !== 'number') return null;
+ return parsed;
+ } catch {
+ return null;
+ }
+ }
+
+ static clearStreamState(conversationId: string): void {
+ if (!conversationId) return;
+ try {
+ localStorage.removeItem(streamStorageKey(conversationId));
+ } catch {
+ // nothing to do
+ }
+ }
+
+ /**
+ * Rebuild the stream identity for a resume. The model persisted at POST time wins, including a
+ * stored null which means the POST carried no explicit model so the identity stays the bare conv
+ * id. Only fall back to the caller supplied current model when nothing was persisted.
+ */
+ static resumeStreamIdentity(
+ conversationId: string,
+ state: ResumableStreamState | null,
+ fallbackModel: string | null
+ ): string {
+ const model = state && state.model !== undefined ? state.model : fallbackModel;
+ return streamIdentity(conversationId, model);
+ }
+
+ /**
+ * Reconnect to an interrupted stream for this conversation. Returns the fetch Response so the
+ * existing SSE parser drains it like a fresh stream. The server returns 200 on success, 404 if
+ * no session exists for the conv_id, and 400 if the offset is below the dropped prefix.
+ */
+ static async resumeStream(
+ conversationId: string,
+ signal?: AbortSignal,
+ model?: string | null
+ ): Promise {
+ if (!conversationId) return null;
+ const state = ChatService.getStreamState(conversationId);
+ const from = state?.bytesReceived ?? 0;
+ const id = streamIdentity(conversationId, model);
+ const url = `${API_STREAM.BASE}/${encodeURIComponent(id)}?from=${from}`;
+ return await fetch(url, { method: 'GET', signal, headers: getAuthHeaders() });
+ }
+
+ static async preEncode(
+ messages: ApiChatMessageData[] | (DatabaseMessage & { extra?: DatabaseMessageExtra[] })[],
+ model?: string | null,
+ excludeReasoning?: boolean,
+ signal?: AbortSignal
+ ): Promise {
+ const normalizedMessages: ApiChatMessageData[] = (
+ await Promise.all(
+ messages.map((msg) => {
+ if ('id' in msg && 'convId' in msg && 'timestamp' in msg) {
+ return ChatService.convertDbMessageToApiChatMessageData(
+ msg as DatabaseMessage & { extra?: DatabaseMessageExtra[] }
+ );
+ }
+
+ return msg as ApiChatMessageData;
+ })
+ )
+ ).filter((msg: { role: ChatRole; content: string | ApiChatMessageContentPart[] }) => {
+ if (msg.role === MessageRole.SYSTEM) {
+ const content = typeof msg.content === 'string' ? msg.content : '';
+
+ return content.trim().length > 0;
+ }
+
+ return true;
+ });
+
+ const requestBody: Record = {
+ messages: normalizedMessages.map((msg: ApiChatMessageData) => {
+ const mapped: Record = {
+ role: msg.role,
+ content: excludeReasoning ? ChatService.stripReasoningContent(msg.content) : msg.content,
+ tool_calls: msg.tool_calls,
+ tool_call_id: msg.tool_call_id
+ };
+
+ if (!excludeReasoning && msg.reasoning_content) {
+ mapped.reasoning_content = msg.reasoning_content;
+ }
+
+ return mapped;
+ }),
+ stream: false,
+ n_predict: 0
+ };
+
+ if (model) {
+ requestBody.model = model;
+ }
+
+ try {
+ await fetch(API_CHAT.COMPLETIONS, {
+ method: 'POST',
+ headers: getJsonHeaders(),
+ body: JSON.stringify(requestBody),
+ signal
+ });
+ } catch (error) {
+ if (!isAbortError(error)) {
+ console.warn('[ChatService] Pre-encode request failed:', error);
+ }
+ }
+ }
+
+ /**
+ *
+ *
+ * Streaming
+ *
+ *
+ */
+
+ /**
+ * Handles streaming response from the chat completion API
+ * @param response - The Response object from the fetch request
+ * @param onChunk - Optional callback invoked for each content chunk received
+ * @param onComplete - Optional callback invoked when the stream is complete with full response
+ * @param onError - Optional callback invoked if an error occurs during streaming
+ * @param onReasoningChunk - Optional callback invoked for each reasoning content chunk
+ * @param conversationId - Optional conversation ID for per-conversation state tracking
+ * @returns {Promise} Promise that resolves when streaming is complete
+ * @throws {Error} if the stream cannot be read or parsed
+ */
+ static async handleStreamResponse(
+ response: Response,
+ onChunk?: (chunk: string) => void,
+ onComplete?: (
+ response: string,
+ reasoningContent?: string,
+ timings?: ChatMessageTimings,
+ toolCalls?: string
+ ) => void,
+ onError?: (error: Error) => void,
+ onReasoningChunk?: (chunk: string) => void,
+ onToolCallChunk?: (chunk: string) => void,
+ onModel?: (model: string) => void,
+ onCompletionId?: (id: string) => void,
+ onTimings?: (timings?: ChatMessageTimings, promptProgress?: ChatMessagePromptProgress) => void,
+ conversationId?: string,
+ abortSignal?: AbortSignal,
+ onConnectionState?: (state: StreamConnectionState) => void,
+ streamModel?: string | null
+ ): Promise {
+ let reader = response.body?.getReader();
+
+ if (!reader) {
+ throw new Error('No response body');
+ }
+
+ // bytesParsed is the absolute server side buffer offset of the next byte to parse
+ // segmentStartOffset is the absolute offset where the current reader started, reset on resume
+ // segmentBytesRead is wire bytes read by the current reader
+ let bytesParsed = 0;
+ let segmentStartOffset = 0;
+ let segmentBytesRead = 0;
+ let lastByteAt = Date.now();
+ // each resume must produce at least one byte to be retried again
+ // if a resume returns 200 but yields nothing, we abandon
+ // since the session has a bounded size, the total number of retries is bounded by construction
+ let madeProgress = true;
+ const encoder = new TextEncoder();
+ if (conversationId) {
+ ChatService.saveStreamState(conversationId, 0, streamModel);
+ }
+ onConnectionState?.(StreamConnectionState.STREAMING);
+
+ let decoder = new TextDecoder();
+ let aggregatedContent = '';
+ let fullReasoningContent = '';
+ let aggregatedToolCalls: ApiChatCompletionToolCall[] = [];
+ let lastTimings: ChatMessageTimings | undefined;
+ let streamFinished = false;
+ let modelEmitted = false;
+ let idEmitted = false;
+ let toolCallIndexOffset = 0;
+ let hasOpenToolCallBatch = false;
+
+ const finalizeOpenToolCallBatch = () => {
+ if (!hasOpenToolCallBatch) {
+ return;
+ }
+
+ toolCallIndexOffset = aggregatedToolCalls.length;
+ hasOpenToolCallBatch = false;
+ };
+
+ const processToolCallDelta = (toolCalls?: ApiChatCompletionToolCallDelta[]) => {
+ if (!toolCalls || toolCalls.length === 0) {
+ return;
+ }
+
+ aggregatedToolCalls = ChatService.mergeToolCallDeltas(
+ aggregatedToolCalls,
+ toolCalls,
+ toolCallIndexOffset
+ );
+
+ if (aggregatedToolCalls.length === 0) {
+ return;
+ }
+
+ hasOpenToolCallBatch = true;
+
+ const serializedToolCalls = JSON.stringify(aggregatedToolCalls);
+
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG) {
+ console.log('[ChatService] Aggregated tool calls:', serializedToolCalls);
+ }
+
+ if (!serializedToolCalls) {
+ return;
+ }
+
+ if (!abortSignal?.aborted) {
+ onToolCallChunk?.(serializedToolCalls);
+ }
+ };
+
+ const onVisibilityChange = () => {
+ if (typeof document === 'undefined') return;
+ if (document.visibilityState !== 'visible') return;
+ if (streamFinished) return;
+ if (!conversationId) return;
+ // the bytes have been quiet for too long, the OS likely killed the socket
+ // kicking the reader unblocks reader.read with done=true so the outer loop can resume
+ if (Date.now() - lastByteAt > STREAM_VISIBILITY_KICK_MS) {
+ reader!.cancel().catch(() => {});
+ }
+ };
+ if (typeof document !== 'undefined') {
+ document.addEventListener('visibilitychange', onVisibilityChange);
+ }
+
+ try {
+ let chunk = '';
+ // outer loop drives the resume cycle, swaps reader on premature end of stream
+ while (true) {
+ while (true) {
+ if (abortSignal?.aborted) break;
+
+ let done: boolean;
+ let value: Uint8Array | undefined;
+ try {
+ const r = await reader.read();
+ done = r.done;
+ value = r.value;
+ } catch (readErr) {
+ // reader.read() rejects with TypeError when the underlying connection drops
+ // instead of just resolving with done=true. treat it like done so the outer
+ // loop swaps reader via the resume path
+ if (isAbortError(readErr)) {
+ throw readErr;
+ }
+ console.warn('reader.read() rejected, treating as premature end:', readErr);
+ done = true;
+ value = undefined;
+ }
+ if (done) break;
+
+ if (abortSignal?.aborted) break;
+
+ if (value && value.byteLength > 0) {
+ segmentBytesRead += value.byteLength;
+ lastByteAt = Date.now();
+ if (!madeProgress) {
+ madeProgress = true;
+ onConnectionState?.(StreamConnectionState.STREAMING);
+ }
+ }
+
+ chunk += decoder.decode(value, { stream: true });
+ const lines = chunk.split(SSE_LINE_SEPARATOR);
+ chunk = lines.pop() || '';
+
+ // the persisted offset must point right after the last fully parsed line,
+ // the trailing `chunk` is partial bytes still waiting for a newline
+ if (conversationId) {
+ const tailBytes = encoder.encode(chunk).byteLength;
+ bytesParsed = segmentStartOffset + segmentBytesRead - tailBytes;
+ ChatService.saveStreamState(conversationId, bytesParsed, streamModel);
+ }
+
+ for (const line of lines) {
+ if (abortSignal?.aborted) break;
+
+ if (line.startsWith(SSE_DATA_PREFIX)) {
+ const data = line.slice(SSE_DATA_PREFIX.length).trim();
+ if (data === SSE_DONE_MARKER) {
+ streamFinished = true;
+
+ continue;
+ }
+
+ try {
+ const parsed: ApiChatCompletionStreamChunk = JSON.parse(data);
+ const choice = parsed.choices?.[0];
+ const content = choice?.delta?.content;
+ const reasoningContent = choice?.delta?.reasoning_content;
+ const toolCalls = choice?.delta?.tool_calls;
+ const timings = parsed.timings;
+ const promptProgress = parsed.prompt_progress;
+
+ const chunkModel = ChatService.extractModelName(parsed);
+ if (chunkModel && !modelEmitted) {
+ modelEmitted = true;
+ onModel?.(chunkModel);
+ }
+
+ if (parsed.id && !idEmitted) {
+ idEmitted = true;
+ onCompletionId?.(parsed.id);
+ }
+
+ if (promptProgress) {
+ ChatService.notifyTimings(undefined, promptProgress, onTimings);
+ }
+
+ if (timings) {
+ ChatService.notifyTimings(timings, promptProgress, onTimings);
+ lastTimings = timings;
+ }
+
+ if (content) {
+ finalizeOpenToolCallBatch();
+ aggregatedContent += content;
+ if (!abortSignal?.aborted) {
+ onChunk?.(content);
+ }
+ }
+
+ if (reasoningContent) {
+ finalizeOpenToolCallBatch();
+ fullReasoningContent += reasoningContent;
+ if (!abortSignal?.aborted) {
+ onReasoningChunk?.(reasoningContent);
+ }
+ }
+
+ processToolCallDelta(toolCalls);
+ } catch (e) {
+ console.error('Error parsing JSON chunk:', e);
+ }
+ }
+ }
+
+ if (abortSignal?.aborted) break;
+ if (streamFinished) break;
+ }
+
+ // inner reader done, decide whether to try a resume
+ if (abortSignal?.aborted) break;
+ if (streamFinished) break;
+ if (!conversationId) break;
+
+ if (!madeProgress) {
+ onConnectionState?.(StreamConnectionState.LOST);
+ onError?.(new Error('Stream resume produced no new bytes, giving up'));
+ break;
+ }
+
+ onConnectionState?.(StreamConnectionState.RESUMING);
+ madeProgress = false;
+
+ // the server resends starting at bytesParsed, discard any partial line we held, it
+ // will be retransmitted from a clean line boundary. reuse the frozen model, not the
+ // live dropdown
+ const resumeResp = await ChatService.resumeStream(
+ conversationId,
+ abortSignal,
+ streamModel
+ ).catch(() => null);
+ // an abort landing during the resume request is intentional, not a lost connection
+ if (abortSignal?.aborted) break;
+ if (!resumeResp || resumeResp.status !== 200) {
+ onConnectionState?.(StreamConnectionState.LOST);
+ onError?.(new Error('Stream connection lost and could not be resumed'));
+ break;
+ }
+ const newReader = resumeResp.body?.getReader();
+ if (!newReader) break;
+
+ try {
+ reader.releaseLock();
+ } catch {
+ /* ignore */
+ }
+ reader = newReader;
+ decoder = new TextDecoder();
+ chunk = '';
+ segmentStartOffset = bytesParsed;
+ segmentBytesRead = 0;
+ lastByteAt = Date.now();
+ }
+
+ if (abortSignal?.aborted) return;
+
+ if (streamFinished) {
+ finalizeOpenToolCallBatch();
+
+ if (conversationId) {
+ ChatService.clearStreamState(conversationId);
+ }
+
+ const finalToolCalls =
+ aggregatedToolCalls.length > 0 ? JSON.stringify(aggregatedToolCalls) : undefined;
+
+ onComplete?.(
+ aggregatedContent,
+ fullReasoningContent || undefined,
+ lastTimings,
+ finalToolCalls
+ );
+ }
+ } catch (error) {
+ const err = error instanceof Error ? error : new Error('Stream error');
+
+ onError?.(err);
+
+ throw err;
+ } finally {
+ if (typeof document !== 'undefined') {
+ document.removeEventListener('visibilitychange', onVisibilityChange);
+ }
+ try {
+ reader.releaseLock();
+ } catch {
+ /* ignore */
+ }
+ }
+ }
+
+ /**
+ * Handles non-streaming response from the chat completion API.
+ * Parses the JSON response and extracts the generated content.
+ *
+ * @param response - The fetch Response object containing the JSON data
+ * @param onComplete - Optional callback invoked when response is successfully parsed
+ * @param onError - Optional callback invoked if an error occurs during parsing
+ * @returns {Promise} Promise that resolves to the generated content string
+ * @throws {Error} if the response cannot be parsed or is malformed
+ */
+ private static async handleNonStreamResponse(
+ response: Response,
+ onComplete?: (
+ response: string,
+ reasoningContent?: string,
+ timings?: ChatMessageTimings,
+ toolCalls?: string
+ ) => void,
+ onError?: (error: Error) => void,
+ onToolCallChunk?: (chunk: string) => void,
+ onModel?: (model: string) => void
+ ): Promise {
+ try {
+ const responseText = await response.text();
+
+ if (!responseText.trim()) {
+ const noResponseError = new Error('No response received from server. Please try again.');
+
+ throw noResponseError;
+ }
+
+ const data: ApiChatCompletionResponse = JSON.parse(responseText);
+
+ const responseModel = ChatService.extractModelName(data);
+ if (responseModel) {
+ onModel?.(responseModel);
+ }
+
+ const content = data.choices[0]?.message?.content || '';
+ const reasoningContent = data.choices[0]?.message?.reasoning_content;
+ const toolCalls = data.choices[0]?.message?.tool_calls;
+
+ let serializedToolCalls: string | undefined;
+
+ if (toolCalls && toolCalls.length > 0) {
+ const mergedToolCalls = ChatService.mergeToolCallDeltas([], toolCalls);
+
+ if (mergedToolCalls.length > 0) {
+ serializedToolCalls = JSON.stringify(mergedToolCalls);
+ if (serializedToolCalls) {
+ onToolCallChunk?.(serializedToolCalls);
+ }
+ }
+ }
+
+ if (!content.trim() && !serializedToolCalls) {
+ const noResponseError = new Error('No response received from server. Please try again.');
+
+ throw noResponseError;
+ }
+
+ onComplete?.(content, reasoningContent, undefined, serializedToolCalls);
+
+ return content;
+ } catch (error) {
+ const err = error instanceof Error ? error : new Error('Parse error');
+
+ onError?.(err);
+
+ throw err;
+ }
+ }
+
+ /**
+ * Merges tool call deltas into an existing array of tool calls.
+ * Handles both existing and new tool calls, updating existing ones and adding new ones.
+ *
+ * @param existing - The existing array of tool calls to merge into
+ * @param deltas - The array of tool call deltas to merge
+ * @param indexOffset - Optional offset to apply to the index of new tool calls
+ * @returns {ApiChatCompletionToolCall[]} The merged array of tool calls
+ */
+ private static mergeToolCallDeltas(
+ existing: ApiChatCompletionToolCall[],
+ deltas: ApiChatCompletionToolCallDelta[],
+ indexOffset = 0
+ ): ApiChatCompletionToolCall[] {
+ const result = existing.map((call) => ({
+ ...call,
+ function: call.function ? { ...call.function } : undefined
+ }));
+
+ for (const delta of deltas) {
+ const index =
+ typeof delta.index === 'number' && delta.index >= 0
+ ? delta.index + indexOffset
+ : result.length;
+
+ while (result.length <= index) {
+ result.push({ function: undefined });
+ }
+
+ const target = result[index]!;
+
+ if (delta.id) {
+ target.id = delta.id;
+ }
+
+ if (delta.type) {
+ target.type = delta.type;
+ }
+
+ if (delta.function) {
+ const fn = target.function ? { ...target.function } : {};
+
+ if (delta.function.name) {
+ fn.name = delta.function.name;
+ }
+
+ if (delta.function.arguments) {
+ fn.arguments = (fn.arguments ?? '') + delta.function.arguments;
+ }
+
+ target.function = fn;
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ *
+ *
+ * Conversion
+ *
+ *
+ */
+
+ /**
+ * Converts a database message with attachments to API chat message format.
+ * Processes various attachment types (images, text files, PDFs) and formats them
+ * as content parts suitable for the chat completion API.
+ *
+ * @param message - Database message object with optional extra attachments
+ * @param message.content - The text content of the message
+ * @param message.role - The role of the message sender (user, assistant, system)
+ * @param message.extra - Optional array of message attachments (images, files, etc.)
+ * @returns {ApiChatMessageData} object formatted for the chat completion API
+ * @static
+ */
+ static async convertDbMessageToApiChatMessageData(
+ message: DatabaseMessage & { extra?: DatabaseMessageExtra[] }
+ ): Promise {
+ // Handle tool result messages (role: 'tool')
+ if (message.role === MessageRole.TOOL && message.toolCallId) {
+ return {
+ role: MessageRole.TOOL,
+ content: message.content,
+ tool_call_id: message.toolCallId
+ };
+ }
+
+ // Parse tool calls for assistant messages
+ let toolCalls: ApiChatCompletionToolCall[] | undefined;
+ if (message.toolCalls) {
+ try {
+ toolCalls = JSON.parse(message.toolCalls);
+ } catch {
+ // Ignore parse errors for malformed tool calls
+ }
+ }
+
+ if (!message.extra || message.extra.length === 0) {
+ const result: ApiChatMessageData = {
+ role: message.role as MessageRole,
+ content: message.content
+ };
+
+ if (message.reasoningContent) {
+ result.reasoning_content = message.reasoningContent;
+ }
+
+ if (toolCalls && toolCalls.length > 0) {
+ result.tool_calls = toolCalls;
+ }
+
+ return result;
+ }
+
+ const contentParts: ApiChatMessageContentPart[] = [];
+
+ const textFiles = message.extra.filter(
+ (extra: DatabaseMessageExtra): extra is DatabaseMessageExtraTextFile =>
+ extra.type === AttachmentType.TEXT
+ );
+
+ for (const textFile of textFiles) {
+ contentParts.push({
+ type: ContentPartType.TEXT,
+ text: formatAttachmentText('File', textFile.name, textFile.content)
+ });
+ }
+
+ // Handle legacy 'context' type from the old UI (pasted content)
+ const legacyContextFiles = message.extra.filter(
+ (extra: DatabaseMessageExtra): extra is DatabaseMessageExtraLegacyContext =>
+ extra.type === AttachmentType.LEGACY_CONTEXT
+ );
+
+ for (const legacyContextFile of legacyContextFiles) {
+ contentParts.push({
+ type: ContentPartType.TEXT,
+ text: formatAttachmentText('File', legacyContextFile.name, legacyContextFile.content)
+ });
+ }
+
+ const imageFiles = message.extra.filter(
+ (extra: DatabaseMessageExtra): extra is DatabaseMessageExtraImageFile =>
+ extra.type === AttachmentType.IMAGE
+ );
+
+ for (const image of imageFiles) {
+ const maxImageResolution = settingsStore.getConfig(SETTINGS_KEYS.MAX_IMAGE_RESOLUTION);
+
+ // Caps the resolution and bakes the jpeg exif orientation in one pass,
+ // untouched images pass through as is
+ const base64Url = await capImageDataURLSize(image.base64Url, maxImageResolution);
+
+ contentParts.push({
+ type: ContentPartType.IMAGE_URL,
+ image_url: { url: base64Url }
+ });
+ }
+
+ const audioFiles = message.extra.filter(
+ (extra: DatabaseMessageExtra): extra is DatabaseMessageExtraAudioFile =>
+ extra.type === AttachmentType.AUDIO
+ );
+
+ for (const audio of audioFiles) {
+ contentParts.push({
+ type: ContentPartType.INPUT_AUDIO,
+ input_audio: {
+ data: audio.base64Data,
+ format: getAudioInputFormat(audio.mimeType)
+ }
+ });
+ }
+
+ if (message.content) {
+ contentParts.push({
+ type: ContentPartType.TEXT,
+ text: message.content
+ });
+ }
+
+ const videoFiles = message.extra.filter(
+ (extra: DatabaseMessageExtra): extra is DatabaseMessageExtraVideoFile =>
+ extra.type === AttachmentType.VIDEO
+ );
+
+ for (const video of videoFiles) {
+ contentParts.push({
+ type: ContentPartType.INPUT_VIDEO,
+ input_video: {
+ data: video.base64Data,
+ format: video.mimeType.includes('mp4')
+ ? 'mp4'
+ : video.mimeType.includes('ogg')
+ ? 'ogg'
+ : 'auto'
+ }
+ });
+ }
+
+ const pdfFiles = message.extra.filter(
+ (extra: DatabaseMessageExtra): extra is DatabaseMessageExtraPdfFile =>
+ extra.type === AttachmentType.PDF
+ );
+
+ for (const pdfFile of pdfFiles) {
+ if (pdfFile.processedAsImages && pdfFile.images) {
+ for (let i = 0; i < pdfFile.images.length; i++) {
+ contentParts.push({
+ type: ContentPartType.IMAGE_URL,
+ image_url: { url: pdfFile.images[i] }
+ });
+ }
+ } else {
+ contentParts.push({
+ type: ContentPartType.TEXT,
+ text: formatAttachmentText(ATTACHMENT_LABEL_PDF_FILE, pdfFile.name, pdfFile.content)
+ });
+ }
+ }
+
+ const mcpPrompts = message.extra.filter(
+ (extra: DatabaseMessageExtra): extra is DatabaseMessageExtraMcpPrompt =>
+ extra.type === AttachmentType.MCP_PROMPT
+ );
+
+ for (const mcpPrompt of mcpPrompts) {
+ contentParts.push({
+ type: ContentPartType.TEXT,
+ text: formatAttachmentText(
+ ATTACHMENT_LABEL_MCP_PROMPT,
+ mcpPrompt.name,
+ mcpPrompt.content,
+ mcpPrompt.serverName
+ )
+ });
+ }
+
+ const mcpResources = message.extra.filter(
+ (extra: DatabaseMessageExtra): extra is DatabaseMessageExtraMcpResource =>
+ extra.type === AttachmentType.MCP_RESOURCE
+ );
+
+ for (const mcpResource of mcpResources) {
+ contentParts.push({
+ type: ContentPartType.TEXT,
+ text: formatAttachmentText(
+ ATTACHMENT_LABEL_MCP_RESOURCE,
+ mcpResource.name,
+ mcpResource.content,
+ mcpResource.serverName
+ )
+ });
+ }
+
+ const result: ApiChatMessageData = {
+ role: message.role as MessageRole,
+ content: contentParts
+ };
+ if (message.reasoningContent) {
+ result.reasoning_content = message.reasoningContent;
+ }
+ if (toolCalls && toolCalls.length > 0) {
+ result.tool_calls = toolCalls;
+ }
+ return result;
+ }
+
+ /**
+ *
+ *
+ * Utilities
+ *
+ *
+ */
+
+ /**
+ * Strips legacy inline reasoning content tags from message content.
+ * Handles both plain string content and multipart content arrays.
+ */
+ private static stripReasoningContent(
+ content: string | ApiChatMessageContentPart[]
+ ): string | ApiChatMessageContentPart[] {
+ const stripFromString = (text: string): string =>
+ text.replace(LEGACY_AGENTIC_REGEX.REASONING_BLOCK, '').trim();
+
+ if (typeof content === 'string') {
+ return stripFromString(content);
+ }
+
+ return content.map((part) => {
+ if (part.type === ContentPartType.TEXT && part.text) {
+ return { ...part, text: stripFromString(part.text) };
+ }
+ return part;
+ });
+ }
+
+ /**
+ * Parses error response and creates appropriate error with context information
+ * @param response - HTTP response object
+ * @returns Promise - Parsed error with context info if available
+ */
+ private static async parseErrorResponse(
+ response: Response
+ ): Promise {
+ try {
+ const errorText = await response.text();
+ const errorData: ApiErrorResponse = JSON.parse(errorText);
+
+ const message = errorData.error?.message || 'Unknown server error';
+ const error = new Error(message) as Error & {
+ contextInfo?: { n_prompt_tokens: number; n_ctx: number };
+ };
+ error.name = response.status === 400 ? 'ServerError' : 'HttpError';
+
+ if (errorData.error && 'n_prompt_tokens' in errorData.error && 'n_ctx' in errorData.error) {
+ error.contextInfo = {
+ n_prompt_tokens: errorData.error.n_prompt_tokens,
+ n_ctx: errorData.error.n_ctx
+ };
+ }
+
+ return error;
+ } catch {
+ const fallback = new Error(
+ `Server error (${response.status}): ${response.statusText}`
+ ) as Error & {
+ contextInfo?: { n_prompt_tokens: number; n_ctx: number };
+ };
+ fallback.name = 'HttpError';
+
+ return fallback;
+ }
+ }
+
+ /**
+ * Extracts model name from Chat Completions API response data.
+ * Handles various response formats including streaming chunks and final responses.
+ *
+ * WORKAROUND: In single model mode, llama-server returns a default/incorrect model name
+ * in the response. We override it with the actual model name from serverStore.
+ *
+ * @param data - Raw response data from the Chat Completions API
+ * @returns Model name string if found, undefined otherwise
+ * @private
+ */
+ private static extractModelName(data: unknown): string | undefined {
+ const asRecord = (value: unknown): Record | undefined => {
+ return typeof value === 'object' && value !== null
+ ? (value as Record)
+ : undefined;
+ };
+
+ const getTrimmedString = (value: unknown): string | undefined => {
+ return typeof value === 'string' && value.trim() ? value.trim() : undefined;
+ };
+
+ const root = asRecord(data);
+ if (!root) return undefined;
+
+ // 1) root (some implementations provide `model` at the top level)
+ const rootModel = getTrimmedString(root.model);
+ if (rootModel) {
+ return rootModel;
+ }
+
+ // 2) streaming choice (delta) or final response (message)
+ const firstChoice = Array.isArray(root.choices) ? asRecord(root.choices[0]) : undefined;
+ if (!firstChoice) {
+ return undefined;
+ }
+
+ // priority: delta.model (first chunk) else message.model (final response)
+ const deltaModel = getTrimmedString(asRecord(firstChoice.delta)?.model);
+ if (deltaModel) {
+ return deltaModel;
+ }
+
+ const messageModel = getTrimmedString(asRecord(firstChoice.message)?.model);
+ if (messageModel) {
+ return messageModel;
+ }
+
+ // avoid guessing from non-standard locations (metadata, etc.)
+ return undefined;
+ }
+
+ /**
+ * Calls the onTimings callback with timing data from streaming response.
+ *
+ * @param timings - Timing information from the Chat Completions API response
+ * @param promptProgress - Prompt processing progress data
+ * @param onTimingsCallback - Callback function to invoke with timing data
+ * @private
+ */
+ private static notifyTimings(
+ timings: ChatMessageTimings | undefined,
+ promptProgress: ChatMessagePromptProgress | undefined,
+ onTimingsCallback:
+ | ((timings?: ChatMessageTimings, promptProgress?: ChatMessagePromptProgress) => void)
+ | undefined
+ ): void {
+ if (!onTimingsCallback || (!timings && !promptProgress)) return;
+
+ onTimingsCallback(timings, promptProgress);
+ }
+}
diff --git a/llama.cpp/tools/ui/src/lib/services/database.service.ts b/llama.cpp/tools/ui/src/lib/services/database.service.ts
new file mode 100644
index 0000000000000000000000000000000000000000..3338439bbec1fd0a2b51259b019b95ae14991fee
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/database.service.ts
@@ -0,0 +1,531 @@
+import Dexie, { type EntityTable } from 'dexie';
+import { findDescendantMessages, uuid, filterByLeafNodeId } from '$lib/utils';
+import { IDXDB_TABLES, IDXDB_STORES, STORAGE_APP_NAME } from '$lib/constants';
+import { MessageRole } from '$lib/enums';
+import type { McpServerOverride } from '$lib/types/database';
+
+class LlamaUiDatabase extends Dexie {
+ [IDXDB_TABLES.conversations]!: EntityTable;
+ [IDXDB_TABLES.messages]!: EntityTable;
+
+ constructor() {
+ super(STORAGE_APP_NAME);
+
+ this.version(1).stores(IDXDB_STORES);
+ }
+}
+
+const db = new LlamaUiDatabase();
+
+export class DatabaseService {
+ /**
+ *
+ *
+ * Conversations
+ *
+ *
+ */
+
+ /**
+ * Creates a new conversation.
+ *
+ * @param name - Name of the conversation
+ * @returns The created conversation
+ */
+ static async createConversation(name: string): Promise {
+ const conversation: DatabaseConversation = {
+ id: uuid(),
+ name,
+ lastModified: Date.now(),
+ currNode: ''
+ };
+
+ await db[IDXDB_TABLES.conversations].add(conversation);
+ return conversation;
+ }
+
+ /**
+ *
+ *
+ * Messages
+ *
+ *
+ */
+
+ /**
+ * Creates a new message branch by adding a message and updating parent/child relationships.
+ * Also updates the conversation's currNode to point to the new message.
+ *
+ * @param message - Message to add (without id)
+ * @param parentId - Parent message ID to attach to
+ * @returns The created message
+ */
+ static async createMessageBranch(
+ message: Omit,
+ parentId: string | null
+ ): Promise {
+ return await db.transaction(
+ 'rw',
+ [db[IDXDB_TABLES.conversations], db[IDXDB_TABLES.messages]],
+ async () => {
+ // Handle null parent (root message case)
+ if (parentId !== null) {
+ const parentMessage = await db[IDXDB_TABLES.messages].get(parentId);
+ if (!parentMessage) {
+ throw new Error(`Parent message ${parentId} not found`);
+ }
+ }
+
+ const newMessage: DatabaseMessage = {
+ ...message,
+ id: uuid(),
+ parent: parentId,
+ toolCalls: message.toolCalls ?? '',
+ children: []
+ };
+
+ await db[IDXDB_TABLES.messages].add(newMessage);
+
+ // Update parent's children array if parent exists
+ if (parentId !== null) {
+ const parentMessage = await db[IDXDB_TABLES.messages].get(parentId);
+ if (parentMessage) {
+ await db[IDXDB_TABLES.messages].update(parentId, {
+ children: [...parentMessage.children, newMessage.id]
+ });
+ }
+ }
+
+ await this.updateConversation(message.convId, {
+ currNode: newMessage.id
+ });
+
+ return newMessage;
+ }
+ );
+ }
+
+ /**
+ * Creates a root message for a new conversation.
+ * Root messages are not displayed but serve as the tree root for branching.
+ *
+ * @param convId - Conversation ID
+ * @returns The created root message
+ */
+ static async createRootMessage(convId: string): Promise {
+ const rootMessage: DatabaseMessage = {
+ id: uuid(),
+ convId,
+ type: 'root',
+ timestamp: Date.now(),
+ role: MessageRole.SYSTEM,
+ content: '',
+ parent: null,
+ toolCalls: '',
+ children: []
+ };
+
+ await db[IDXDB_TABLES.messages].add(rootMessage);
+ return rootMessage.id;
+ }
+
+ /**
+ * Creates a system prompt message for a conversation.
+ *
+ * @param convId - Conversation ID
+ * @param systemPrompt - The system prompt content (must be non-empty)
+ * @param parentId - Parent message ID (typically the root message)
+ * @returns The created system message
+ * @throws Error if systemPrompt is empty
+ */
+ static async createSystemMessage(
+ convId: string,
+ systemPrompt: string,
+ parentId: string
+ ): Promise {
+ const trimmedPrompt = systemPrompt.trim();
+ if (!trimmedPrompt) {
+ throw new Error('Cannot create system message with empty content');
+ }
+
+ const systemMessage: DatabaseMessage = {
+ id: uuid(),
+ convId,
+ type: MessageRole.SYSTEM,
+ timestamp: Date.now(),
+ role: MessageRole.SYSTEM,
+ content: trimmedPrompt,
+ parent: parentId,
+ children: []
+ };
+
+ await db[IDXDB_TABLES.messages].add(systemMessage);
+
+ const parentMessage = await db[IDXDB_TABLES.messages].get(parentId);
+ if (parentMessage) {
+ await db[IDXDB_TABLES.messages].update(parentId, {
+ children: [...parentMessage.children, systemMessage.id]
+ });
+ }
+
+ return systemMessage;
+ }
+
+ /**
+ * Deletes a conversation and all its messages.
+ *
+ * @param id - Conversation ID
+ */
+ static async deleteConversation(
+ id: string,
+ options?: { deleteWithForks?: boolean }
+ ): Promise {
+ await db.transaction(
+ 'rw',
+ [db[IDXDB_TABLES.conversations], db[IDXDB_TABLES.messages]],
+ async () => {
+ if (options?.deleteWithForks) {
+ // Recursively collect all descendant IDs
+ const idsToDelete: string[] = [];
+ const queue = [id];
+
+ while (queue.length > 0) {
+ const parentId = queue.pop()!;
+ const children = await db[IDXDB_TABLES.conversations]
+ .filter((c) => c.forkedFromConversationId === parentId)
+ .toArray();
+
+ for (const child of children) {
+ idsToDelete.push(child.id);
+ queue.push(child.id);
+ }
+ }
+
+ for (const forkId of idsToDelete) {
+ await db[IDXDB_TABLES.conversations].delete(forkId);
+ await db[IDXDB_TABLES.messages].where('convId').equals(forkId).delete();
+ }
+ } else {
+ // Reparent direct children to deleted conv's parent
+ const conv = await db[IDXDB_TABLES.conversations].get(id);
+ const newParent = conv?.forkedFromConversationId;
+ const directChildren = await db[IDXDB_TABLES.conversations]
+ .filter((c) => c.forkedFromConversationId === id)
+ .toArray();
+
+ for (const child of directChildren) {
+ await db[IDXDB_TABLES.conversations].update(child.id, {
+ forkedFromConversationId: newParent ?? undefined
+ });
+ }
+ }
+
+ await db[IDXDB_TABLES.conversations].delete(id);
+ await db[IDXDB_TABLES.messages].where('convId').equals(id).delete();
+ }
+ );
+ }
+
+ /**
+ * Deletes a message and removes it from its parent's children array.
+ *
+ * @param messageId - ID of the message to delete
+ */
+ static async deleteMessage(messageId: string): Promise {
+ await db.transaction('rw', db[IDXDB_TABLES.messages], async () => {
+ const message = await db[IDXDB_TABLES.messages].get(messageId);
+ if (!message) return;
+
+ // Remove this message from its parent's children array
+ if (message.parent) {
+ const parent = await db[IDXDB_TABLES.messages].get(message.parent);
+ if (parent) {
+ parent.children = parent.children.filter((childId: string) => childId !== messageId);
+ await db[IDXDB_TABLES.messages].put(parent);
+ }
+ }
+
+ // Delete the message
+ await db[IDXDB_TABLES.messages].delete(messageId);
+ });
+ }
+
+ /**
+ * Deletes a message and all its descendant messages (cascading deletion).
+ * This removes the entire branch starting from the specified message.
+ *
+ * @param conversationId - ID of the conversation containing the message
+ * @param messageId - ID of the root message to delete (along with all descendants)
+ * @returns Array of all deleted message IDs
+ */
+ static async deleteMessageCascading(
+ conversationId: string,
+ messageId: string
+ ): Promise {
+ return await db.transaction('rw', db[IDXDB_TABLES.messages], async () => {
+ // Get all messages in the conversation to find descendants
+ const allMessages = await db[IDXDB_TABLES.messages]
+ .where('convId')
+ .equals(conversationId)
+ .toArray();
+
+ // Find all descendant messages
+ const descendants = findDescendantMessages(allMessages, messageId);
+ const allToDelete = [messageId, ...descendants];
+
+ // Get the message to delete for parent cleanup
+ const message = await db[IDXDB_TABLES.messages].get(messageId);
+ if (message && message.parent) {
+ const parent = await db[IDXDB_TABLES.messages].get(message.parent);
+ if (parent) {
+ parent.children = parent.children.filter((childId: string) => childId !== messageId);
+ await db[IDXDB_TABLES.messages].put(parent);
+ }
+ }
+
+ // Delete all messages in the branch
+ await db[IDXDB_TABLES.messages].bulkDelete(allToDelete);
+
+ return allToDelete;
+ });
+ }
+
+ /**
+ * Gets all conversations, sorted by last modified time (newest first).
+ *
+ * @returns Array of conversations
+ */
+ static async getAllConversations(): Promise {
+ return await db[IDXDB_TABLES.conversations].orderBy('lastModified').reverse().toArray();
+ }
+
+ /**
+ * Gets a conversation by ID.
+ *
+ * @param id - Conversation ID
+ * @returns The conversation if found, otherwise undefined
+ */
+ static async getConversation(id: string): Promise {
+ return await db[IDXDB_TABLES.conversations].get(id);
+ }
+
+ /**
+ * Gets all messages in a conversation, sorted by timestamp (oldest first).
+ *
+ * @param convId - Conversation ID
+ * @returns Array of messages in the conversation
+ */
+ static async getConversationMessages(convId: string): Promise {
+ return await db[IDXDB_TABLES.messages].where('convId').equals(convId).sortBy('timestamp');
+ }
+
+ /**
+ * Updates a conversation.
+ *
+ * @param id - Conversation ID
+ * @param updates - Partial updates to apply
+ * @returns Promise that resolves when the conversation is updated
+ */
+ static async updateConversation(
+ id: string,
+ updates: Partial>
+ ): Promise {
+ await db[IDXDB_TABLES.conversations].update(id, {
+ ...updates,
+ lastModified: Date.now()
+ });
+ }
+
+ /**
+ *
+ *
+ * Navigation
+ *
+ *
+ */
+
+ /**
+ * Toggles the pinned status of a conversation.
+ *
+ * @param id - Conversation ID
+ * @returns The new pinned status
+ */
+ static async toggleConversationPin(id: string): Promise {
+ const conversation = await db.conversations.get(id);
+ if (!conversation) {
+ throw new Error(`Conversation ${id} not found`);
+ }
+ const newPinnedState = !conversation.pinned;
+ await this.updateConversation(id, { pinned: newPinnedState });
+ return newPinnedState;
+ }
+
+ /**
+ * Updates the conversation's current node (active branch).
+ * This determines which conversation path is currently being viewed.
+ *
+ * @param convId - Conversation ID
+ * @param nodeId - Message ID to set as current node
+ */
+ static async updateCurrentNode(convId: string, nodeId: string): Promise {
+ await this.updateConversation(convId, {
+ currNode: nodeId
+ });
+ }
+
+ /**
+ * Updates a message.
+ *
+ * @param id - Message ID
+ * @param updates - Partial updates to apply
+ * @returns Promise that resolves when the message is updated
+ */
+ static async updateMessage(
+ id: string,
+ updates: Partial>
+ ): Promise {
+ await db[IDXDB_TABLES.messages].update(id, updates);
+ }
+
+ /**
+ *
+ *
+ * Import
+ *
+ *
+ */
+
+ /**
+ * Imports multiple conversations and their messages.
+ * Skips conversations that already exist.
+ *
+ * @param data - Array of { conv, messages } objects
+ */
+ static async importConversations(
+ data: { conv: DatabaseConversation; messages: DatabaseMessage[] }[]
+ ): Promise<{ imported: number; skipped: number }> {
+ let importedCount = 0;
+ let skippedCount = 0;
+
+ return await db.transaction(
+ 'rw',
+ [db[IDXDB_TABLES.conversations], db[IDXDB_TABLES.messages]],
+ async () => {
+ for (const item of data) {
+ const { conv, messages } = item;
+
+ const existing = await db[IDXDB_TABLES.conversations].get(conv.id);
+ if (existing) {
+ console.warn(`Conversation "${conv.name}" already exists, skipping...`);
+ skippedCount++;
+ continue;
+ }
+
+ await db[IDXDB_TABLES.conversations].add(conv);
+ for (const msg of messages) {
+ await db[IDXDB_TABLES.messages].put(msg);
+ }
+
+ importedCount++;
+ }
+
+ return { imported: importedCount, skipped: skippedCount };
+ }
+ );
+ }
+
+ /**
+ *
+ *
+ * Forking
+ *
+ *
+ */
+
+ /**
+ * Forks a conversation at a specific message, creating a new conversation
+ * containing all messages from the root up to (and including) the target message.
+ *
+ * @param sourceConvId - The source conversation ID
+ * @param atMessageId - The message ID to fork at (the new conversation ends here)
+ * @param options - Fork options (name and whether to include attachments)
+ * @returns The newly created conversation
+ */
+ static async forkConversation(
+ sourceConvId: string,
+ atMessageId: string,
+ options: { name: string; includeAttachments: boolean }
+ ): Promise {
+ return await db.transaction(
+ 'rw',
+ [db[IDXDB_TABLES.conversations], db[IDXDB_TABLES.messages]],
+ async () => {
+ const sourceConv = await db[IDXDB_TABLES.conversations].get(sourceConvId);
+ if (!sourceConv) {
+ throw new Error(`Source conversation ${sourceConvId} not found`);
+ }
+
+ const allMessages = await db[IDXDB_TABLES.messages]
+ .where('convId')
+ .equals(sourceConvId)
+ .toArray();
+
+ const pathMessages = filterByLeafNodeId(
+ allMessages,
+ atMessageId,
+ true
+ ) as DatabaseMessage[];
+ if (pathMessages.length === 0) {
+ throw new Error(`Could not resolve message path to ${atMessageId}`);
+ }
+
+ const idMap = new Map();
+
+ for (const msg of pathMessages) {
+ idMap.set(msg.id, uuid());
+ }
+
+ const newConvId = uuid();
+ const clonedMessages: DatabaseMessage[] = pathMessages.map((msg) => {
+ const newId = idMap.get(msg.id)!;
+ const newParent = msg.parent ? (idMap.get(msg.parent) ?? null) : null;
+ const newChildren = msg.children
+ .filter((childId: string) => idMap.has(childId))
+ .map((childId: string) => idMap.get(childId)!);
+
+ return {
+ ...msg,
+ id: newId,
+ convId: newConvId,
+ parent: newParent,
+ children: newChildren,
+ extra: options.includeAttachments ? msg.extra : undefined
+ };
+ });
+
+ const lastClonedMessage = clonedMessages[clonedMessages.length - 1];
+ const newConv: DatabaseConversation = {
+ id: newConvId,
+ name: options.name,
+ lastModified: Date.now(),
+ currNode: lastClonedMessage.id,
+ forkedFromConversationId: sourceConvId,
+ mcpServerOverrides: sourceConv.mcpServerOverrides
+ ? sourceConv.mcpServerOverrides.map((o: McpServerOverride) => ({
+ serverId: o.serverId,
+ enabled: o.enabled
+ }))
+ : undefined
+ };
+
+ await db[IDXDB_TABLES.conversations].add(newConv);
+
+ for (const msg of clonedMessages) {
+ await db[IDXDB_TABLES.messages].add(msg);
+ }
+
+ return newConv;
+ }
+ );
+ }
+}
diff --git a/llama.cpp/tools/ui/src/lib/services/index.ts b/llama.cpp/tools/ui/src/lib/services/index.ts
new file mode 100644
index 0000000000000000000000000000000000000000..386f740b8f04a4f1d5bcb11a9edae7ebf0f08e85
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/index.ts
@@ -0,0 +1,333 @@
+/**
+ *
+ * SERVICES
+ *
+ * Stateless service layer for API communication and data operations.
+ * Services handle protocol-level concerns (HTTP, WebSocket, MCP, IndexedDB)
+ * without managing reactive state — that responsibility belongs to stores.
+ *
+ * **Design Principles:**
+ * - All methods are static — no instance state
+ * - Pure I/O operations (network requests, database queries)
+ * - No Svelte runes or reactive primitives
+ * - Error handling at the protocol level; business-level error handling in stores
+ *
+ * **Architecture (bottom to top):**
+ * - **Services** (this layer): Stateless protocol communication
+ * - **Stores**: Reactive state management consuming services
+ * - **Components**: UI consuming stores
+ *
+ */
+
+/**
+ * **ChatService** - Chat Completions API communication layer
+ *
+ * Handles direct communication with the llama-server's `/v1/chat/completions` endpoint.
+ * Provides streaming and non-streaming response parsing, message format conversion
+ * (DatabaseMessage → API format), and request lifecycle management.
+ *
+ * **Terminology - Chat vs Conversation:**
+ * - **Chat**: The active interaction space with the Chat Completions API. Ephemeral and
+ * runtime-focused — sending messages, receiving streaming responses, managing request lifecycles.
+ * - **Conversation**: The persistent database entity storing all messages and metadata.
+ * Managed by conversationsStore, conversations persist across sessions.
+ *
+ * **Architecture & Relationships:**
+ * - **ChatService** (this class): Stateless API communication layer
+ * - Handles HTTP requests/responses with the llama-server
+ * - Manages streaming and non-streaming response parsing
+ * - Converts database messages to API format (multimodal, tool calls)
+ * - Handles error translation with user-friendly messages
+ *
+ * - **chatStore**: Primary consumer — uses ChatService for all AI model communication
+ * - **agenticStore**: Uses ChatService for multi-turn agentic loop streaming
+ * - **conversationsStore**: Provides message context for API requests
+ *
+ * **Key Responsibilities:**
+ * - Streaming response handling with real-time content/reasoning/tool-call callbacks
+ * - Non-streaming response parsing with complete response extraction
+ * - Database message to API format conversion (attachments, tool calls, multimodal)
+ * - Tool call delta merging for incremental streaming aggregation
+ * - Request parameter assembly (sampling, penalties, custom params)
+ * - File attachment processing (images, PDFs, audio, text, MCP prompts/resources)
+ * - Reasoning content stripping from prompt history to avoid KV cache pollution
+ * - Error translation (network, timeout, server errors → user-friendly messages)
+ *
+ * @see chatStore in stores/chat.svelte.ts — primary consumer for chat state management
+ * @see agenticStore in stores/agentic.svelte.ts — uses ChatService for agentic loop streaming
+ * @see conversationsStore in stores/conversations.svelte.ts — provides message context
+ */
+export { ChatService } from './chat.service';
+
+/**
+ * **DatabaseService** - IndexedDB persistence layer via Dexie ORM
+ *
+ * Provides stateless data access for conversations and messages using IndexedDB.
+ * Handles all low-level storage operations including branching tree structures,
+ * cascade deletions, and transaction safety for multi-table operations.
+ *
+ * **Architecture & Relationships (bottom to top):**
+ * - **DatabaseService** (this class): Stateless IndexedDB operations
+ * - Lowest layer — direct Dexie/IndexedDB communication
+ * - Pure CRUD operations without business logic
+ * - Handles branching tree structure (parent-child relationships)
+ * - Provides transaction safety for multi-table operations
+ *
+ * - **conversationsStore**: Reactive state management layer
+ * - Uses DatabaseService for all persistence operations
+ * - Manages conversation list, active conversation, and messages in memory
+ *
+ * - **chatStore**: Active AI interaction management
+ * - Uses conversationsStore for conversation context
+ * - Directly uses DatabaseService for message CRUD during streaming
+ *
+ * **Key Responsibilities:**
+ * - Conversation CRUD (create, read, update, delete)
+ * - Message CRUD with branching support (parent-child relationships)
+ * - Root message and system prompt creation
+ * - Cascade deletion of message branches (descendants)
+ * - Transaction-safe multi-table operations
+ * - Conversation import with duplicate detection
+ *
+ * **Database Schema:**
+ * - `conversations`: id, lastModified, currNode, name
+ * - `messages`: id, convId, type, role, timestamp, parent, children
+ *
+ * **Branching Model:**
+ * Messages form a tree structure where each message can have multiple children,
+ * enabling conversation branching and alternative response paths. The conversation's
+ * `currNode` tracks the currently active branch endpoint.
+ *
+ * @see conversationsStore in stores/conversations.svelte.ts — reactive layer on top of DatabaseService
+ * @see chatStore in stores/chat.svelte.ts — uses DatabaseService directly for message CRUD during streaming
+ */
+export { DatabaseService } from './database.service';
+
+/**
+ * **ModelsService** - Model management API communication
+ *
+ * Handles communication with model-related endpoints for both MODEL (single model)
+ * and ROUTER (multi-model) server modes. Provides model listing, loading/unloading,
+ * and status checking without managing any model state.
+ *
+ * **Architecture & Relationships:**
+ * - **ModelsService** (this class): Stateless HTTP communication
+ * - Sends requests to model endpoints
+ * - Parses and returns typed API responses
+ * - Provides model status utility methods
+ *
+ * - **modelsStore**: Primary consumer — manages reactive model state
+ * - Calls ModelsService for all model API operations
+ * - Handles polling, caching, and state updates
+ *
+ * **Key Responsibilities:**
+ * - List available models via OpenAI-compatible `/v1/models` endpoint
+ * - Load/unload models via `/models/load` and `/models/unload` (ROUTER mode)
+ * - Model status queries (loaded, loading)
+ *
+ * **Server Mode Behavior:**
+ * - **MODEL mode**: Only `list()` is relevant — single model always loaded
+ * - **ROUTER mode**: Full lifecycle — `list()`, `listRouter()`, `load()`, `unload()`
+ *
+ * **Endpoints:**
+ * - `GET /v1/models` — OpenAI-compatible model list (both modes)
+ * - `POST /models/load` — Load a model (ROUTER mode only)
+ * - `POST /models/unload` — Unload a model (ROUTER mode only)
+ *
+ * @see modelsStore in stores/models.svelte.ts — primary consumer for reactive model state
+ */
+export { ModelsService } from './models.service';
+
+/**
+ * **PropsService** - Server properties and capabilities retrieval
+ *
+ * Fetches server configuration, model information, and capabilities from the `/props`
+ * endpoint. Supports both global server props and per-model props (ROUTER mode).
+ *
+ * **Architecture & Relationships:**
+ * - **PropsService** (this class): Stateless HTTP communication
+ * - Fetches server properties from `/props` endpoint
+ * - Handles authentication and request parameters
+ * - Returns typed `ApiLlamaCppServerProps` responses
+ *
+ * - **serverStore**: Consumes global server properties (role detection, connection state)
+ * - **modelsStore**: Consumes per-model properties (modalities, context size)
+ * - **settingsStore**: Syncs default generation parameters from props response
+ *
+ * **Key Responsibilities:**
+ * - Fetch global server properties (default generation settings, modalities)
+ * - Fetch per-model properties in ROUTER mode via `?model=` parameter
+ * - Handle autoload control to prevent unintended model loading
+ *
+ * **API Behavior:**
+ * - `GET /props` → Global server props (MODEL mode: includes modalities)
+ * - `GET /props?model=` → Per-model props (ROUTER mode: model-specific modalities)
+ * - `&autoload=false` → Prevents model auto-loading when querying props
+ *
+ * @see serverStore in stores/server.svelte.ts — consumes global server props
+ * @see modelsStore in stores/models.svelte.ts — consumes per-model props for modalities
+ * @see settingsStore in stores/settings.svelte.ts — syncs default generation params from props
+ */
+export { PropsService } from './props.service';
+
+/**
+ * **ParameterSyncService** - Server defaults and user settings synchronization
+ *
+ * Manages the complex logic of merging server-provided default parameters with
+ * user-configured overrides. Ensures the UI reflects the actual server state
+ * while preserving user customizations. Tracks parameter sources (server default
+ * vs user override) for display in the settings UI.
+ *
+ * **Architecture & Relationships:**
+ * - **ParameterSyncService** (this class): Stateless sync logic
+ * - Pure functions for parameter extraction, merging, and diffing
+ * - No side effects — receives data in, returns data out
+ * - Handles floating-point precision normalization
+ *
+ * - **settingsStore**: Primary consumer — calls sync methods during:
+ * - Initial load (`syncWithServerDefaults`)
+ * - Settings reset (`forceSyncWithServerDefaults`)
+ * - Parameter info queries (`getParameterInfo`)
+ *
+ * - **PropsService**: Provides raw server props that feed into extraction
+ *
+ * **Key Responsibilities:**
+ * - Extract syncable parameters from server `/props` response
+ * - Merge server defaults with user overrides (user wins)
+ * - Track parameter source (Custom vs Default) for UI badges
+ * - Validate server parameter values by type (number, string, boolean)
+ * - Create diffs between current settings and server defaults
+ * - Floating-point precision normalization for consistent comparisons
+ *
+ * **Parameter Source Priority:**
+ * 1. **User Override** (Custom badge) — explicitly set by user in settings
+ * 2. **Server Default** (Default badge) — from `/props` endpoint
+ * 3. **App Default** — hardcoded fallback when server props unavailable
+ *
+ * **Exports:**
+ * - `ParameterSyncService` class — static methods for sync logic
+ * - `SYNCABLE_PARAMETERS` — mapping of UI setting keys to server parameter keys
+ *
+ * @see settingsStore in stores/settings.svelte.ts — primary consumer for settings sync
+ * @see SettingsChatParameterSourceIndicator — displays parameter source badges in UI
+ */
+export { ParameterSyncService } from './parameter-sync.service';
+
+/**
+ * **MCPService** - Low-level MCP protocol communication layer
+ *
+ * Implements the client-side MCP (Model Context Protocol) SDK operations for connecting
+ * to MCP servers, discovering capabilities, and executing protocol operations.
+ * Supports multiple transport types: WebSocket, StreamableHTTP, and SSE (legacy fallback).
+ *
+ * **Architecture & Relationships:**
+ * - **MCPService** (this class): Stateless protocol communication
+ * - Creates and manages transport connections (WebSocket, StreamableHTTP, SSE)
+ * - Wraps MCP SDK client operations with error handling
+ * - Formats tool results and extracts server info
+ * - Provides abort signal support for cancellable operations
+ *
+ * - **mcpStore**: Reactive business logic facade
+ * - Uses MCPService for all protocol-level operations
+ * - Manages connection lifecycle, health checks, reconnection
+ * - Handles tool name conflict resolution and server coordination
+ *
+ * - **mcpResourceStore**: Reactive resource state
+ * - Receives resource data fetched via MCPService
+ * - Manages resource caching, subscriptions, and attachments
+ *
+ * - **agenticStore**: Agentic loop orchestration
+ * - Executes tool calls via mcpStore → MCPService chain
+ *
+ * **Key Responsibilities:**
+ * - Transport creation with automatic fallback (StreamableHTTP → SSE)
+ * - Server connection with detailed phase tracking and progress callbacks
+ * - Tool discovery (`listTools`) and execution (`callTool`) with abort support
+ * - Prompt listing (`listPrompts`) and retrieval (`getPrompt`) with arguments
+ * - Resource operations: list, read, subscribe/unsubscribe, template support
+ * - Completion suggestions for prompt arguments and resource URI templates
+ * - CORS proxy routing via llama-server for cross-origin MCP servers
+ * - Tool result formatting (text, images, embedded resources)
+ *
+ * **Transport Hierarchy:**
+ * 1. **WebSocket** — bidirectional, no CORS proxy support
+ * 2. **StreamableHTTP** — modern HTTP-based, supports CORS proxy
+ * 3. **SSE** — legacy fallback, supports CORS proxy
+ *
+ * @see mcpStore in stores/mcp.svelte.ts — reactive business logic facade on top of MCPService
+ * @see mcpResourceStore in stores/mcp-resources.svelte.ts — reactive resource state management
+ * @see agenticStore in stores/agentic.svelte.ts — uses MCPService (via mcpStore) for tool execution
+ * @see MCP Protocol Specification: https://modelcontextprotocol.io/specification/2025-06-18
+ */
+export { MCPService } from './mcp.service';
+
+/**
+ * **SandboxService** - Frontend JavaScript execution in a browser sandbox
+ *
+ * Stateless executor for the run_javascript frontend tool. Model generated
+ * code runs in a Web Worker spawned inside a sandboxed iframe with an opaque
+ * origin: no access to the app origin, its storage or its API, and outgoing
+ * requests carry a null origin. The code never touches a main thread, so the
+ * parent enforces the timeout by removing the iframe, which terminates the
+ * worker at the browser level.
+ *
+ * **Architecture & Relationships:**
+ * - **SandboxService** (this class): Stateless sandbox execution
+ * - **toolsStore**: Exposes the tool definition when the sandbox is enabled
+ * - **agenticStore**: Dispatches ToolSource.FRONTEND calls here
+ *
+ * @see SANDBOX_TOOL_DEFINITION in constants/sandbox.ts - tool schema sent to the LLM
+ * @see agenticStore in stores/agentic.svelte.ts - tool dispatch
+ */
+export { SandboxService } from './sandbox.service';
+
+/**
+ * **RouterService** — Dynamic route URL construction utility
+ *
+ * Stateless utility for building dynamic route URLs from ROUTES base paths.
+ * Static routes (START, NEW_CHAT, MCP_SERVERS) live in ROUTES constants;
+ * dynamic routes (CHAT, SETTINGS) are constructed here by appending parameters.
+ *
+ * **Architecture & Relationships:**
+ * - **RouterService** (this class): Stateless URL construction
+ * - Builds dynamic route URLs from ROUTES base paths
+ * - No side effects — receives route parameters, returns route strings
+ *
+ * - **ROUTES constant** (constants/routes.ts): Static route base paths
+ * - **All components/stores**: Call RouterService for dynamic route URLs
+ *
+ * **Key Responsibilities:**
+ * - Build chat URLs for specific conversations: `RouterService.chat(id)` → `#/chat/:id`
+ * - Build settings URLs for sections: `RouterService.settings(section)` → `#/settings/:section`
+ *
+ * @see ROUTES in constants/routes.ts — static route base paths
+ */
+export { RouterService } from './router.service';
+
+/**
+ * **MigrationService** — Unified data migration hook
+ *
+ * Centralizes all data migrations (localStorage, IndexedDB, legacy formats) into a single
+ * initialization point. All migrations are NON-DESTRUCTIVE - legacy data is preserved
+ * for downgrade compatibility (no rollback needed).
+ *
+ * **Current Migrations:**
+ * 1. **localStorage prefix**: Copy LlamaCppWebui.* → LlamaUi.* (both preserved)
+ * 2. **IndexedDB database**: Copy LlamacppWebui → LlamaUi (both preserved)
+ * 3. **Legacy message format**: Marker-based → Structured format
+ * 4. **Theme key**: Copy standalone `theme` → config object (both preserved)
+ *
+ * **Usage:**
+ * ```typescript
+ * import { MigrationService } from '$lib/services';
+ *
+ * // Run all migrations on app startup (non-destructive)
+ * await MigrationService.runAllMigrations();
+ *
+ * // Check migration status
+ * const state = MigrationService.getState();
+ * ```
+ *
+ * @see migration.service.ts — full implementation (non-destructive)
+ */
+export { MigrationService } from './migration.service';
diff --git a/llama.cpp/tools/ui/src/lib/services/mcp.service.ts b/llama.cpp/tools/ui/src/lib/services/mcp.service.ts
new file mode 100644
index 0000000000000000000000000000000000000000..4432989171d1898d26b9eff09af54d5d18c8d29e
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/mcp.service.ts
@@ -0,0 +1,1218 @@
+import { Client } from '@modelcontextprotocol/sdk/client';
+import {
+ StreamableHTTPClientTransport,
+ StreamableHTTPError
+} from '@modelcontextprotocol/sdk/client/streamableHttp.js';
+import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
+import { WebSocketClientTransport } from '@modelcontextprotocol/sdk/client/websocket.js';
+import type {
+ Tool,
+ Prompt,
+ GetPromptResult,
+ ListChangedHandlers
+} from '@modelcontextprotocol/sdk/types.js';
+import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
+import {
+ DEFAULT_MCP_CONFIG,
+ DEFAULT_CLIENT_VERSION,
+ DEFAULT_IMAGE_MIME_TYPE,
+ CORS_PROXY_HEADER_PREFIX,
+ MCP_PARTIAL_REDACT_HEADERS,
+ CORS_PROXY_ENDPOINT
+} from '$lib/constants';
+import {
+ MCPConnectionPhase,
+ MCPLogLevel,
+ MCPTransportType,
+ MCPContentType,
+ MCPRefType
+} from '$lib/enums';
+import type {
+ MCPServerConfig,
+ MCPResourceIcon,
+ ToolCallParams,
+ ToolExecutionResult,
+ Implementation,
+ ClientCapabilities,
+ MCPConnection,
+ MCPPhaseCallback,
+ MCPConnectionLog,
+ MCPServerInfo,
+ MCPResource,
+ MCPResourceTemplate,
+ MCPResourceContent,
+ MCPReadResourceResult
+} from '$lib/types';
+import {
+ buildProxiedUrl,
+ buildProxiedHeaders,
+ getAuthHeaders,
+ sanitizeHeaders,
+ throwIfAborted,
+ isAbortError,
+ createBase64DataUrl,
+ getRequestUrl,
+ getRequestMethod,
+ getRequestBody,
+ summarizeRequestBody,
+ formatDiagnosticErrorMessage,
+ extractJsonRpcMethods,
+ type RequestBodySummary
+} from '$lib/utils';
+
+interface ToolResultContentItem {
+ type: string;
+ text?: string;
+ data?: string;
+ mimeType?: string;
+ resource?: { text?: string; blob?: string; uri?: string };
+}
+
+interface ToolCallResult {
+ content?: ToolResultContentItem[];
+ isError?: boolean;
+ _meta?: Record;
+}
+
+interface DiagnosticRequestDetails {
+ url: string;
+ method: string;
+ credentials?: RequestCredentials;
+ mode?: RequestMode;
+ headers: Record;
+ body: RequestBodySummary;
+ jsonRpcMethods?: string[];
+}
+
+export class MCPService {
+ /**
+ * Create a connection log entry for phase tracking.
+ *
+ * @param phase - The connection phase this log belongs to
+ * @param message - Human-readable log message
+ * @param level - Log severity level (default: INFO)
+ * @param details - Optional structured details for debugging
+ * @returns Formatted connection log entry
+ */
+ private static createLog(
+ phase: MCPConnectionPhase,
+ message: string,
+ level: MCPLogLevel = MCPLogLevel.INFO,
+ details?: unknown
+ ): MCPConnectionLog {
+ return {
+ timestamp: new Date(),
+ phase,
+ message,
+ level,
+ details
+ };
+ }
+
+ private static createDiagnosticRequestDetails(
+ input: RequestInfo | URL,
+ init: RequestInit | undefined,
+ baseInit: RequestInit,
+ requestHeaders: Headers,
+ extraRedactedHeaders?: Iterable
+ ): DiagnosticRequestDetails {
+ const body = getRequestBody(input, init);
+ const details: DiagnosticRequestDetails = {
+ url: getRequestUrl(input),
+ method: getRequestMethod(input, init, baseInit).toUpperCase(),
+ credentials: init?.credentials ?? baseInit.credentials,
+ mode: init?.mode ?? baseInit.mode,
+ headers: sanitizeHeaders(requestHeaders, extraRedactedHeaders, MCP_PARTIAL_REDACT_HEADERS),
+ body: summarizeRequestBody(body)
+ };
+ const jsonRpcMethods = extractJsonRpcMethods(body);
+
+ if (jsonRpcMethods) {
+ details.jsonRpcMethods = jsonRpcMethods;
+ }
+
+ return details;
+ }
+
+ private static addRequestHeaders(
+ requestHeaders: Headers,
+ headers: HeadersInit,
+ useProxy: boolean
+ ) {
+ for (const [key, value] of new Headers(headers).entries()) {
+ const proxiedKey =
+ useProxy && !key.toLowerCase().startsWith(CORS_PROXY_HEADER_PREFIX)
+ ? `${CORS_PROXY_HEADER_PREFIX}${key}`
+ : key;
+ requestHeaders.set(proxiedKey, value);
+ }
+ }
+
+ private static summarizeError(error: unknown): Record {
+ if (error instanceof Error) {
+ return {
+ name: error.name,
+ message: error.message,
+ cause:
+ error.cause instanceof Error
+ ? { name: error.cause.name, message: error.cause.message }
+ : error.cause,
+ stack: error.stack?.split('\n').slice(0, 6).join('\n')
+ };
+ }
+
+ return { value: String(error) };
+ }
+
+ private static getBrowserContext(
+ targetUrl: URL,
+ useProxy: boolean
+ ): Record | undefined {
+ if (typeof window === 'undefined') {
+ return undefined;
+ }
+
+ return {
+ location: window.location.href,
+ origin: window.location.origin,
+ protocol: window.location.protocol,
+ isSecureContext: window.isSecureContext,
+ targetOrigin: targetUrl.origin,
+ targetProtocol: targetUrl.protocol,
+ sameOrigin: window.location.origin === targetUrl.origin,
+ useProxy
+ };
+ }
+
+ private static getConnectionHints(
+ targetUrl: URL,
+ config: MCPServerConfig,
+ error: unknown
+ ): string[] {
+ const hints: string[] = [];
+ const message = error instanceof Error ? error.message : String(error);
+ const headerNames = Object.keys(config.headers ?? {});
+
+ if (typeof window !== 'undefined') {
+ if (
+ window.location.protocol === 'https:' &&
+ targetUrl.protocol === 'http:' &&
+ !config.useProxy
+ ) {
+ hints.push(
+ 'The page is running over HTTPS but the MCP server is HTTP. Browsers often block this as mixed content; enable the proxy or use HTTPS/WSS for the MCP server.'
+ );
+ }
+
+ if (window.location.origin !== targetUrl.origin && !config.useProxy) {
+ hints.push(
+ 'This is a cross-origin browser request. If the server is reachable from curl or Node but not from the browser, missing CORS headers are the most likely cause.'
+ );
+ }
+ }
+
+ if (headerNames.length > 0) {
+ hints.push(
+ `Custom request headers are configured (${headerNames.join(', ')}). That triggers a CORS preflight, so the server must allow OPTIONS and include the matching Access-Control-Allow-Headers response.`
+ );
+ }
+
+ if (config.credentials && config.credentials !== 'omit') {
+ hints.push(
+ 'Credentials are enabled for this connection. Cross-origin credentialed requests need Access-Control-Allow-Credentials: true and cannot use a wildcard Access-Control-Allow-Origin.'
+ );
+ }
+
+ if (message.includes('Failed to fetch')) {
+ hints.push(
+ '"Failed to fetch" is a browser-level network failure. Common causes are CORS rejection, mixed-content blocking, certificate/TLS errors, DNS failures, or nothing listening on the target port.'
+ );
+ }
+
+ return hints;
+ }
+
+ private static createDiagnosticFetch(
+ serverName: string,
+ config: MCPServerConfig,
+ baseInit: RequestInit,
+ targetUrl: URL,
+ useProxy: boolean,
+ onLog?: (log: MCPConnectionLog) => void
+ ): {
+ fetch: typeof fetch;
+ disable: () => void;
+ } {
+ let enabled = true;
+ const logIfEnabled = (log: MCPConnectionLog) => {
+ if (enabled) {
+ onLog?.(log);
+ }
+ };
+
+ return {
+ fetch: async (input, init) => {
+ if (useProxy && typeof window !== 'undefined') {
+ let requestUrlStr = '';
+ if (typeof input === 'string') {
+ requestUrlStr = input;
+ } else if (input instanceof URL) {
+ requestUrlStr = input.href;
+ }
+
+ if (requestUrlStr) {
+ const parsedRequestUrl = new URL(requestUrlStr, window.location.origin);
+ if (
+ parsedRequestUrl.origin === window.location.origin &&
+ !parsedRequestUrl.pathname.includes(CORS_PROXY_ENDPOINT)
+ ) {
+ const originalConfigUrl = new URL(config.url);
+ const realTargetUrl = new URL(
+ parsedRequestUrl.pathname + parsedRequestUrl.search,
+ originalConfigUrl.origin
+ );
+ const proxiedUrl = buildProxiedUrl(realTargetUrl.href);
+
+ if (typeof input === 'string') {
+ input = proxiedUrl.href;
+ } else if (input instanceof URL) {
+ input = proxiedUrl;
+ }
+ }
+ }
+ }
+
+ const startedAt = performance.now();
+ const requestHeaders = new Headers(baseInit.headers);
+
+ if (typeof Request !== 'undefined' && input instanceof Request) {
+ this.addRequestHeaders(requestHeaders, input.headers, useProxy);
+ }
+
+ if (init?.headers) {
+ this.addRequestHeaders(requestHeaders, init.headers, useProxy);
+ }
+
+ const request = this.createDiagnosticRequestDetails(
+ input,
+ init,
+ baseInit,
+ requestHeaders,
+ Object.keys(config.headers ?? {})
+ );
+ const { method, url } = request;
+
+ logIfEnabled(
+ this.createLog(
+ MCPConnectionPhase.INITIALIZING,
+ `HTTP ${method} ${url}`,
+ MCPLogLevel.INFO,
+ {
+ serverName,
+ request
+ }
+ )
+ );
+
+ try {
+ const response = await fetch(input, {
+ ...baseInit,
+ ...init,
+ headers: requestHeaders
+ });
+ const durationMs = Math.round(performance.now() - startedAt);
+
+ logIfEnabled(
+ this.createLog(
+ MCPConnectionPhase.INITIALIZING,
+ `HTTP ${response.status} ${method} ${url} (${durationMs}ms)`,
+ response.ok ? MCPLogLevel.INFO : MCPLogLevel.WARN,
+ {
+ response: {
+ url,
+ status: response.status,
+ statusText: response.statusText,
+ headers: sanitizeHeaders(response.headers, undefined, MCP_PARTIAL_REDACT_HEADERS),
+ durationMs
+ }
+ }
+ )
+ );
+
+ return response;
+ } catch (error) {
+ const durationMs = Math.round(performance.now() - startedAt);
+
+ logIfEnabled(
+ this.createLog(
+ MCPConnectionPhase.ERROR,
+ `HTTP ${method} ${url} failed: ${formatDiagnosticErrorMessage(error)}`,
+ MCPLogLevel.ERROR,
+ {
+ serverName,
+ request,
+ error: this.summarizeError(error),
+ browser: this.getBrowserContext(targetUrl, useProxy),
+ hints: this.getConnectionHints(targetUrl, config, error),
+ durationMs
+ }
+ )
+ );
+
+ throw error;
+ }
+ },
+ disable: () => {
+ enabled = false;
+ }
+ };
+ }
+
+ /**
+ * Detect if an error indicates an expired/invalidated MCP session.
+ * Per MCP spec 2025-11-25: HTTP 404 means session invalidated, client MUST
+ * discard its session ID and start a new session with a fresh initialize request.
+ *
+ * @param error - The caught error to inspect
+ * @returns true if the error is a StreamableHTTP 404 (session not found)
+ */
+ static isSessionExpiredError(error: unknown): boolean {
+ return error instanceof StreamableHTTPError && error.code === 404;
+ }
+
+ /**
+ * Create transport based on server configuration.
+ * Supports WebSocket, StreamableHTTP (modern), and SSE (legacy) transports.
+ * When `useProxy` is enabled, routes HTTP requests through llama-server's CORS proxy.
+ *
+ * **Fallback Order:**
+ * 1. WebSocket — if explicitly configured (no CORS proxy support)
+ * 2. StreamableHTTP — default for HTTP connections
+ * 3. SSE — automatic fallback if StreamableHTTP fails
+ *
+ * @param config - Server configuration with url, transport type, proxy, and auth settings
+ * @returns Object containing the created transport and the transport type used
+ * @throws {Error} If url is missing, WebSocket + proxy combination, or all transports fail
+ */
+ static createTransport(
+ serverName: string,
+ config: MCPServerConfig,
+ onLog?: (log: MCPConnectionLog) => void
+ ): {
+ transport: Transport;
+ type: MCPTransportType;
+ stopPhaseLogging: () => void;
+ } {
+ if (!config.url) {
+ throw new Error('MCP server configuration is missing url');
+ }
+
+ const useProxy = config.useProxy ?? false;
+ const requestInit: RequestInit = {};
+
+ if (config.headers) {
+ requestInit.headers = config.useProxy ? buildProxiedHeaders(config.headers) : config.headers;
+ }
+
+ if (useProxy) {
+ requestInit.headers = {
+ ...getAuthHeaders(),
+ ...(requestInit.headers as Record)
+ };
+ }
+
+ if (config.credentials) {
+ requestInit.credentials = config.credentials;
+ }
+
+ if (config.transport === MCPTransportType.WEBSOCKET) {
+ if (useProxy) {
+ throw new Error(
+ 'WebSocket transport is not supported when using CORS proxy. Use HTTP transport instead.'
+ );
+ }
+
+ const url = new URL(config.url);
+
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG) {
+ console.log(`[MCPService] Creating WebSocket transport for ${url.href}`);
+ }
+
+ return {
+ transport: new WebSocketClientTransport(url),
+ type: MCPTransportType.WEBSOCKET,
+ stopPhaseLogging: () => {}
+ };
+ }
+
+ if (config.transport === MCPTransportType.SSE) {
+ const url = useProxy ? buildProxiedUrl(config.url) : new URL(config.url);
+ const { fetch: diagnosticFetch, disable: stopPhaseLogging } = this.createDiagnosticFetch(
+ serverName,
+ config,
+ requestInit,
+ url,
+ useProxy,
+ onLog
+ );
+
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG) {
+ console.log(`[MCPService] Creating SSE transport for ${url.href}`);
+ }
+
+ return {
+ transport: new SSEClientTransport(url, {
+ requestInit,
+ fetch: diagnosticFetch,
+ eventSourceInit: { fetch: diagnosticFetch }
+ }),
+ type: MCPTransportType.SSE,
+ stopPhaseLogging
+ };
+ }
+
+ const url = useProxy ? buildProxiedUrl(config.url) : new URL(config.url);
+ const { fetch: diagnosticFetch, disable: stopPhaseLogging } = this.createDiagnosticFetch(
+ serverName,
+ config,
+ requestInit,
+ url,
+ useProxy,
+ onLog
+ );
+
+ if (useProxy && import.meta.env.DEV && import.meta.env.VITE_DEBUG) {
+ console.log(`[MCPService] Using CORS proxy for ${config.url} -> ${url.href}`);
+ }
+
+ try {
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG) {
+ console.log(`[MCPService] Creating StreamableHTTP transport for ${url.href}`);
+ }
+
+ return {
+ transport: new StreamableHTTPClientTransport(url, {
+ requestInit,
+ fetch: diagnosticFetch
+ }),
+ type: MCPTransportType.STREAMABLE_HTTP,
+ stopPhaseLogging
+ };
+ } catch (httpError) {
+ console.warn(`[MCPService] StreamableHTTP failed, trying SSE transport...`, httpError);
+
+ try {
+ return {
+ transport: new SSEClientTransport(url, {
+ requestInit,
+ fetch: diagnosticFetch,
+ eventSourceInit: { fetch: diagnosticFetch }
+ }),
+ type: MCPTransportType.SSE,
+ stopPhaseLogging
+ };
+ } catch (sseError) {
+ const httpMsg = httpError instanceof Error ? httpError.message : String(httpError);
+ const sseMsg = sseError instanceof Error ? sseError.message : String(sseError);
+
+ throw new Error(`Failed to create transport. StreamableHTTP: ${httpMsg}; SSE: ${sseMsg}`);
+ }
+ }
+ }
+
+ /**
+ * Extract server info from SDK Implementation type.
+ * Normalizes the SDK's server version response into our MCPServerInfo type.
+ *
+ * @param impl - Raw Implementation object from MCP SDK
+ * @returns Normalized server info or undefined if input is empty
+ */
+ private static extractServerInfo(impl: Implementation | undefined): MCPServerInfo | undefined {
+ if (!impl) {
+ return undefined;
+ }
+
+ return {
+ name: impl.name,
+ version: impl.version,
+ title: impl.title,
+ description: impl.description,
+ websiteUrl: impl.websiteUrl,
+ icons: impl.icons?.map((icon: MCPResourceIcon) => ({
+ src: icon.src,
+ mimeType: icon.mimeType,
+ sizes: icon.sizes,
+ theme: icon.theme
+ }))
+ };
+ }
+
+ /**
+ * Connect to a single MCP server with detailed phase tracking.
+ *
+ * Performs the full MCP connection lifecycle:
+ * 1. Transport creation (with automatic fallback)
+ * 2. Client initialization and capability exchange
+ * 3. Tool discovery via `listTools`
+ *
+ * Reports progress via `onPhase` callback at each step, enabling
+ * UI progress indicators during connection.
+ *
+ * @param serverName - Display name for the server (used in logging)
+ * @param serverConfig - Server URL, transport type, proxy, and auth configuration
+ * @param clientInfo - Optional client identification (defaults to app info)
+ * @param capabilities - Optional client capability declaration
+ * @param onPhase - Optional callback for connection phase progress updates
+ * @param listChangedHandlers - Optional handlers for server-initiated list change notifications
+ * @returns Full connection object with client, transport, tools, server info, and timing
+ * @throws {Error} If transport creation or connection fails
+ */
+ static async connect(
+ serverName: string,
+ serverConfig: MCPServerConfig,
+ clientInfo?: Implementation,
+ capabilities?: ClientCapabilities,
+ onPhase?: MCPPhaseCallback,
+ listChangedHandlers?: ListChangedHandlers
+ ): Promise {
+ const startTime = performance.now();
+ const effectiveClientInfo = clientInfo ?? DEFAULT_MCP_CONFIG.clientInfo;
+ const effectiveCapabilities = capabilities ?? DEFAULT_MCP_CONFIG.capabilities;
+
+ // Phase: Creating transport
+ onPhase?.(
+ MCPConnectionPhase.TRANSPORT_CREATING,
+ this.createLog(
+ MCPConnectionPhase.TRANSPORT_CREATING,
+ `Creating transport for ${serverConfig.url}`
+ )
+ );
+
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG) {
+ console.log(`[MCPService][${serverName}] Creating transport...`);
+ }
+
+ const {
+ transport,
+ type: transportType,
+ stopPhaseLogging
+ } = this.createTransport(serverName, serverConfig, (log) => onPhase?.(log.phase, log));
+
+ // Setup WebSocket reconnection handler
+ if (transportType === MCPTransportType.WEBSOCKET) {
+ transport.onclose = () => {
+ console.log(`[MCPService][${serverName}] WebSocket closed, notifying for reconnection`);
+ onPhase?.(
+ MCPConnectionPhase.DISCONNECTED,
+ this.createLog(MCPConnectionPhase.DISCONNECTED, 'WebSocket connection closed')
+ );
+ };
+ }
+
+ // Phase: Transport ready
+ onPhase?.(
+ MCPConnectionPhase.TRANSPORT_READY,
+ this.createLog(MCPConnectionPhase.TRANSPORT_READY, `Transport ready (${transportType})`),
+ { transportType }
+ );
+
+ const client = new Client(
+ {
+ name: effectiveClientInfo.name,
+ version: effectiveClientInfo.version ?? DEFAULT_CLIENT_VERSION
+ },
+ {
+ capabilities: effectiveCapabilities,
+ listChanged: listChangedHandlers
+ }
+ );
+
+ const runtimeErrorHandler = (error: Error) => {
+ // the SDK reports any post initialize error here, including the abort we trigger
+ // ourselves on the next health check cycle, on tab unload, or on server teardown.
+ // these are lifecycle aborts, not actionable errors, so we keep them out of the red console.
+ // the SDK wraps the original AbortError in a generic Error like
+ // "SSE stream disconnected: AbortError: The operation was aborted."
+ // which isAbortError cannot recognize by name alone, so we also pattern match on the message
+ if (isAbortError(error)) {
+ return;
+ }
+ const msg = error?.message ?? '';
+ if (
+ /SSE stream disconnected:.*AbortError/i.test(msg) ||
+ /AbortError: .*aborted/i.test(msg) ||
+ /stream locked by a reader/i.test(msg)
+ ) {
+ return;
+ }
+ console.error(`[MCPService][${serverName}] Protocol error after initialize:`, error);
+ };
+
+ client.onerror = (error) => {
+ onPhase?.(
+ MCPConnectionPhase.ERROR,
+ this.createLog(
+ MCPConnectionPhase.ERROR,
+ `Protocol error: ${error.message}`,
+ MCPLogLevel.ERROR,
+ {
+ error: this.summarizeError(error)
+ }
+ )
+ );
+ };
+
+ // Phase: Initializing
+ onPhase?.(
+ MCPConnectionPhase.INITIALIZING,
+ this.createLog(MCPConnectionPhase.INITIALIZING, 'Sending initialize request...')
+ );
+
+ try {
+ await client.connect(transport);
+ // Transport diagnostics are only for the initial handshake, not long-lived traffic.
+ stopPhaseLogging();
+ client.onerror = runtimeErrorHandler;
+ } catch (error) {
+ client.onerror = runtimeErrorHandler;
+ const url =
+ (serverConfig.useProxy ?? false)
+ ? buildProxiedUrl(serverConfig.url)
+ : new URL(serverConfig.url);
+
+ onPhase?.(
+ MCPConnectionPhase.ERROR,
+ this.createLog(
+ MCPConnectionPhase.ERROR,
+ `Connection failed during initialize: ${
+ error instanceof Error ? error.message : String(error)
+ }`,
+ MCPLogLevel.ERROR,
+ {
+ error: this.summarizeError(error),
+ config: {
+ serverName,
+ configuredUrl: serverConfig.url,
+ effectiveUrl: url.href,
+ transportType,
+ useProxy: serverConfig.useProxy ?? false,
+ headers: sanitizeHeaders(
+ serverConfig.headers,
+ Object.keys(serverConfig.headers ?? {}),
+ MCP_PARTIAL_REDACT_HEADERS
+ ),
+ credentials: serverConfig.credentials
+ },
+ browser: this.getBrowserContext(url, serverConfig.useProxy ?? false),
+ hints: this.getConnectionHints(url, serverConfig, error)
+ }
+ )
+ );
+
+ throw error;
+ }
+
+ const serverVersion = client.getServerVersion();
+ const serverCapabilities = client.getServerCapabilities();
+ const instructions = client.getInstructions();
+ const serverInfo = this.extractServerInfo(serverVersion);
+
+ // Phase: Capabilities exchanged
+ onPhase?.(
+ MCPConnectionPhase.CAPABILITIES_EXCHANGED,
+ this.createLog(
+ MCPConnectionPhase.CAPABILITIES_EXCHANGED,
+ 'Capabilities exchanged successfully',
+ MCPLogLevel.INFO,
+ {
+ serverCapabilities,
+ serverInfo
+ }
+ ),
+ {
+ serverInfo,
+ serverCapabilities,
+ clientCapabilities: effectiveCapabilities,
+ instructions
+ }
+ );
+
+ // Phase: Listing tools
+ onPhase?.(
+ MCPConnectionPhase.LISTING_TOOLS,
+ this.createLog(MCPConnectionPhase.LISTING_TOOLS, 'Listing available tools...')
+ );
+
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG) {
+ console.log(`[MCPService][${serverName}] Connected, listing tools...`);
+ }
+
+ const tools = await this.listTools({
+ client,
+ transport,
+ tools: [],
+ serverName,
+ transportType,
+ connectionTimeMs: 0,
+ requestTimeoutMs:
+ serverConfig.requestTimeoutMs ?? DEFAULT_MCP_CONFIG.requestTimeoutSeconds * 1000
+ });
+
+ const connectionTimeMs = Math.round(performance.now() - startTime);
+
+ // Phase: Connected
+ onPhase?.(
+ MCPConnectionPhase.CONNECTED,
+ this.createLog(
+ MCPConnectionPhase.CONNECTED,
+ `Connection established with ${tools.length} tools (${connectionTimeMs}ms)`
+ )
+ );
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG) {
+ console.log(
+ `[MCPService][${serverName}] Initialization complete with ${tools.length} tools in ${connectionTimeMs}ms`
+ );
+ }
+
+ return {
+ client,
+ transport,
+ tools,
+ serverName,
+ transportType,
+ serverInfo,
+ serverCapabilities,
+ clientCapabilities: effectiveCapabilities,
+ protocolVersion: DEFAULT_MCP_CONFIG.protocolVersion,
+ instructions,
+ connectionTimeMs,
+ requestTimeoutMs:
+ serverConfig.requestTimeoutMs ?? DEFAULT_MCP_CONFIG.requestTimeoutSeconds * 1000
+ };
+ }
+
+ /**
+ * Disconnect from a server.
+ * Clears the `onclose` handler to prevent reconnection attempts on voluntary disconnect.
+ *
+ * @param connection - The active MCP connection to close
+ */
+ static async disconnect(connection: MCPConnection): Promise {
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG) {
+ console.log(`[MCPService][${connection.serverName}] Disconnecting...`);
+ }
+
+ try {
+ // Terminate the session first for streamable-http transports to cleanly
+ // close streams, matching the inspector's disconnect flow.
+ if (connection.transport instanceof StreamableHTTPClientTransport) {
+ await connection.transport.terminateSession();
+ }
+
+ // Clear error handlers before closing to prevent noise from expected
+ // abort errors during shutdown. The inspector avoids this entirely
+ // by not setting onerror, but since we use it for protocol logging,
+ // we must clear it before disconnect.
+ connection.client.onerror = undefined;
+ if (connection.transport.onclose) {
+ connection.transport.onclose = undefined;
+ }
+
+ await connection.client.close();
+ } catch (error) {
+ console.warn(`[MCPService][${connection.serverName}] Error during disconnect:`, error);
+ }
+ }
+
+ /**
+ * List tools from a connection.
+ * Silently returns empty array on failure (logged as warning).
+ *
+ * @param connection - The MCP connection to query
+ * @returns Array of available tools, or empty array on error
+ */
+ static async listTools(connection: MCPConnection): Promise {
+ try {
+ const result = await connection.client.listTools();
+
+ return result.tools ?? [];
+ } catch (error) {
+ // Let session-expired errors propagate for reconnection handling
+ if (this.isSessionExpiredError(error)) {
+ throw error;
+ }
+
+ console.warn(`[MCPService][${connection.serverName}] Failed to list tools:`, error);
+
+ return [];
+ }
+ }
+
+ /**
+ * List prompts from a connection.
+ * Silently returns empty array on failure (logged as warning).
+ *
+ * @param connection - The MCP connection to query
+ * @returns Array of available prompts, or empty array on error
+ */
+ static async listPrompts(connection: MCPConnection): Promise {
+ try {
+ const result = await connection.client.listPrompts();
+
+ return result.prompts ?? [];
+ } catch (error) {
+ // Let session-expired errors propagate for reconnection handling
+ if (this.isSessionExpiredError(error)) {
+ throw error;
+ }
+
+ console.warn(`[MCPService][${connection.serverName}] Failed to list prompts:`, error);
+
+ return [];
+ }
+ }
+
+ /**
+ * Get a specific prompt with arguments.
+ * Unlike list operations, this throws on failure since the caller explicitly
+ * requested a specific prompt and needs to handle the error.
+ *
+ * @param connection - The MCP connection to use
+ * @param name - The prompt name to retrieve
+ * @param args - Optional key-value arguments to pass to the prompt
+ * @returns The prompt result with messages and metadata
+ * @throws {Error} If the prompt retrieval fails
+ */
+ static async getPrompt(
+ connection: MCPConnection,
+ name: string,
+ args?: Record
+ ): Promise {
+ try {
+ return await connection.client.getPrompt({ name, arguments: args });
+ } catch (error) {
+ console.error(`[MCPService][${connection.serverName}] Failed to get prompt:`, error);
+
+ throw error;
+ }
+ }
+
+ /**
+ * Execute a tool call on a connection.
+ * Supports abort signal for cancellable operations (e.g., when user stops generation).
+ * Formats the raw tool result into a string representation.
+ *
+ * @param connection - The MCP connection to execute against
+ * @param params - Tool name and arguments to execute
+ * @param signal - Optional AbortSignal for cancellation support
+ * @returns Formatted tool execution result with content string and error flag
+ * @throws {Error} If tool execution fails or is aborted
+ */
+ static async callTool(
+ connection: MCPConnection,
+ params: ToolCallParams,
+ signal?: AbortSignal
+ ): Promise {
+ throwIfAborted(signal);
+
+ try {
+ const result = await connection.client.callTool(
+ { name: params.name, arguments: params.arguments },
+ undefined,
+ { signal, timeout: connection.requestTimeoutMs }
+ );
+
+ return {
+ content: this.formatToolResult(result as ToolCallResult),
+ isError: (result as ToolCallResult).isError ?? false
+ };
+ } catch (error) {
+ if (isAbortError(error)) {
+ throw error;
+ }
+
+ // Let session-expired errors propagate unwrapped for reconnection handling
+ if (this.isSessionExpiredError(error)) {
+ throw error;
+ }
+
+ const message = error instanceof Error ? error.message : String(error);
+
+ throw new Error(
+ `Tool "${params.name}" execution failed on server "${connection.serverName}": ${message}`,
+ { cause: error instanceof Error ? error : undefined }
+ );
+ }
+ }
+
+ /**
+ * Format tool result content items to a single string.
+ * Handles text, image (base64 data URL), and embedded resource content types.
+ *
+ * @param result - Raw tool call result from MCP SDK
+ * @returns Concatenated string representation of all content items
+ */
+ private static formatToolResult(result: ToolCallResult): string {
+ const content = result.content;
+ if (!Array.isArray(content)) return '';
+
+ return content
+ .map((item) => this.formatSingleContent(item))
+ .filter(Boolean)
+ .join('\n');
+ }
+
+ private static formatSingleContent(content: ToolResultContentItem): string {
+ if (content.type === MCPContentType.TEXT && content.text) {
+ return content.text;
+ }
+
+ if (content.type === MCPContentType.IMAGE && content.data) {
+ return createBase64DataUrl(content.mimeType ?? DEFAULT_IMAGE_MIME_TYPE, content.data);
+ }
+
+ if (content.type === MCPContentType.RESOURCE && content.resource) {
+ const resource = content.resource;
+
+ if (resource.text) return resource.text;
+ if (resource.blob) return resource.blob;
+
+ return JSON.stringify(resource);
+ }
+
+ if (content.data && content.mimeType) {
+ return createBase64DataUrl(content.mimeType, content.data);
+ }
+
+ return JSON.stringify(content);
+ }
+
+ /**
+ *
+ *
+ * Completions Operations
+ *
+ *
+ */
+
+ /**
+ * Request completion suggestions from a server.
+ * Used for autocompleting prompt arguments or resource URI templates.
+ *
+ * @param connection - The MCP connection to use
+ * @param ref - Reference to the prompt or resource template
+ * @param argument - The argument being completed (name and current value)
+ * @returns Completion result with suggested values
+ */
+ static async complete(
+ connection: MCPConnection,
+ ref: { type: MCPRefType.PROMPT; name: string } | { type: MCPRefType.RESOURCE; uri: string },
+ argument: { name: string; value: string }
+ ): Promise<{ values: string[]; total?: number; hasMore?: boolean } | null> {
+ try {
+ const result = await connection.client.complete({
+ ref,
+ argument
+ });
+
+ return result.completion;
+ } catch (error) {
+ console.error(`[MCPService] Failed to get completions:`, error);
+
+ return null;
+ }
+ }
+
+ /**
+ *
+ *
+ * Resources Operations
+ *
+ *
+ */
+
+ /**
+ * List resources from a connection.
+ * @param connection - The MCP connection to use
+ * @param cursor - Optional pagination cursor
+ * @returns Array of available resources and optional next cursor
+ */
+ static async listResources(
+ connection: MCPConnection,
+ cursor?: string
+ ): Promise<{ resources: MCPResource[]; nextCursor?: string }> {
+ try {
+ const result = await connection.client.listResources(cursor ? { cursor } : undefined);
+
+ return {
+ resources: (result.resources ?? []) as MCPResource[],
+ nextCursor: result.nextCursor
+ };
+ } catch (error) {
+ if (this.isSessionExpiredError(error)) {
+ throw error;
+ }
+
+ console.warn(`[MCPService][${connection.serverName}] Failed to list resources:`, error);
+
+ return { resources: [] };
+ }
+ }
+
+ /**
+ * List all resources from a connection (handles pagination automatically).
+ * @param connection - The MCP connection to use
+ * @returns Array of all available resources
+ */
+ static async listAllResources(connection: MCPConnection): Promise {
+ const allResources: MCPResource[] = [];
+ let cursor: string | undefined;
+
+ do {
+ const result = await this.listResources(connection, cursor);
+ allResources.push(...result.resources);
+ cursor = result.nextCursor;
+ } while (cursor);
+
+ return allResources;
+ }
+
+ /**
+ * List resource templates from a connection.
+ * @param connection - The MCP connection to use
+ * @param cursor - Optional pagination cursor
+ * @returns Array of available resource templates and optional next cursor
+ */
+ static async listResourceTemplates(
+ connection: MCPConnection,
+ cursor?: string
+ ): Promise<{ resourceTemplates: MCPResourceTemplate[]; nextCursor?: string }> {
+ try {
+ const result = await connection.client.listResourceTemplates(cursor ? { cursor } : undefined);
+
+ return {
+ resourceTemplates: (result.resourceTemplates ?? []) as MCPResourceTemplate[],
+ nextCursor: result.nextCursor
+ };
+ } catch (error) {
+ if (this.isSessionExpiredError(error)) {
+ throw error;
+ }
+
+ console.warn(
+ `[MCPService][${connection.serverName}] Failed to list resource templates:`,
+ error
+ );
+
+ return { resourceTemplates: [] };
+ }
+ }
+
+ /**
+ * List all resource templates from a connection (handles pagination automatically).
+ * @param connection - The MCP connection to use
+ * @returns Array of all available resource templates
+ */
+ static async listAllResourceTemplates(connection: MCPConnection): Promise {
+ const allTemplates: MCPResourceTemplate[] = [];
+ let cursor: string | undefined;
+
+ do {
+ const result = await this.listResourceTemplates(connection, cursor);
+ allTemplates.push(...result.resourceTemplates);
+ cursor = result.nextCursor;
+ } while (cursor);
+
+ return allTemplates;
+ }
+
+ /**
+ * Read the contents of a resource.
+ * @param connection - The MCP connection to use
+ * @param uri - The URI of the resource to read
+ * @returns The resource contents
+ */
+ static async readResource(
+ connection: MCPConnection,
+ uri: string
+ ): Promise {
+ try {
+ const result = await connection.client.readResource({ uri });
+
+ return {
+ contents: (result.contents ?? []) as MCPResourceContent[],
+ _meta: result._meta
+ };
+ } catch (error) {
+ console.error(`[MCPService][${connection.serverName}] Failed to read resource:`, error);
+
+ throw error;
+ }
+ }
+
+ /**
+ * Subscribe to updates for a resource.
+ * The server will send notifications/resources/updated when the resource changes.
+ * @param connection - The MCP connection to use
+ * @param uri - The URI of the resource to subscribe to
+ */
+ static async subscribeResource(connection: MCPConnection, uri: string): Promise {
+ try {
+ await connection.client.subscribeResource({ uri });
+
+ console.log(`[MCPService][${connection.serverName}] Subscribed to resource: ${uri}`);
+ } catch (error) {
+ console.error(
+ `[MCPService][${connection.serverName}] Failed to subscribe to resource:`,
+ error
+ );
+
+ throw error;
+ }
+ }
+
+ /**
+ * Unsubscribe from updates for a resource.
+ * @param connection - The MCP connection to use
+ * @param uri - The URI of the resource to unsubscribe from
+ */
+ static async unsubscribeResource(connection: MCPConnection, uri: string): Promise {
+ try {
+ await connection.client.unsubscribeResource({ uri });
+
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG) {
+ console.log(`[MCPService][${connection.serverName}] Unsubscribed from resource: ${uri}`);
+ }
+ } catch (error) {
+ console.error(
+ `[MCPService][${connection.serverName}] Failed to unsubscribe from resource:`,
+ error
+ );
+
+ throw error;
+ }
+ }
+
+ /**
+ * Check if a connection supports resources.
+ * Per MCP spec: presence of the `resources` key (even as empty object `{}`) indicates support.
+ * Empty object means resources are supported but no sub-features (subscribe, listChanged).
+ *
+ * @param connection - The MCP connection to check
+ * @returns Whether the server declares the resources capability
+ */
+ static supportsResources(connection: MCPConnection): boolean {
+ // Per MCP spec: "Servers that support resources MUST declare the resources capability"
+ // The presence of the key indicates support, even if it's an empty object
+ return connection.serverCapabilities?.resources !== undefined;
+ }
+
+ /**
+ * Check if a connection supports resource subscriptions.
+ * @param connection - The MCP connection to check
+ * @returns Whether the server supports resource subscriptions
+ */
+ static supportsResourceSubscriptions(connection: MCPConnection): boolean {
+ return !!connection.serverCapabilities?.resources?.subscribe;
+ }
+}
diff --git a/llama.cpp/tools/ui/src/lib/services/migration.service.ts b/llama.cpp/tools/ui/src/lib/services/migration.service.ts
new file mode 100644
index 0000000000000000000000000000000000000000..d7709bc6bd8c9b2f259b572002c802dcacda60e4
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/migration.service.ts
@@ -0,0 +1,662 @@
+/**
+ * Migration Service - Unified data migration hook
+ *
+ * Centralizes all data migrations (localStorage, IndexedDB, legacy formats) into a single
+ * initialization point. Each migration copies data to new format WITHOUT deleting the old.
+ *
+ * **Architecture:**
+ * - Migrations are defined as objects with `id` and `run()` methods
+ * - Migration state is tracked in localStorage to avoid re-running
+ * - `runAllMigrations()` should be called once at app startup
+ * - All migrations are NON-DESTRUCTIVE - legacy data is preserved for downgrade compatibility
+ *
+ * **Current Migrations:**
+ * 1. localStorage prefix: Copy LlamaCppWebui.* → LlamaUi.* (both preserved)
+ * 2. IndexedDB database: Copy LlamacppWebui → LlamaUi (both preserved)
+ * 3. Legacy message format: Transform in-place (preserves structure, migrates markers)
+ * 4. Theme key: Copy standalone `theme` → config object (both preserved)
+ */
+
+import Dexie from 'dexie';
+import {
+ STORAGE_APP_NAME,
+ STORAGE_APP_NAME_DEPRECATED,
+ DB_APP_NAME_DEPRECATED,
+ CONFIG_LOCALSTORAGE_KEY,
+ IDXDB_TABLES,
+ IDXDB_STORES,
+ NEW_TO_DEPRECATED_MAP
+} from '$lib/constants';
+import { LEGACY_AGENTIC_REGEX, LEGACY_REASONING_TAGS } from '$lib/constants/agentic';
+import { SETTINGS_KEYS } from '$lib/constants/settings-registry';
+import { MessageRole } from '$lib/enums';
+
+// Types
+
+interface Migration {
+ /** Unique identifier for this migration */
+ id: string;
+ /** Human-readable description */
+ description: string;
+ /** Run the migration forward (non-destructive - copies, doesn't delete) */
+ run(): Promise;
+}
+
+interface MigrationState {
+ completed: string[];
+ failed: string[];
+ lastRun: string;
+}
+
+// Constants
+
+const MIGRATION_STATE_KEY = `${STORAGE_APP_NAME}.migration-state`;
+const MIGRATION_STATE_VERSION = 1;
+
+// State Management
+
+function getMigrationState(): MigrationState {
+ try {
+ const raw = localStorage.getItem(MIGRATION_STATE_KEY);
+ if (!raw) return { completed: [], failed: [], lastRun: '' };
+ const parsed = JSON.parse(raw);
+ if (parsed.version !== MIGRATION_STATE_VERSION) {
+ return { completed: [], failed: [], lastRun: '' };
+ }
+ return {
+ completed: parsed.completed ?? [],
+ failed: parsed.failed ?? [],
+ lastRun: parsed.lastRun ?? ''
+ };
+ } catch {
+ return { completed: [], failed: [], lastRun: '' };
+ }
+}
+
+function saveMigrationState(state: MigrationState): void {
+ localStorage.setItem(
+ MIGRATION_STATE_KEY,
+ JSON.stringify({
+ version: MIGRATION_STATE_VERSION,
+ ...state,
+ lastRun: new Date().toISOString()
+ })
+ );
+}
+
+function isMigrationCompleted(id: string): boolean {
+ const state = getMigrationState();
+ return state.completed.includes(id);
+}
+
+function markMigrationCompleted(id: string): void {
+ const state = getMigrationState();
+ if (!state.completed.includes(id)) {
+ state.completed.push(id);
+ }
+ state.failed = state.failed.filter((f) => f !== id);
+ saveMigrationState(state);
+}
+
+function markMigrationFailed(id: string): void {
+ const state = getMigrationState();
+ if (!state.failed.includes(id)) {
+ state.failed.push(id);
+ }
+ saveMigrationState(state);
+}
+
+// Migration 1: LocalStorage Key Prefix (Non-Destructive)
+
+const LOCALSTORAGE_MIGRATION_ID = 'localstorage-prefix-v1';
+
+const localStorageMigration: Migration = {
+ id: LOCALSTORAGE_MIGRATION_ID,
+ description: 'Copy localStorage keys from LlamaCppWebui to LlamaUi prefix (non-destructive)',
+
+ async run(): Promise {
+ // Non-destructive: copy to new key, but KEEP the old key
+ for (const [newKey, deprecatedKey] of Object.entries(NEW_TO_DEPRECATED_MAP)) {
+ // Only migrate if new key doesn't already exist
+ const newValue = localStorage.getItem(newKey);
+ if (newValue !== null) {
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log(`[Migration] localStorage: ${newKey} already exists, skipping`);
+ continue;
+ }
+
+ const oldValue = localStorage.getItem(deprecatedKey);
+ if (oldValue !== null) {
+ localStorage.setItem(newKey, oldValue);
+ // Keep old key for downgrade compatibility - DO NOT DELETE
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG) {
+ console.log(
+ `[Migration] localStorage: copied ${deprecatedKey} → ${newKey} (preserved old)`
+ );
+ }
+ }
+ }
+ }
+};
+
+// Migration 2: IndexedDB Database Name (Non-Destructive)
+
+const IDXDB_MIGRATION_ID = 'idxdb-database-v1';
+
+const idxdbMigration: Migration = {
+ id: IDXDB_MIGRATION_ID,
+ description: 'Copy IndexedDB from LlamacppWebui to LlamaUi database (non-destructive)',
+
+ async run(): Promise {
+ const oldDbNames = await Dexie.getDatabaseNames();
+ if (!oldDbNames.includes(DB_APP_NAME_DEPRECATED)) {
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log('[Migration] IndexedDB: no old database found, skipping');
+ return;
+ }
+
+ // Check if new database already has data
+ const newDb = new Dexie(STORAGE_APP_NAME);
+ newDb.version(1).stores(IDXDB_STORES);
+ const existingConvs = await newDb.table(IDXDB_TABLES.conversations).count();
+ if (existingConvs > 0) {
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log('[Migration] IndexedDB: new database already has data, skipping');
+ return;
+ }
+
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log('[Migration] IndexedDB: copying from', DB_APP_NAME_DEPRECATED);
+
+ const oldDb = new Dexie(DB_APP_NAME_DEPRECATED);
+ oldDb.version(1).stores(IDXDB_STORES);
+
+ const conversations = await oldDb.table(IDXDB_TABLES.conversations).toArray();
+ const messages = await oldDb.table(IDXDB_TABLES.messages).toArray();
+
+ if (conversations.length > 0) {
+ await newDb.table(IDXDB_TABLES.conversations).bulkAdd(conversations);
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log(`[Migration] IndexedDB: copied ${conversations.length} conversations`);
+ }
+ if (messages.length > 0) {
+ await newDb.table(IDXDB_TABLES.messages).bulkAdd(messages);
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log(`[Migration] IndexedDB: copied ${messages.length} messages`);
+ }
+
+ // Non-destructive: DO NOT delete old database - keep for downgrade compatibility
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log('[Migration] IndexedDB: preserved old database for downgrade compatibility');
+ }
+};
+
+// Migration 3: Legacy Message Format
+
+const LEGACY_MESSAGE_MIGRATION_ID = 'legacy-message-format-v2';
+
+interface ParsedTurn {
+ textBefore: string;
+ toolCalls: Array<{ name: string; args: string; result: string }>;
+}
+
+function parseLegacyToolCalls(content: string): ParsedTurn[] {
+ const turns: ParsedTurn[] = [];
+ const regex = new RegExp(LEGACY_AGENTIC_REGEX.COMPLETED_TOOL_CALL.source, 'g');
+
+ let lastIndex = 0;
+ let currentTurn: ParsedTurn = { textBefore: '', toolCalls: [] };
+ let match;
+
+ while ((match = regex.exec(content)) !== null) {
+ const textBefore = content.slice(lastIndex, match.index).trim();
+
+ if (textBefore && currentTurn.toolCalls.length > 0) {
+ turns.push(currentTurn);
+ currentTurn = { textBefore, toolCalls: [] };
+ } else if (textBefore && currentTurn.toolCalls.length === 0) {
+ currentTurn.textBefore = textBefore;
+ }
+
+ currentTurn.toolCalls.push({
+ name: match[1],
+ args: match[2],
+ result: match[3].replace(/^\n+|\n+$/g, '')
+ });
+
+ lastIndex = match.index + match[0].length;
+ }
+
+ const remainingText = content.slice(lastIndex).trim();
+
+ if (currentTurn.toolCalls.length > 0) {
+ turns.push(currentTurn);
+ }
+
+ if (remainingText) {
+ const cleanRemaining = remainingText
+ .replace(LEGACY_AGENTIC_REGEX.AGENTIC_TOOL_CALL_OPEN, '')
+ .trim();
+ if (cleanRemaining) {
+ turns.push({ textBefore: cleanRemaining, toolCalls: [] });
+ }
+ }
+
+ if (turns.length === 0) {
+ turns.push({ textBefore: content.trim(), toolCalls: [] });
+ }
+
+ return turns;
+}
+
+function extractLegacyReasoning(content: string): { reasoning: string; cleanContent: string } {
+ let reasoning = '';
+ let cleanContent = content;
+
+ const re = new RegExp(LEGACY_AGENTIC_REGEX.REASONING_EXTRACT.source, 'g');
+ let match;
+ while ((match = re.exec(content)) !== null) {
+ reasoning += match[1];
+ }
+
+ cleanContent = cleanContent
+ .replace(new RegExp(LEGACY_AGENTIC_REGEX.REASONING_BLOCK.source, 'g'), '')
+ .replace(LEGACY_AGENTIC_REGEX.REASONING_OPEN, '');
+
+ return { reasoning, cleanContent };
+}
+
+function hasLegacyMarkers(content: string): boolean {
+ return LEGACY_AGENTIC_REGEX.HAS_LEGACY_MARKERS.test(content);
+}
+
+let DatabaseService: typeof import('./database.service').DatabaseService | null = null;
+
+async function getDatabaseService() {
+ if (!DatabaseService) {
+ const module = await import('./database.service');
+ DatabaseService = module.DatabaseService;
+ }
+ return DatabaseService;
+}
+
+const legacyMessageMigration: Migration = {
+ id: LEGACY_MESSAGE_MIGRATION_ID,
+ description: 'Migrate legacy marker-based messages to structured format',
+
+ async run(): Promise {
+ const db = await getDatabaseService();
+ const conversations = await db.getAllConversations();
+ let migratedCount = 0;
+
+ for (const conv of conversations) {
+ const allMessages = await db.getConversationMessages(conv.id);
+
+ for (const message of allMessages) {
+ if (message.role !== MessageRole.ASSISTANT) {
+ if (message.content?.includes(LEGACY_REASONING_TAGS.START)) {
+ const { reasoning, cleanContent } = extractLegacyReasoning(message.content);
+ await db.updateMessage(message.id, {
+ content: cleanContent.trim(),
+ reasoningContent: reasoning || undefined
+ });
+ migratedCount++;
+ }
+ continue;
+ }
+
+ if (!hasLegacyMarkers(message.content ?? '')) continue;
+
+ const { reasoning, cleanContent } = extractLegacyReasoning(message.content);
+ const turns = parseLegacyToolCalls(cleanContent);
+
+ let existingToolCalls: Array<{
+ id: string;
+ function?: { name: string; arguments: string };
+ }> = [];
+ if (message.toolCalls) {
+ try {
+ existingToolCalls = JSON.parse(message.toolCalls);
+ } catch {
+ // Ignore
+ }
+ }
+
+ const firstTurn = turns[0];
+ if (!firstTurn) continue;
+
+ const firstTurnToolCalls = firstTurn.toolCalls.map((tc, i) => {
+ const existing =
+ existingToolCalls.find((e) => e.function?.name === tc.name) || existingToolCalls[i];
+ return {
+ id: existing?.id || `legacy_tool_${i}`,
+ type: 'function' as const,
+ function: { name: tc.name, arguments: tc.args }
+ };
+ });
+
+ await db.updateMessage(message.id, {
+ content: firstTurn.textBefore,
+ reasoningContent: reasoning || undefined,
+ toolCalls: firstTurnToolCalls.length > 0 ? JSON.stringify(firstTurnToolCalls) : ''
+ });
+
+ let currentParentId = message.id;
+ let toolCallIdCounter = existingToolCalls.length;
+
+ for (let i = 0; i < firstTurn.toolCalls.length; i++) {
+ const tc = firstTurn.toolCalls[i];
+ const toolCallId = firstTurnToolCalls[i]?.id || `legacy_tool_${i}`;
+
+ const toolMsg = await db.createMessageBranch(
+ {
+ convId: conv.id,
+ type: 'text',
+ role: MessageRole.TOOL,
+ content: tc.result,
+ toolCallId,
+ timestamp: message.timestamp + i + 1,
+ toolCalls: '',
+ children: []
+ },
+ currentParentId
+ );
+ currentParentId = toolMsg.id;
+ }
+
+ for (let turnIdx = 1; turnIdx < turns.length; turnIdx++) {
+ const turn = turns[turnIdx];
+
+ const turnToolCalls = turn.toolCalls.map((tc, i) => {
+ const idx = toolCallIdCounter + i;
+ const existing = existingToolCalls[idx];
+ return {
+ id: existing?.id || `legacy_tool_${idx}`,
+ type: 'function' as const,
+ function: { name: tc.name, arguments: tc.args }
+ };
+ });
+ toolCallIdCounter += turn.toolCalls.length;
+
+ const assistantMsg = await db.createMessageBranch(
+ {
+ convId: conv.id,
+ type: 'text',
+ role: MessageRole.ASSISTANT,
+ content: turn.textBefore,
+ timestamp: message.timestamp + turnIdx * 100,
+ toolCalls: turnToolCalls.length > 0 ? JSON.stringify(turnToolCalls) : '',
+ children: [],
+ model: message.model
+ },
+ currentParentId
+ );
+ currentParentId = assistantMsg.id;
+
+ for (let i = 0; i < turn.toolCalls.length; i++) {
+ const tc = turn.toolCalls[i];
+ const toolCallId = turnToolCalls[i]?.id || `legacy_tool_${toolCallIdCounter + i}`;
+
+ const toolMsg = await db.createMessageBranch(
+ {
+ convId: conv.id,
+ type: 'text',
+ role: MessageRole.TOOL,
+ content: tc.result,
+ toolCallId,
+ timestamp: message.timestamp + turnIdx * 100 + i + 1,
+ toolCalls: '',
+ children: []
+ },
+ currentParentId
+ );
+ currentParentId = toolMsg.id;
+ }
+ }
+
+ if (message.children.length > 0 && currentParentId !== message.id) {
+ for (const childId of message.children) {
+ const child = allMessages.find((m) => m.id === childId);
+ if (!child) continue;
+ if (child.role !== MessageRole.TOOL) {
+ await db.updateMessage(childId, { parent: currentParentId });
+ }
+ }
+ await db.updateMessage(message.id, { children: [] });
+ }
+
+ migratedCount++;
+ }
+ }
+
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log(`[Migration] Legacy messages: migrated ${migratedCount} messages`);
+ }
+};
+
+// Migration 4: Theme Key (Non-Destructive)
+
+const THEME_MIGRATION_ID = 'theme-key-v1';
+
+const themeMigration: Migration = {
+ id: THEME_MIGRATION_ID,
+ description: 'Copy standalone theme key to config object (non-destructive)',
+
+ async run(): Promise {
+ const legacyTheme = localStorage.getItem('theme');
+ if (legacyTheme === null) {
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log('[Migration] Theme: no legacy theme key found, skipping');
+ return;
+ }
+
+ // Check if config already has theme
+ const configRaw = localStorage.getItem(CONFIG_LOCALSTORAGE_KEY);
+ const config = configRaw ? JSON.parse(configRaw) : {};
+
+ if (SETTINGS_KEYS.THEME in config) {
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log('[Migration] Theme: config already has theme, skipping');
+ return;
+ }
+
+ config[SETTINGS_KEYS.THEME] = legacyTheme;
+ localStorage.setItem(CONFIG_LOCALSTORAGE_KEY, JSON.stringify(config));
+
+ // Non-destructive: DO NOT delete legacy theme key - keep for downgrade compatibility
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log(`[Migration] Theme: copied standalone theme to config (preserved old key)`);
+ }
+};
+
+// Migration Registry & Runner
+
+const CUSTOM_JSON_MIGRATION_ID = 'custom-json-key-v1';
+
+const customJsonKeyMigration: Migration = {
+ id: CUSTOM_JSON_MIGRATION_ID,
+ description: 'Copy legacy custom config key to customJson (non-destructive)',
+
+ async run(): Promise {
+ const configRaw = localStorage.getItem(CONFIG_LOCALSTORAGE_KEY);
+ if (configRaw === null) return;
+
+ const config = JSON.parse(configRaw);
+
+ if (!('custom' in config)) return;
+ if (SETTINGS_KEYS.CUSTOM_JSON in config) return;
+
+ config[SETTINGS_KEYS.CUSTOM_JSON] = config.custom;
+ localStorage.setItem(CONFIG_LOCALSTORAGE_KEY, JSON.stringify(config));
+
+ // Non-destructive: keep the legacy custom key for downgrade compatibility
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log(`[Migration] Custom JSON: copied custom to customJson (preserved old key)`);
+ }
+};
+
+const MCP_DEFAULT_ENABLED_MIGRATION_ID = 'mcp-default-enabled-to-config-v1';
+
+const LEGACY_MCP_DEFAULT_ENABLED_KEY = `${STORAGE_APP_NAME}.mcpDefaultEnabled`;
+const DEPRECATED_LEGACY_MCP_DEFAULT_ENABLED_KEY = `${STORAGE_APP_NAME_DEPRECATED}.mcpDefaultEnabled`;
+
+const mcpDefaultEnabledMigration: Migration = {
+ id: MCP_DEFAULT_ENABLED_MIGRATION_ID,
+ description:
+ 'Copy mcpDefaultEnabled localStorage key into settings config (preserves legacy keys)',
+
+ async run(): Promise {
+ const raw =
+ localStorage.getItem(LEGACY_MCP_DEFAULT_ENABLED_KEY) ??
+ localStorage.getItem(DEPRECATED_LEGACY_MCP_DEFAULT_ENABLED_KEY);
+
+ // Legacy keys intentionally left in place so a downgrade keeps reading them.
+
+ if (raw === null) {
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log('[Migration] MCP default enabled: no legacy key found, skipping');
+ return;
+ }
+
+ const configRaw = localStorage.getItem(CONFIG_LOCALSTORAGE_KEY);
+ const config = configRaw ? JSON.parse(configRaw) : {};
+
+ // Don't overwrite an existing config entry — current data wins.
+ if (SETTINGS_KEYS.MCP_DEFAULT_SERVER_OVERRIDES in config) {
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log('[Migration] MCP default enabled: config already has overrides, skipping');
+ return;
+ }
+
+ try {
+ const parsed = JSON.parse(raw);
+ if (!Array.isArray(parsed)) return;
+ const valid = parsed.every(
+ (o) =>
+ typeof o === 'object' &&
+ o !== null &&
+ typeof (o as Record).serverId === 'string' &&
+ typeof (o as Record).enabled === 'boolean'
+ );
+ if (!valid) return;
+ } catch {
+ return;
+ }
+
+ config[SETTINGS_KEYS.MCP_DEFAULT_SERVER_OVERRIDES] = raw;
+ localStorage.setItem(CONFIG_LOCALSTORAGE_KEY, JSON.stringify(config));
+
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log('[Migration] MCP default enabled: moved legacy key into config');
+ }
+};
+
+const CONFIG_TYPES_MIGRATION_ID = 'config-type-normalization-v1';
+
+const configTypesMigration: Migration = {
+ id: CONFIG_TYPES_MIGRATION_ID,
+ description: 'Coerce legacy string-encoded booleans in persisted config to real booleans',
+
+ async run(): Promise {
+ const configRaw = localStorage.getItem(CONFIG_LOCALSTORAGE_KEY);
+ if (configRaw === null) return;
+
+ const config = JSON.parse(configRaw);
+ let changed = false;
+
+ // Pre-schema configs persisted booleans as the strings "true"/"false", which the
+ // strict server schema now rejects. Coerce those back to real booleans. No config
+ // string field holds exactly "true"/"false", so the match is unambiguous.
+ for (const key of Object.keys(config)) {
+ if (config[key] === 'true') {
+ config[key] = true;
+ changed = true;
+ } else if (config[key] === 'false') {
+ config[key] = false;
+ changed = true;
+ }
+ }
+
+ if (changed) {
+ localStorage.setItem(CONFIG_LOCALSTORAGE_KEY, JSON.stringify(config));
+ }
+
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log(`[Migration] Config types: coerced string booleans (changed=${changed})`);
+ }
+};
+
+const migrations: Migration[] = [
+ localStorageMigration,
+ idxdbMigration,
+ legacyMessageMigration,
+ themeMigration,
+ customJsonKeyMigration,
+ mcpDefaultEnabledMigration,
+ configTypesMigration
+];
+
+export const MigrationService = {
+ /**
+ * Get all registered migrations
+ */
+ getMigrations(): Migration[] {
+ return [...migrations];
+ },
+
+ /**
+ * Check if a specific migration has been completed
+ */
+ isCompleted(id: string): boolean {
+ return isMigrationCompleted(id);
+ },
+
+ /**
+ * Get current migration state
+ */
+ getState(): MigrationState {
+ return getMigrationState();
+ },
+
+ /**
+ * Reset migration state (use with caution - migrations will run again)
+ */
+ resetState(): void {
+ localStorage.removeItem(MIGRATION_STATE_KEY);
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log('[Migration] State reset - all migrations will run again');
+ },
+
+ /**
+ * Run all pending migrations (non-destructive - preserves legacy data)
+ * Should be called once at app initialization
+ */
+ async runAllMigrations(): Promise {
+ const state = getMigrationState();
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log('[Migration] Starting migration run, state:', state);
+
+ for (const migration of migrations) {
+ if (isMigrationCompleted(migration.id)) {
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log(`[Migration] ${migration.id}: already completed, skipping`);
+ continue;
+ }
+
+ try {
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log(`[Migration] ${migration.id}: running...`);
+ await migration.run();
+ markMigrationCompleted(migration.id);
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log(`[Migration] ${migration.id}: completed successfully`);
+ } catch (error) {
+ console.error(`[Migration] ${migration.id}: failed`, error);
+ markMigrationFailed(migration.id);
+ }
+ }
+
+ if (import.meta.env.DEV && import.meta.env.VITE_DEBUG)
+ console.log('[Migration] All migrations complete');
+ }
+};
diff --git a/llama.cpp/tools/ui/src/lib/services/models.service.ts b/llama.cpp/tools/ui/src/lib/services/models.service.ts
new file mode 100644
index 0000000000000000000000000000000000000000..9574da59ef9f6a0b4e56910f97fa60e2f9b48343
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/models.service.ts
@@ -0,0 +1,233 @@
+import { ServerModelStatus } from '$lib/enums';
+import { apiFetch, apiPost, normalizeModelName } from '$lib/utils';
+import type { ParsedModelId } from '$lib/types/models';
+import {
+ MODEL_QUANTIZATION_SEGMENT_RE,
+ MODEL_CUSTOM_QUANTIZATION_PREFIX_RE,
+ MODEL_PARAMS_RE,
+ MODEL_ACTIVATED_PARAMS_RE,
+ MODEL_IGNORED_SEGMENTS,
+ MODEL_WEIGHT_EXTENSION_RE,
+ MODEL_ID_NOT_FOUND,
+ MODEL_ID_ORG_SEPARATOR,
+ MODEL_ID_SEGMENT_SEPARATOR,
+ MODEL_ID_QUANTIZATION_SEPARATOR,
+ API_MODELS
+} from '$lib/constants';
+
+export class ModelsService {
+ /**
+ *
+ *
+ * Listing
+ *
+ *
+ */
+
+ /**
+ * Fetch list of models from OpenAI-compatible endpoint.
+ * Works in both MODEL and ROUTER modes.
+ *
+ * @returns List of available models with basic metadata
+ */
+ static async list(): Promise {
+ return apiFetch(API_MODELS.LIST);
+ }
+
+ /**
+ * Fetch list of all models with detailed metadata (ROUTER mode).
+ * Returns models with load status, paths, and other metadata
+ * beyond what the OpenAI-compatible endpoint provides.
+ *
+ * @returns List of models with detailed status and configuration info
+ */
+ static async listRouter(): Promise {
+ return apiFetch(API_MODELS.LIST);
+ }
+
+ /**
+ *
+ *
+ * Load/Unload
+ *
+ *
+ */
+
+ /**
+ * Load a model (ROUTER mode only).
+ * Sends POST request to `/models/load`. Note: the endpoint returns success
+ * before loading completes — use polling to await actual load status.
+ *
+ * @param modelId - Model identifier to load
+ * @param extraArgs - Optional additional arguments to pass to the model instance
+ * @returns Load response from the server
+ */
+ static async load(modelId: string, extraArgs?: string[]): Promise {
+ const payload: { model: string; extra_args?: string[] } = { model: modelId };
+ if (extraArgs && extraArgs.length > 0) {
+ payload.extra_args = extraArgs;
+ }
+
+ return apiPost(API_MODELS.LOAD, payload);
+ }
+
+ /**
+ * Unload a model (ROUTER mode only).
+ * Sends POST request to `/models/unload`. Note: the endpoint returns success
+ * before unloading completes — use polling to await actual unload status.
+ *
+ * @param modelId - Model identifier to unload
+ * @returns Unload response from the server
+ */
+ static async unload(modelId: string): Promise {
+ return apiPost(API_MODELS.UNLOAD, { model: modelId });
+ }
+
+ /**
+ *
+ *
+ * Status
+ *
+ *
+ */
+
+ /**
+ * Check if a model is loaded based on its metadata.
+ *
+ * @param model - Model data entry from the API response
+ * @returns True if the model status is LOADED
+ */
+ static isModelLoaded(model: ApiModelDataEntry): boolean {
+ return model.status.value === ServerModelStatus.LOADED;
+ }
+
+ /**
+ * Check if a model is currently loading.
+ *
+ * @param model - Model data entry from the API response
+ * @returns True if the model status is LOADING
+ */
+ static isModelLoading(model: ApiModelDataEntry): boolean {
+ return model.status.value === ServerModelStatus.LOADING;
+ }
+
+ /**
+ *
+ *
+ * Parsing
+ *
+ *
+ */
+
+ /**
+ * Parse a model ID string into its structured components.
+ *
+ * Handles conventions like:
+ * `/-(-)(-)(-):`
+ * `.` (dot-separated quantization, e.g. `model.Q4_K_M`)
+ *
+ * @param modelId - Raw model identifier string
+ * @returns Structured {@link ParsedModelId} with all detected fields
+ */
+ static parseModelId(modelId: string): ParsedModelId {
+ const result: ParsedModelId = {
+ raw: modelId,
+ orgName: null,
+ modelName: null,
+ params: null,
+ activatedParams: null,
+ quantization: null,
+ tags: []
+ };
+
+ // strip directory path and weight extension so a bare `-m /path/file.gguf`
+ // parses like a clean repo id; the HF `org/model` form is preserved
+ const source = normalizeModelName(modelId).replace(MODEL_WEIGHT_EXTENSION_RE, '');
+
+ // 1. Extract colon-separated quantization (e.g. `model:Q4_K_M`)
+ const colonIdx = source.indexOf(MODEL_ID_QUANTIZATION_SEPARATOR);
+ let modelPath: string;
+
+ if (colonIdx !== MODEL_ID_NOT_FOUND) {
+ result.quantization = source.slice(colonIdx + 1) || null;
+ modelPath = source.slice(0, colonIdx);
+ } else {
+ modelPath = source;
+ }
+
+ // 2. Extract org name (e.g. `org/model` -> org = "org")
+ const slashIdx = modelPath.indexOf(MODEL_ID_ORG_SEPARATOR);
+ let modelStr: string;
+
+ if (slashIdx !== MODEL_ID_NOT_FOUND) {
+ result.orgName = modelPath.slice(0, slashIdx);
+ modelStr = modelPath.slice(slashIdx + 1);
+ } else {
+ modelStr = modelPath;
+ }
+
+ // 3. Handle dot-separated quantization (e.g. `model-name.Q4_K_M`)
+ const dotIdx = modelStr.lastIndexOf('.');
+
+ if (dotIdx !== MODEL_ID_NOT_FOUND && !result.quantization) {
+ const afterDot = modelStr.slice(dotIdx + 1);
+
+ if (MODEL_QUANTIZATION_SEGMENT_RE.test(afterDot)) {
+ result.quantization = afterDot;
+ modelStr = modelStr.slice(0, dotIdx);
+ }
+ }
+
+ const segments = modelStr.split(MODEL_ID_SEGMENT_SEPARATOR);
+
+ // 4. Detect trailing quantization from dash-separated segments
+ // Handle UD-prefixed quantization (e.g. `UD-Q8_K_XL`) and
+ // standalone quantization (e.g. `Q4_K_M`, `BF16`, `F16`, `MXFP4`)
+ if (!result.quantization && segments.length > 1) {
+ const last = segments[segments.length - 1];
+ const secondLast = segments.length > 2 ? segments[segments.length - 2] : null;
+
+ if (MODEL_QUANTIZATION_SEGMENT_RE.test(last)) {
+ if (secondLast && MODEL_CUSTOM_QUANTIZATION_PREFIX_RE.test(secondLast)) {
+ result.quantization = `${secondLast}-${last}`;
+ segments.splice(segments.length - 2, 2);
+ } else {
+ result.quantization = last;
+ segments.pop();
+ }
+ }
+ }
+
+ // 5. Find params and activated params
+ let paramsIdx = MODEL_ID_NOT_FOUND;
+ let activatedParamsIdx = MODEL_ID_NOT_FOUND;
+
+ for (let i = 0; i < segments.length; i++) {
+ const seg = segments[i];
+
+ if (paramsIdx === MODEL_ID_NOT_FOUND && MODEL_PARAMS_RE.test(seg)) {
+ paramsIdx = i;
+ result.params = seg.toUpperCase();
+ } else if (paramsIdx !== MODEL_ID_NOT_FOUND && MODEL_ACTIVATED_PARAMS_RE.test(seg)) {
+ activatedParamsIdx = i;
+ result.activatedParams = seg.toUpperCase();
+ }
+ }
+
+ // 6. Model name = segments before params; tags = remaining segments after params
+ const pivotIdx = paramsIdx !== MODEL_ID_NOT_FOUND ? paramsIdx : segments.length;
+
+ result.modelName = segments.slice(0, pivotIdx).join(MODEL_ID_SEGMENT_SEPARATOR) || null;
+
+ if (paramsIdx !== MODEL_ID_NOT_FOUND) {
+ result.tags = segments.slice(paramsIdx + 1).filter((_, relIdx) => {
+ const absIdx = paramsIdx + 1 + relIdx;
+ if (absIdx === activatedParamsIdx) return false;
+
+ return !MODEL_IGNORED_SEGMENTS.has(segments[absIdx].toUpperCase());
+ });
+ }
+
+ return result;
+ }
+}
diff --git a/llama.cpp/tools/ui/src/lib/services/parameter-sync.service.spec.ts b/llama.cpp/tools/ui/src/lib/services/parameter-sync.service.spec.ts
new file mode 100644
index 0000000000000000000000000000000000000000..f15c0c143379372ce653a82bf98285d51fcec213
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/parameter-sync.service.spec.ts
@@ -0,0 +1,149 @@
+import { describe, it, expect } from 'vitest';
+import { ParameterSyncService } from './parameter-sync.service';
+import { ColorMode } from '$lib/enums';
+
+describe('ParameterSyncService', () => {
+ describe('roundFloatingPoint', () => {
+ it('should fix JavaScript floating-point precision issues', () => {
+ // Test the specific values from the screenshot
+ const mockServerParams = {
+ top_p: 0.949999988079071,
+ min_p: 0.009999999776482582,
+ temperature: 0.800000011920929,
+ top_k: 40,
+ samplers: ['top_k', 'typ_p', 'top_p', 'min_p', 'temperature']
+ };
+
+ const result = ParameterSyncService.extractServerDefaults({
+ ...mockServerParams,
+ // Add other required fields to match the API type
+ n_predict: 512,
+ seed: -1,
+ dynatemp_range: 0.0,
+ dynatemp_exponent: 1.0,
+ xtc_probability: 0.0,
+ xtc_threshold: 0.1,
+ typ_p: 1.0,
+ repeat_last_n: 64,
+ repeat_penalty: 1.0,
+ presence_penalty: 0.0,
+ frequency_penalty: 0.0,
+ dry_multiplier: 0.0,
+ dry_base: 1.75,
+ dry_allowed_length: 2,
+ dry_penalty_last_n: -1,
+ mirostat: 0,
+ mirostat_tau: 5.0,
+ mirostat_eta: 0.1,
+ stop: [],
+ max_tokens: -1,
+ n_keep: 0,
+ n_discard: 0,
+ ignore_eos: false,
+ stream: true,
+ logit_bias: [],
+ n_probs: 0,
+ min_keep: 0,
+ grammar: '',
+ grammar_lazy: false,
+ grammar_triggers: [],
+ preserved_tokens: [],
+ chat_format: '',
+ reasoning_format: '',
+ reasoning_in_content: false,
+ generation_prompt: '',
+ 'speculative.n_max': 0,
+ 'speculative.n_min': 0,
+ 'speculative.p_min': 0.0,
+ timings_per_token: false,
+ post_sampling_probs: false,
+ lora: [],
+ top_n_sigma: 0.0,
+ dry_sequence_breakers: []
+ } as ApiLlamaCppServerProps['default_generation_settings']['params']);
+
+ // Check that the problematic floating-point values are rounded correctly
+ expect(result.top_p).toBe(0.95);
+ expect(result.min_p).toBe(0.01);
+ expect(result.temperature).toBe(0.8);
+ expect(result.top_k).toBe(40); // Integer should remain unchanged
+ expect(result.samplers).toBe('top_k;typ_p;top_p;min_p;temperature');
+ });
+
+ it('should preserve non-numeric values', () => {
+ const mockServerParams = {
+ samplers: ['top_k', 'temperature'],
+ max_tokens: -1,
+ temperature: 0.7
+ };
+
+ const result = ParameterSyncService.extractServerDefaults({
+ ...mockServerParams,
+ // Minimal required fields
+ n_predict: 512,
+ seed: -1,
+ dynatemp_range: 0.0,
+ dynatemp_exponent: 1.0,
+ top_k: 40,
+ top_p: 0.95,
+ min_p: 0.05,
+ xtc_probability: 0.0,
+ xtc_threshold: 0.1,
+ typ_p: 1.0,
+ repeat_last_n: 64,
+ repeat_penalty: 1.0,
+ presence_penalty: 0.0,
+ frequency_penalty: 0.0,
+ dry_multiplier: 0.0,
+ dry_base: 1.75,
+ dry_allowed_length: 2,
+ dry_penalty_last_n: -1,
+ mirostat: 0,
+ mirostat_tau: 5.0,
+ mirostat_eta: 0.1,
+ stop: [],
+ n_keep: 0,
+ n_discard: 0,
+ ignore_eos: false,
+ stream: true,
+ logit_bias: [],
+ n_probs: 0,
+ min_keep: 0,
+ grammar: '',
+ grammar_lazy: false,
+ grammar_triggers: [],
+ preserved_tokens: [],
+ chat_format: '',
+ reasoning_format: '',
+ reasoning_in_content: false,
+ generation_prompt: '',
+ 'speculative.n_max': 0,
+ 'speculative.n_min': 0,
+ 'speculative.p_min': 0.0,
+ timings_per_token: false,
+ post_sampling_probs: false,
+ lora: [],
+ top_n_sigma: 0.0,
+ dry_sequence_breakers: []
+ } as ApiLlamaCppServerProps['default_generation_settings']['params']);
+
+ expect(result.samplers).toBe('top_k;temperature');
+ expect(result.max_tokens).toBe(-1);
+ expect(result.temperature).toBe(0.7);
+ });
+
+ it('should merge ui settings from props when provided', () => {
+ const result = ParameterSyncService.extractServerDefaults(null, {
+ pasteLongTextToFileLen: 0,
+ pdfAsImage: true,
+ renderUserContentAsMarkdown: false,
+ theme: ColorMode.DARK
+ });
+
+ expect(result.pasteLongTextToFileLen).toBe(0);
+ expect(result.pdfAsImage).toBe(true);
+ expect(result.renderUserContentAsMarkdown).toBe(false);
+ expect(result.theme).toBeUndefined();
+ });
+ });
+});
diff --git a/llama.cpp/tools/ui/src/lib/services/parameter-sync.service.ts b/llama.cpp/tools/ui/src/lib/services/parameter-sync.service.ts
new file mode 100644
index 0000000000000000000000000000000000000000..900471b3aa9e1a360c1ff80ed3f394eeb51d897f
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/parameter-sync.service.ts
@@ -0,0 +1,232 @@
+import { normalizeFloatingPoint } from '$lib/utils';
+import { SETTINGS_KEYS, SYNCABLE_PARAMETERS } from '$lib/constants';
+import type { ParameterRecord, ParameterInfo, ParameterValue } from '$lib/types';
+import { SyncableParameterType, ParameterSource } from '$lib/enums';
+
+export class ParameterSyncService {
+ /**
+ *
+ *
+ * Extraction
+ *
+ *
+ */
+
+ /**
+ * Round floating-point numbers to avoid JavaScript precision issues.
+ * E.g., 0.1 + 0.2 = 0.30000000000000004 → 0.3
+ *
+ * @param value - Parameter value to normalize
+ * @returns Precision-normalized value
+ */
+ private static roundFloatingPoint(value: ParameterValue): ParameterValue {
+ return normalizeFloatingPoint(value) as ParameterValue;
+ }
+
+ /**
+ * Extract server default parameters that can be synced from `/props` response.
+ * Handles both generation settings parameters and UI-specific settings.
+ * Converts samplers array to semicolon-delimited string for UI display.
+ *
+ * @param serverParams - Raw generation settings from server `/props` endpoint
+ * @param uiSettings - Optional UI-specific settings from server
+ * @returns Record of extracted parameter key-value pairs with normalized precision
+ */
+ static extractServerDefaults(
+ serverParams: ApiLlamaCppServerProps['default_generation_settings']['params'] | null,
+ uiSettings?: Record
+ ): ParameterRecord {
+ const extracted: ParameterRecord = {};
+
+ if (serverParams) {
+ for (const param of SYNCABLE_PARAMETERS) {
+ if (param.canSync && param.serverKey in serverParams) {
+ const value = (serverParams as unknown as Record)[
+ param.serverKey
+ ];
+ if (value !== undefined) {
+ // Apply precision rounding to avoid JavaScript floating-point issues
+ extracted[param.key] = this.roundFloatingPoint(value);
+ }
+ }
+ }
+
+ // Handle samplers array conversion to string
+ if (serverParams.samplers && Array.isArray(serverParams.samplers)) {
+ extracted[SETTINGS_KEYS.SAMPLERS] = serverParams.samplers.join(';');
+ }
+ }
+
+ if (uiSettings) {
+ for (const param of SYNCABLE_PARAMETERS) {
+ if (param.canSync && param.serverKey in uiSettings) {
+ const value = uiSettings[param.serverKey];
+
+ if (value !== undefined) {
+ extracted[param.key] = this.roundFloatingPoint(value);
+ }
+ }
+ }
+ }
+
+ return extracted;
+ }
+
+ /**
+ *
+ *
+ * Merging
+ *
+ *
+ */
+
+ /**
+ * Merge server defaults with current user settings.
+ * User overrides always take priority — only parameters not in `userOverrides`
+ * set will be updated from server defaults.
+ *
+ * @param currentSettings - Current parameter values in the settings store
+ * @param serverDefaults - Default values extracted from server props
+ * @param userOverrides - Set of parameter keys explicitly overridden by the user
+ * @returns Merged parameter record with user overrides preserved
+ */
+ static mergeWithServerDefaults(
+ currentSettings: ParameterRecord,
+ serverDefaults: ParameterRecord,
+ userOverrides: Set = new Set()
+ ): ParameterRecord {
+ const merged = { ...currentSettings };
+
+ for (const [key, serverValue] of Object.entries(serverDefaults)) {
+ // Only update if user hasn't explicitly overridden this parameter
+ if (!userOverrides.has(key)) {
+ merged[key] = this.roundFloatingPoint(serverValue);
+ }
+ }
+
+ return merged;
+ }
+
+ /**
+ *
+ *
+ * Info
+ *
+ *
+ */
+
+ /**
+ * Get parameter information including source and values.
+ * Used by SettingsChatParameterSourceIndicator to display the correct badge
+ * (Custom vs Default) for each parameter in the settings UI.
+ *
+ * @param key - The parameter key to get info for
+ * @param currentValue - The current value of the parameter
+ * @param propsDefaults - Server default values from `/props`
+ * @param userOverrides - Set of parameter keys explicitly overridden by the user
+ * @returns Parameter info with source, server default, and user override values
+ */
+ static getParameterInfo(
+ key: string,
+ currentValue: ParameterValue,
+ propsDefaults: ParameterRecord,
+ userOverrides: Set
+ ): ParameterInfo {
+ const hasPropsDefault = propsDefaults[key] !== undefined;
+ const isUserOverride = userOverrides.has(key);
+
+ // Simple logic: either using default (from props) or custom (user override)
+ const source = isUserOverride ? ParameterSource.CUSTOM : ParameterSource.DEFAULT;
+
+ return {
+ value: currentValue,
+ source,
+ serverDefault: hasPropsDefault ? propsDefaults[key] : undefined, // Keep same field name for compatibility
+ userOverride: isUserOverride ? currentValue : undefined
+ };
+ }
+
+ /**
+ * Check if a parameter can be synced from server.
+ *
+ * @param key - The parameter key to check
+ * @returns True if the parameter is in the syncable parameters list
+ */
+ static canSyncParameter(key: string): boolean {
+ return SYNCABLE_PARAMETERS.some((param) => param.key === key && param.canSync);
+ }
+
+ /**
+ * Get all syncable parameter keys.
+ *
+ * @returns Array of parameter keys that can be synced from server
+ */
+ static getSyncableParameterKeys(): string[] {
+ return SYNCABLE_PARAMETERS.filter((param) => param.canSync).map((param) => param.key);
+ }
+
+ /**
+ * Validate a server parameter value against its expected type.
+ *
+ * @param key - The parameter key to validate
+ * @param value - The value to validate
+ * @returns True if value matches the expected type for this parameter
+ */
+ static validateServerParameter(key: string, value: ParameterValue): boolean {
+ const param = SYNCABLE_PARAMETERS.find((p) => p.key === key);
+ if (!param) return false;
+
+ switch (param.type) {
+ case SyncableParameterType.NUMBER:
+ return typeof value === 'number' && !isNaN(value);
+ case SyncableParameterType.STRING:
+ return typeof value === 'string';
+ case SyncableParameterType.BOOLEAN:
+ return typeof value === 'boolean';
+ default:
+ return false;
+ }
+ }
+
+ /**
+ *
+ *
+ * Diff
+ *
+ *
+ */
+
+ /**
+ * Create a diff between current settings and server defaults.
+ * Shows which parameters differ from server values, useful for debugging
+ * and for the "Reset to defaults" functionality.
+ *
+ * @param currentSettings - Current parameter values in the settings store
+ * @param serverDefaults - Default values extracted from server props
+ * @returns Record of parameter diffs with current value, server value, and whether they differ
+ */
+ static createParameterDiff(
+ currentSettings: ParameterRecord,
+ serverDefaults: ParameterRecord
+ ): Record {
+ const diff: Record<
+ string,
+ { current: ParameterValue; server: ParameterValue; differs: boolean }
+ > = {};
+
+ for (const key of this.getSyncableParameterKeys()) {
+ const currentValue = currentSettings[key];
+ const serverValue = serverDefaults[key];
+
+ if (serverValue !== undefined) {
+ diff[key] = {
+ current: currentValue,
+ server: serverValue,
+ differs: currentValue !== serverValue
+ };
+ }
+ }
+
+ return diff;
+ }
+}
diff --git a/llama.cpp/tools/ui/src/lib/services/props.service.ts b/llama.cpp/tools/ui/src/lib/services/props.service.ts
new file mode 100644
index 0000000000000000000000000000000000000000..45c3e457732c1b7bb17f36f3c9583aee22b5eb47
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/props.service.ts
@@ -0,0 +1,47 @@
+import { apiFetchWithParams } from '$lib/utils';
+
+export class PropsService {
+ /**
+ *
+ *
+ * Fetching
+ *
+ *
+ */
+
+ /**
+ * Fetches global server properties from the `/props` endpoint.
+ * In MODEL mode, returns modalities for the single loaded model.
+ * In ROUTER mode, returns server-wide settings without model-specific modalities.
+ *
+ * @param autoload - If false, prevents automatic model loading (default: false)
+ * @returns Server properties including default generation settings and capabilities
+ * @throws {Error} If the request fails or returns invalid data
+ */
+ static async fetch(autoload = false): Promise {
+ const params: Record = {};
+ if (!autoload) {
+ params.autoload = 'false';
+ }
+
+ return apiFetchWithParams('./props', params, { authOnly: true });
+ }
+
+ /**
+ * Fetches server properties for a specific model (ROUTER mode only).
+ * Required in ROUTER mode because global `/props` does not include per-model modalities.
+ *
+ * @param modelId - The model ID to fetch properties for
+ * @param autoload - If false, prevents automatic model loading (default: false)
+ * @returns Server properties specific to the requested model
+ * @throws {Error} If the request fails, model not found, or model not loaded
+ */
+ static async fetchForModel(modelId: string, autoload = false): Promise {
+ const params: Record = { model: modelId };
+ if (!autoload) {
+ params.autoload = 'false';
+ }
+
+ return apiFetchWithParams('./props', params, { authOnly: true });
+ }
+}
diff --git a/llama.cpp/tools/ui/src/lib/services/router.service.ts b/llama.cpp/tools/ui/src/lib/services/router.service.ts
new file mode 100644
index 0000000000000000000000000000000000000000..6fa172eec23428de10c9adc5ba33772630f512a3
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/router.service.ts
@@ -0,0 +1,11 @@
+import { ROUTES } from '$lib/constants/routes';
+
+export class RouterService {
+ static chat(id: string): string {
+ return `${ROUTES.CHAT}/${id}`;
+ }
+
+ static settings(section: string): string {
+ return `${ROUTES.SETTINGS}/${section}`;
+ }
+}
diff --git a/llama.cpp/tools/ui/src/lib/services/sandbox-harness.ts b/llama.cpp/tools/ui/src/lib/services/sandbox-harness.ts
new file mode 100644
index 0000000000000000000000000000000000000000..27b05e24b4b4a33416328dfa79b2fac88b47cab8
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/sandbox-harness.ts
@@ -0,0 +1,25 @@
+import WORKER_SHIM from './sandbox-worker.js?raw';
+
+/**
+ * Harness loaded as srcdoc into a sandboxed iframe (allow-scripts only).
+ * The opaque origin is the security boundary: no access to the app origin,
+ * its storage or its API. The harness spawns a worker so model code never
+ * runs on a main thread, which makes the parent timeout enforceable by
+ * removing the iframe.
+ */
+export const SANDBOX_HARNESS_HTML = ``;
diff --git a/llama.cpp/tools/ui/src/lib/services/sandbox-worker.js b/llama.cpp/tools/ui/src/lib/services/sandbox-worker.js
new file mode 100644
index 0000000000000000000000000000000000000000..689b9211eb5403749353eeaceab8fcf4d94746e1
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/sandbox-worker.js
@@ -0,0 +1,30 @@
+const logs = [];
+const fmt = (value) => {
+ if (typeof value === 'string') return value;
+ try {
+ return JSON.stringify(value);
+ } catch {
+ return String(value);
+ }
+};
+const capture =
+ (level, prefix) =>
+ (...args) => {
+ logs.push(prefix + args.map(fmt).join(' '));
+ };
+console.log = capture('log', '');
+console.info = capture('info', '');
+console.debug = capture('debug', '');
+console.warn = capture('warn', 'warn: ');
+console.error = capture('error', 'error: ');
+self.onmessage = async (event) => {
+ const reply = { logs, result: null, error: null };
+ try {
+ const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
+ const value = await new AsyncFunction(event.data.code)();
+ if (value !== undefined) reply.result = fmt(value);
+ } catch (err) {
+ reply.error = err instanceof Error ? err.stack || err.message : String(err);
+ }
+ self.postMessage(reply);
+};
diff --git a/llama.cpp/tools/ui/src/lib/services/sandbox.service.ts b/llama.cpp/tools/ui/src/lib/services/sandbox.service.ts
new file mode 100644
index 0000000000000000000000000000000000000000..ef0e6cf4eb445ca0c15cee1011568df993526de7
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/sandbox.service.ts
@@ -0,0 +1,112 @@
+import {
+ NEWLINE_SEPARATOR,
+ SANDBOX_EMPTY_OUTPUT,
+ SANDBOX_OUTPUT_MAX_CHARS,
+ SANDBOX_TIMEOUT_MS_DEFAULT,
+ SANDBOX_TIMEOUT_MS_MAX,
+ SANDBOX_TOOL_NAME,
+ SANDBOX_TRUNCATION_NOTICE
+} from '$lib/constants';
+import { SANDBOX_HARNESS_HTML } from './sandbox-harness';
+import type { ToolExecutionResult } from '$lib/types';
+
+interface SandboxReply {
+ logs?: unknown;
+ result?: unknown;
+ error?: unknown;
+}
+
+function formatReply(reply: SandboxReply): ToolExecutionResult {
+ const lines: string[] = [];
+
+ if (Array.isArray(reply.logs)) {
+ for (const line of reply.logs) lines.push(String(line));
+ }
+
+ if (reply.error != null) {
+ lines.push(`Error: ${String(reply.error)}`);
+ } else if (reply.result != null) {
+ lines.push(`=> ${String(reply.result)}`);
+ }
+
+ let content = lines.join(NEWLINE_SEPARATOR);
+ if (!content) content = SANDBOX_EMPTY_OUTPUT;
+ if (content.length > SANDBOX_OUTPUT_MAX_CHARS) {
+ content = `${content.slice(0, SANDBOX_OUTPUT_MAX_CHARS)}${NEWLINE_SEPARATOR}${SANDBOX_TRUNCATION_NOTICE}`;
+ }
+
+ return { content, isError: reply.error != null };
+}
+
+export class SandboxService {
+ /**
+ * Execute a frontend sandbox tool call and return its output.
+ * One disposable iframe per execution, removed on completion,
+ * timeout or abort. Removing the iframe terminates the worker
+ * at the browser level, so runaway code cannot outlive it.
+ */
+ static executeTool(
+ toolName: string,
+ params: Record,
+ signal?: AbortSignal
+ ): Promise {
+ if (toolName !== SANDBOX_TOOL_NAME) {
+ return Promise.resolve({ content: `Unknown frontend tool: ${toolName}`, isError: true });
+ }
+
+ const code = typeof params.code === 'string' ? params.code : '';
+ if (!code) {
+ return Promise.resolve({ content: 'Missing required parameter: code', isError: true });
+ }
+
+ const requested = Number(params.timeout_ms);
+ const timeoutMs =
+ Number.isFinite(requested) && requested > 0
+ ? Math.min(requested, SANDBOX_TIMEOUT_MS_MAX)
+ : SANDBOX_TIMEOUT_MS_DEFAULT;
+
+ return new Promise((resolve, reject) => {
+ const iframe = document.createElement('iframe');
+ iframe.setAttribute('sandbox', 'allow-scripts');
+ iframe.style.display = 'none';
+ iframe.srcdoc = SANDBOX_HARNESS_HTML;
+
+ let settled = false;
+
+ const cleanup = () => {
+ settled = true;
+ clearTimeout(timer);
+ window.removeEventListener('message', onMessage);
+ signal?.removeEventListener('abort', onAbort);
+ iframe.remove();
+ };
+
+ const finish = (result: ToolExecutionResult) => {
+ if (settled) return;
+ cleanup();
+ resolve(result);
+ };
+
+ const onAbort = () => {
+ if (settled) return;
+ cleanup();
+ reject(new DOMException('Sandbox execution aborted', 'AbortError'));
+ };
+
+ const onMessage = (event: MessageEvent) => {
+ if (event.source !== iframe.contentWindow) return;
+ finish(formatReply((event.data ?? {}) as SandboxReply));
+ };
+
+ const timer = setTimeout(
+ () => finish({ content: `Execution timed out after ${timeoutMs} ms`, isError: true }),
+ timeoutMs
+ );
+
+ window.addEventListener('message', onMessage);
+ signal?.addEventListener('abort', onAbort);
+ iframe.onload = () => iframe.contentWindow?.postMessage({ code }, '*');
+ document.body.appendChild(iframe);
+ });
+ }
+}
diff --git a/llama.cpp/tools/ui/src/lib/services/tools.service.ts b/llama.cpp/tools/ui/src/lib/services/tools.service.ts
new file mode 100644
index 0000000000000000000000000000000000000000..8f39f5209f8e6552677e9172de933eb5ae6b6dab
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/services/tools.service.ts
@@ -0,0 +1,40 @@
+import { apiFetch } from '$lib/utils';
+import { API_TOOLS } from '$lib/constants';
+import { ToolResponseField } from '$lib/enums';
+import type { ToolExecutionResult, ServerBuiltinToolInfo } from '$lib/types';
+
+export class ToolsService {
+ /**
+ * Fetch the list of built-in tools from the server.
+ *
+ * @returns Array of tool definitions in OpenAI-compatible format
+ */
+ static async list(): Promise {
+ return apiFetch(API_TOOLS.LIST);
+ }
+
+ /**
+ * Execute a built-in tool on the server.
+ */
+ static async executeTool(
+ toolName: string,
+ params: Record,
+ signal?: AbortSignal
+ ): Promise {
+ const result = await apiFetch>(API_TOOLS.EXECUTE, {
+ method: 'POST',
+ body: JSON.stringify({ tool: toolName, params }),
+ signal
+ });
+
+ if (ToolResponseField.ERROR in result) {
+ return { content: String(result[ToolResponseField.ERROR]), isError: true };
+ }
+
+ if (ToolResponseField.PLAIN_TEXT in result) {
+ return { content: String(result[ToolResponseField.PLAIN_TEXT]), isError: false };
+ }
+
+ return { content: JSON.stringify(result), isError: false };
+ }
+}
diff --git a/llama.cpp/tools/ui/src/lib/stores/agentic.svelte.ts b/llama.cpp/tools/ui/src/lib/stores/agentic.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..27491257ac7295d0649d1edba68f54360e9415af
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/stores/agentic.svelte.ts
@@ -0,0 +1,1037 @@
+/**
+ * agenticStore - Reactive State Store for Agentic Loop Orchestration
+ *
+ * Manages multi-turn agentic loop with MCP tools:
+ * - LLM streaming with tool call detection
+ * - Tool execution via mcpStore
+ * - Session state management
+ * - Turn limit enforcement
+ *
+ * Each agentic turn produces separate DB messages:
+ * - One assistant message per LLM turn (with tool_calls if any)
+ * - One tool result message per tool call execution
+ *
+ * **Architecture & Relationships:**
+ * - **ChatService**: Stateless API layer (sendMessage, streaming)
+ * - **mcpStore**: MCP connection management and tool execution
+ * - **agenticStore** (this): Reactive state + business logic
+ *
+ * @see ChatService in services/chat.service.ts for API operations
+ * @see mcpStore in stores/mcp.svelte.ts for MCP operations
+ */
+
+import { ChatService } from '$lib/services';
+import { config } from '$lib/stores/settings.svelte';
+import { mcpStore } from '$lib/stores/mcp.svelte';
+import { modelsStore } from '$lib/stores/models.svelte';
+import { toolsStore } from '$lib/stores/tools.svelte';
+import { permissionsStore } from '$lib/stores/permissions.svelte';
+import { ToolSource, ToolPermissionDecision } from '$lib/enums';
+import { SvelteMap } from 'svelte/reactivity';
+import { ToolsService } from '$lib/services/tools.service';
+import { SandboxService } from '$lib/services/sandbox.service';
+import { isAbortError } from '$lib/utils';
+import { DEFAULT_AGENTIC_CONFIG, NEWLINE_SEPARATOR } from '$lib/constants';
+import {
+ IMAGE_MIME_TO_EXTENSION,
+ DATA_URI_BASE64_REGEX,
+ MCP_ATTACHMENT_NAME_PREFIX,
+ DEFAULT_IMAGE_EXTENSION
+} from '$lib/constants';
+import {
+ AttachmentType,
+ ContentPartType,
+ MessageRole,
+ MimeTypePrefix,
+ ToolCallType
+} from '$lib/enums';
+import type {
+ AgenticFlowParams,
+ AgenticFlowResult,
+ AgenticSession,
+ AgenticConfig,
+ SettingsConfigType,
+ McpServerOverride,
+ MCPToolCall
+} from '$lib/types';
+import type {
+ AgenticMessage,
+ AgenticToolCallList,
+ AgenticFlowCallbacks,
+ AgenticFlowOptions,
+ SteeringMessage
+} from '$lib/types/agentic';
+import type {
+ ApiChatCompletionToolCall,
+ ApiChatMessageData,
+ ApiChatMessageContentPart
+} from '$lib/types/api';
+import type {
+ ChatMessagePromptProgress,
+ ChatMessageTimings,
+ ChatMessageAgenticTimings,
+ ChatMessageToolCallTiming,
+ ChatMessageAgenticTurnStats
+} from '$lib/types/chat';
+import type {
+ DatabaseMessage,
+ DatabaseMessageExtra,
+ DatabaseMessageExtraImageFile
+} from '$lib/types/database';
+
+function createDefaultSession(): AgenticSession {
+ return {
+ isRunning: false,
+ currentTurn: 0,
+ totalToolCalls: 0,
+ lastError: null,
+ streamingToolCall: null,
+ pendingPermissionRequest: null
+ };
+}
+
+function toAgenticMessages(messages: ApiChatMessageData[]): AgenticMessage[] {
+ return messages.map((message) => {
+ if (
+ message.role === MessageRole.ASSISTANT &&
+ message.tool_calls &&
+ message.tool_calls.length > 0
+ ) {
+ return {
+ role: MessageRole.ASSISTANT,
+ content: message.content,
+ reasoning_content: message.reasoning_content,
+ tool_calls: message.tool_calls.map((call, index) => ({
+ id: call.id ?? `call_${index}`,
+ type: (call.type as ToolCallType.FUNCTION) ?? ToolCallType.FUNCTION,
+ function: {
+ name: call.function?.name ?? '',
+ arguments: call.function?.arguments ?? ''
+ }
+ }))
+ } satisfies AgenticMessage;
+ }
+ if (message.role === MessageRole.ASSISTANT) {
+ return {
+ role: MessageRole.ASSISTANT,
+ content: message.content,
+ reasoning_content: message.reasoning_content
+ } satisfies AgenticMessage;
+ }
+ if (message.role === MessageRole.TOOL && message.tool_call_id) {
+ return {
+ role: MessageRole.TOOL,
+ tool_call_id: message.tool_call_id,
+ content: typeof message.content === 'string' ? message.content : ''
+ } satisfies AgenticMessage;
+ }
+ return {
+ role: message.role as MessageRole.SYSTEM | MessageRole.USER,
+ content: message.content
+ } satisfies AgenticMessage;
+ });
+}
+
+class AgenticStore {
+ private _sessions = new SvelteMap();
+ /** Dedicated reactive state for pending permission requests (ensures immediate UI updates) */
+ private _pendingPermissions = new SvelteMap<
+ string,
+ { toolName: string; serverLabel: string } | null
+ >();
+ /** Non-reactive: stores resolve functions for pending permission Promises */
+ private _permissionResolvers = new Map void>();
+
+ /** Dedicated reactive state for pending continue requests (turn limit reached) */
+ private _pendingContinueRequests = new SvelteMap();
+ /** Non-reactive: stores resolve functions for pending continue Promises */
+ private _continueResolvers = new Map void>();
+
+ /** Reactive: queued steering messages to inject between turns */
+ private _steeringMessages = new SvelteMap();
+
+ get isReady(): boolean {
+ return true;
+ }
+ get isAnyRunning(): boolean {
+ for (const session of this._sessions.values()) {
+ if (session.isRunning) return true;
+ }
+ return false;
+ }
+
+ getSession(conversationId: string): AgenticSession {
+ let session = this._sessions.get(conversationId);
+ if (!session) {
+ session = createDefaultSession();
+ this._sessions.set(conversationId, session);
+ }
+ return session;
+ }
+
+ private updateSession(conversationId: string, update: Partial): void {
+ const session = this.getSession(conversationId);
+ this._sessions.set(conversationId, { ...session, ...update });
+ }
+
+ clearSession(conversationId: string): void {
+ this._sessions.delete(conversationId);
+ }
+
+ getActiveSessions(): Array<{ conversationId: string; session: AgenticSession }> {
+ const active: Array<{ conversationId: string; session: AgenticSession }> = [];
+ for (const [conversationId, session] of this._sessions.entries()) {
+ if (session.isRunning) active.push({ conversationId, session });
+ }
+ return active;
+ }
+
+ isRunning(conversationId: string): boolean {
+ return this.getSession(conversationId).isRunning;
+ }
+
+ currentTurn(conversationId: string): number {
+ return this.getSession(conversationId).currentTurn;
+ }
+
+ totalToolCalls(conversationId: string): number {
+ return this.getSession(conversationId).totalToolCalls;
+ }
+
+ lastError(conversationId: string): Error | null {
+ return this.getSession(conversationId).lastError;
+ }
+
+ streamingToolCall(conversationId: string): { name: string; arguments: string } | null {
+ return this.getSession(conversationId).streamingToolCall;
+ }
+
+ pendingPermissionRequest(
+ conversationId: string
+ ): { toolName: string; serverLabel: string } | null {
+ return this._pendingPermissions.get(conversationId) ?? null;
+ }
+
+ pendingContinueRequest(conversationId: string): boolean {
+ return this._pendingContinueRequests.get(conversationId) ?? false;
+ }
+
+ resolveContinue(conversationId: string, shouldContinue: boolean): void {
+ const resolver = this._continueResolvers.get(conversationId);
+ if (resolver) {
+ this._continueResolvers.delete(conversationId);
+ resolver(shouldContinue);
+ }
+ }
+
+ resolvePermission(conversationId: string, decision: ToolPermissionDecision): void {
+ const resolver = this._permissionResolvers.get(conversationId);
+ if (resolver) {
+ this._permissionResolvers.delete(conversationId);
+ resolver(decision);
+ }
+ }
+
+ clearError(conversationId: string): void {
+ this.updateSession(conversationId, { lastError: null });
+ }
+
+ hasPendingSteeringMessage(conversationId: string): boolean {
+ return this._steeringMessages.has(conversationId);
+ }
+
+ pendingSteeringMessageContent(conversationId: string): string | null {
+ return this._steeringMessages.get(conversationId)?.content ?? null;
+ }
+
+ pendingSteeringMessageExtras(conversationId: string): DatabaseMessageExtra[] | undefined {
+ return this._steeringMessages.get(conversationId)?.extras;
+ }
+
+ /**
+ * Queue a steering message. When the current agentic turn completes,
+ * the flow exits and the caller re-sends the message as a normal chat message.
+ */
+ injectSteeringMessage(
+ conversationId: string,
+ content: string,
+ extras?: DatabaseMessageExtra[]
+ ): void {
+ this._steeringMessages.set(conversationId, { content, extras });
+ }
+
+ /**
+ * Clear the pending steering message without consuming it.
+ */
+ clearSteeringMessage(conversationId: string): void {
+ this._steeringMessages.delete(conversationId);
+ }
+
+ /**
+ * Consume and return the pending steering message for re-sending.
+ * Called by chatStore after the agentic flow exits.
+ */
+ consumePendingSteeringMessage(conversationId: string): SteeringMessage | null {
+ const msg = this._steeringMessages.get(conversationId);
+ if (!msg) return null;
+ this._steeringMessages.delete(conversationId);
+ return msg;
+ }
+
+ getConfig(settings: SettingsConfigType, perChatOverrides?: McpServerOverride[]): AgenticConfig {
+ const maxTurns = Number(settings.agenticMaxTurns) || DEFAULT_AGENTIC_CONFIG.maxTurns;
+ const maxToolPreviewLines =
+ Number(settings.agenticMaxToolPreviewLines) || DEFAULT_AGENTIC_CONFIG.maxToolPreviewLines;
+ const hasTools =
+ mcpStore.hasEnabledServers(perChatOverrides) ||
+ toolsStore.builtinTools.length > 0 ||
+ toolsStore.customTools.length > 0;
+ return {
+ enabled: hasTools && DEFAULT_AGENTIC_CONFIG.enabled,
+ maxTurns,
+ maxToolPreviewLines
+ };
+ }
+
+ private parseToolArguments(args: string | Record): Record {
+ if (typeof args === 'object') return args;
+ const trimmed = args.trim();
+ if (trimmed === '') return {};
+ return JSON.parse(trimmed) as Record;
+ }
+
+ private async requestPermission(
+ conversationId: string,
+ toolName: string,
+ serverLabel: string,
+ signal?: AbortSignal
+ ): Promise {
+ const permissionKey = toolsStore.getPermissionKey(toolName);
+ if (permissionKey && permissionsStore.hasTool(permissionKey)) {
+ return ToolPermissionDecision.ONCE;
+ }
+
+ this._pendingPermissions.set(conversationId, { toolName, serverLabel });
+
+ return new Promise((resolve) => {
+ if (signal?.aborted) {
+ this._pendingPermissions.set(conversationId, null);
+ resolve(ToolPermissionDecision.DENY);
+ return;
+ }
+
+ this._permissionResolvers.set(conversationId, (decision) => {
+ this._pendingPermissions.set(conversationId, null);
+ if (decision === ToolPermissionDecision.ALWAYS && permissionKey) {
+ permissionsStore.allowTool(permissionKey);
+ } else if (decision === ToolPermissionDecision.ALWAYS_SERVER) {
+ const serverToolKeys = toolsStore.allTools
+ .filter((t) =>
+ t.serverName
+ ? t.serverName === serverLabel
+ : toolsStore.getToolServerLabel(t.definition.function.name) === serverLabel
+ )
+ .map((t) => toolsStore.getPermissionKey(t.definition.function.name)!)
+ .filter((k): k is string => k !== null);
+ permissionsStore.allowTools(serverToolKeys);
+ }
+ resolve(decision);
+ });
+
+ signal?.addEventListener(
+ 'abort',
+ () => {
+ const resolver = this._permissionResolvers.get(conversationId);
+ if (resolver) {
+ this._permissionResolvers.delete(conversationId);
+ this._pendingPermissions.set(conversationId, null);
+ resolve(ToolPermissionDecision.DENY);
+ }
+ },
+ { once: true }
+ );
+ });
+ }
+
+ private async requestContinue(conversationId: string, signal?: AbortSignal): Promise {
+ this._pendingContinueRequests.set(conversationId, true);
+
+ return new Promise((resolve) => {
+ if (signal?.aborted) {
+ this._pendingContinueRequests.set(conversationId, false);
+ resolve(false);
+ return;
+ }
+
+ this._continueResolvers.set(conversationId, (shouldContinue) => {
+ this._pendingContinueRequests.set(conversationId, false);
+ resolve(shouldContinue);
+ });
+
+ signal?.addEventListener(
+ 'abort',
+ () => {
+ const resolver = this._continueResolvers.get(conversationId);
+ if (resolver) {
+ this._continueResolvers.delete(conversationId);
+ this._pendingContinueRequests.set(conversationId, false);
+ resolve(false);
+ }
+ },
+ { once: true }
+ );
+ });
+ }
+
+ async runAgenticFlow(params: AgenticFlowParams): Promise {
+ const { conversationId, messages, options = {}, callbacks, signal, perChatOverrides } = params;
+
+ // Clear any pending permissions/continue requests for this conversation when starting a new flow
+ this._pendingPermissions.set(conversationId, null);
+ this._permissionResolvers.delete(conversationId);
+ this._pendingContinueRequests.set(conversationId, false);
+ this._continueResolvers.delete(conversationId);
+ this._steeringMessages.delete(conversationId);
+
+ // Ensure built-in tools are fetched before checking if agentic is enabled
+ if (toolsStore.builtinTools.length === 0 && !toolsStore.loading) {
+ await toolsStore.fetchBuiltinTools();
+ }
+
+ const agenticConfig = this.getConfig(config(), perChatOverrides);
+ if (!agenticConfig.enabled) return { handled: false };
+
+ const hasMcpServers = mcpStore.hasEnabledServers(perChatOverrides);
+ if (hasMcpServers) {
+ const initialized = await mcpStore.ensureInitialized(perChatOverrides);
+
+ if (!initialized) {
+ console.log('[AgenticStore] MCP not initialized');
+ }
+ }
+
+ const tools = toolsStore.getEnabledToolsForLLM();
+ if (tools.length === 0) {
+ return { handled: false };
+ }
+
+ console.log(`[AgenticStore] Starting agentic flow with ${tools.length} tools`);
+
+ const normalizedMessages: ApiChatMessageData[] = (
+ await Promise.all(
+ messages.map((msg) => {
+ if ('id' in msg && 'convId' in msg && 'timestamp' in msg)
+ return ChatService.convertDbMessageToApiChatMessageData(
+ msg as DatabaseMessage & { extra?: DatabaseMessageExtra[] }
+ );
+ return msg as ApiChatMessageData;
+ })
+ )
+ ).filter((msg: { role: ChatRole; content: string | ApiChatMessageContentPart[] }) => {
+ if (msg.role === MessageRole.SYSTEM) {
+ const content = typeof msg.content === 'string' ? msg.content : '';
+ return content.trim().length > 0;
+ }
+ return true;
+ });
+
+ this.updateSession(conversationId, {
+ isRunning: true,
+ currentTurn: 0,
+ totalToolCalls: 0,
+ lastError: null
+ });
+
+ if (hasMcpServers) mcpStore.acquireConnection();
+
+ try {
+ await this.executeAgenticLoop({
+ conversationId,
+ messages: normalizedMessages,
+ options,
+ tools,
+ agenticConfig,
+ callbacks,
+ signal
+ });
+ return { handled: true };
+ } catch (error) {
+ const normalizedError = error instanceof Error ? error : new Error(String(error));
+ this.updateSession(conversationId, { lastError: normalizedError });
+ callbacks.onError?.(normalizedError);
+ return { handled: true, error: normalizedError };
+ } finally {
+ this.updateSession(conversationId, { isRunning: false });
+
+ if (hasMcpServers) {
+ await mcpStore
+ .releaseConnection()
+ .catch((err: unknown) =>
+ console.warn('[AgenticStore] Failed to release MCP connection:', err)
+ );
+ }
+ }
+ }
+
+ private async executeAgenticLoop(params: {
+ conversationId: string;
+ messages: ApiChatMessageData[];
+ options: AgenticFlowOptions;
+ tools: ReturnType;
+ agenticConfig: AgenticConfig;
+ callbacks: AgenticFlowCallbacks;
+ signal?: AbortSignal;
+ }): Promise {
+ const { conversationId, messages, options, tools, agenticConfig, callbacks, signal } = params;
+ const {
+ onChunk,
+ onReasoningChunk,
+ onToolCallsStreaming,
+ onAttachments,
+ onModel,
+ onCompletionId,
+ onAssistantTurnComplete,
+ createToolResultMessage,
+ createAssistantMessage,
+ onFlowComplete,
+ onTimings,
+ onTurnComplete
+ } = callbacks;
+
+ const sessionMessages: AgenticMessage[] = toAgenticMessages(messages);
+ let capturedTimings: ChatMessageTimings | undefined;
+ let totalToolCallCount = 0;
+
+ const agenticTimings: ChatMessageAgenticTimings = {
+ turns: 0,
+ toolCallsCount: 0,
+ toolsMs: 0,
+ toolCalls: [],
+ perTurn: [],
+ llm: { predicted_n: 0, predicted_ms: 0, prompt_n: 0, prompt_ms: 0 }
+ };
+ const maxTurns = agenticConfig.maxTurns;
+
+ const effectiveModel = options.model || modelsStore.models[0]?.model || '';
+
+ let turn = 0;
+ while (true) {
+ if (turn >= maxTurns) {
+ // Turn limit reached - ask user whether to continue
+ const shouldContinue = await this.requestContinue(conversationId, signal);
+
+ // Yield to allow Svelte to flush the UI update
+ await new Promise((r) => setTimeout(r, 0));
+
+ if (!shouldContinue || signal?.aborted) {
+ onFlowComplete?.(this.buildFinalTimings(capturedTimings, agenticTimings));
+ return;
+ }
+
+ // User chose to continue - extend the limit
+ turn = 0;
+ }
+
+ this.updateSession(conversationId, { currentTurn: turn + 1 });
+ agenticTimings.turns = turn + 1;
+
+ if (signal?.aborted) {
+ onFlowComplete?.(this.buildFinalTimings(capturedTimings, agenticTimings));
+ return;
+ }
+
+ // For turns > 0, create a new assistant message via callback
+ if (turn > 0 && createAssistantMessage) {
+ await createAssistantMessage();
+ }
+
+ let turnContent = '';
+ let turnReasoningContent = '';
+ let turnToolCalls: ApiChatCompletionToolCall[] = [];
+ let lastStreamingToolCallName = '';
+ let lastStreamingToolCallArgsLength = 0;
+ let turnTimings: ChatMessageTimings | undefined;
+
+ const turnStats: ChatMessageAgenticTurnStats = {
+ turn: turn + 1,
+ llm: { predicted_n: 0, predicted_ms: 0, prompt_n: 0, prompt_ms: 0 },
+ toolCalls: [],
+ toolsMs: 0
+ };
+
+ try {
+ await ChatService.sendMessage(
+ sessionMessages as ApiChatMessageData[],
+ {
+ ...options,
+ stream: true,
+ tools: tools.length > 0 ? tools : undefined,
+ onChunk: (chunk: string) => {
+ turnContent += chunk;
+ onChunk?.(chunk);
+ },
+ onReasoningChunk: (chunk: string) => {
+ turnReasoningContent += chunk;
+ onReasoningChunk?.(chunk);
+ },
+ onToolCallChunk: (serialized: string) => {
+ try {
+ turnToolCalls = JSON.parse(serialized) as ApiChatCompletionToolCall[];
+ onToolCallsStreaming?.(turnToolCalls);
+
+ if (turnToolCalls.length > 0 && turnToolCalls[0]?.function) {
+ const name = turnToolCalls[0].function.name || '';
+ const args = turnToolCalls[0].function.arguments || '';
+ const argsLengthBucket = Math.floor(args.length / 100);
+ if (
+ name !== lastStreamingToolCallName ||
+ argsLengthBucket !== lastStreamingToolCallArgsLength
+ ) {
+ lastStreamingToolCallName = name;
+ lastStreamingToolCallArgsLength = argsLengthBucket;
+ this.updateSession(conversationId, {
+ streamingToolCall: { name, arguments: args }
+ });
+ }
+ }
+ } catch {
+ /* Ignore parse errors during streaming */
+ }
+ },
+ onModel,
+ onCompletionId,
+ onTimings: (timings?: ChatMessageTimings, progress?: ChatMessagePromptProgress) => {
+ onTimings?.(timings, progress);
+ if (timings) {
+ capturedTimings = timings;
+ turnTimings = timings;
+ }
+ },
+ onComplete: () => {
+ /* Completion handled after sendMessage resolves */
+ },
+ onError: (error: Error) => {
+ throw error;
+ }
+ },
+ conversationId,
+ signal
+ );
+
+ this.updateSession(conversationId, { streamingToolCall: null });
+
+ if (turnTimings) {
+ agenticTimings.llm.predicted_n += turnTimings.predicted_n || 0;
+ agenticTimings.llm.predicted_ms += turnTimings.predicted_ms || 0;
+ agenticTimings.llm.prompt_n += turnTimings.prompt_n || 0;
+ agenticTimings.llm.prompt_ms += turnTimings.prompt_ms || 0;
+ turnStats.llm.predicted_n = turnTimings.predicted_n || 0;
+ turnStats.llm.predicted_ms = turnTimings.predicted_ms || 0;
+ turnStats.llm.prompt_n = turnTimings.prompt_n || 0;
+ turnStats.llm.prompt_ms = turnTimings.prompt_ms || 0;
+ }
+ } catch (error) {
+ if (signal?.aborted) {
+ // Save whatever we have for this turn before exiting
+ await onAssistantTurnComplete?.(
+ turnContent,
+ turnReasoningContent || undefined,
+ this.buildFinalTimings(capturedTimings, agenticTimings),
+ undefined
+ );
+ onFlowComplete?.(this.buildFinalTimings(capturedTimings, agenticTimings));
+ return;
+ }
+ const normalizedError = error instanceof Error ? error : new Error('LLM stream error');
+ // preserve partial output as is, the outer error dialog informs the user separately
+ await onAssistantTurnComplete?.(
+ turnContent,
+ turnReasoningContent || undefined,
+ this.buildFinalTimings(capturedTimings, agenticTimings),
+ undefined
+ );
+ onFlowComplete?.(this.buildFinalTimings(capturedTimings, agenticTimings));
+ throw normalizedError;
+ }
+
+ // === Steering check: if a user message was queued during this turn, exit the flow.
+ // The caller (chatStore) will consume the pending message and re-send it normally.
+ if (this._steeringMessages.has(conversationId)) {
+ console.log('[AgenticStore] Steering message detected after turn, exiting agentic flow');
+ await onAssistantTurnComplete?.(
+ turnContent,
+ turnReasoningContent || undefined,
+ this.buildFinalTimings(capturedTimings, agenticTimings),
+ turnToolCalls.length > 0 ? this.normalizeToolCalls(turnToolCalls) : undefined
+ );
+ onFlowComplete?.(this.buildFinalTimings(capturedTimings, agenticTimings));
+ return;
+ }
+
+ // No tool calls = final turn, save and complete
+ if (turnToolCalls.length === 0) {
+ agenticTimings.perTurn!.push(turnStats);
+
+ const finalTimings = this.buildFinalTimings(capturedTimings, agenticTimings);
+
+ await onAssistantTurnComplete?.(
+ turnContent,
+ turnReasoningContent || undefined,
+ finalTimings,
+ undefined
+ );
+
+ if (finalTimings) onTurnComplete?.(finalTimings);
+
+ onFlowComplete?.(finalTimings);
+
+ return;
+ }
+
+ // Normalize and save assistant turn with tool calls
+ const normalizedCalls = this.normalizeToolCalls(turnToolCalls);
+ if (normalizedCalls.length === 0) {
+ await onAssistantTurnComplete?.(
+ turnContent,
+ turnReasoningContent || undefined,
+ this.buildFinalTimings(capturedTimings, agenticTimings),
+ undefined
+ );
+ onFlowComplete?.(this.buildFinalTimings(capturedTimings, agenticTimings));
+ return;
+ }
+
+ totalToolCallCount += normalizedCalls.length;
+ this.updateSession(conversationId, { totalToolCalls: totalToolCallCount });
+
+ // Save the assistant message with its tool calls
+ await onAssistantTurnComplete?.(
+ turnContent,
+ turnReasoningContent || undefined,
+ turnTimings,
+ normalizedCalls
+ );
+
+ // Add assistant message to session history
+ sessionMessages.push({
+ role: MessageRole.ASSISTANT,
+ content: turnContent || undefined,
+ reasoning_content: turnReasoningContent || undefined,
+ tool_calls: normalizedCalls
+ });
+
+ // Execute each tool call and create result messages
+ for (let i = 0; i < normalizedCalls.length; i++) {
+ const toolCall = normalizedCalls[i];
+
+ if (signal?.aborted) {
+ onFlowComplete?.(this.buildFinalTimings(capturedTimings, agenticTimings));
+ return;
+ }
+
+ // Check for pending steering message - skip remaining tool calls
+ if (this._steeringMessages.has(conversationId)) {
+ console.log(
+ `[AgenticStore] Steering message detected, skipping ${normalizedCalls.length - i} remaining tool call(s)`
+ );
+ for (let j = i; j < normalizedCalls.length; j++) {
+ const remainingCall = normalizedCalls[j];
+ const interruptedContent = 'Tool execution was interrupted by a new user message.';
+ if (createToolResultMessage) {
+ await createToolResultMessage(remainingCall.id, interruptedContent);
+ }
+ sessionMessages.push({
+ role: MessageRole.TOOL,
+ tool_call_id: remainingCall.id,
+ content: interruptedContent
+ });
+ }
+ break;
+ }
+
+ const toolName = toolCall.function.name;
+ const serverLabel = toolsStore.getToolServerLabel(toolName);
+
+ // Ask for permission before executing the tool
+ const permission = await this.requestPermission(
+ conversationId,
+ toolName,
+ serverLabel,
+ signal
+ );
+
+ // Yield to allow Svelte to flush the UI update (hide permission dialog)
+ await new Promise((r) => setTimeout(r, 0));
+
+ if (signal?.aborted) {
+ onFlowComplete?.(this.buildFinalTimings(capturedTimings, agenticTimings));
+ return;
+ }
+
+ const toolStartTime = performance.now();
+ const toolSource = toolsStore.getToolSource(toolName);
+
+ let result: string;
+ let toolSuccess = true;
+
+ if (permission === ToolPermissionDecision.DENY) {
+ result = 'Tool execution was denied by the user.';
+ toolSuccess = false;
+ } else {
+ try {
+ if (toolSource === ToolSource.BUILTIN) {
+ const args = this.parseToolArguments(toolCall.function.arguments);
+ const executionResult = await ToolsService.executeTool(toolName, args, signal);
+
+ result = executionResult.content;
+
+ if (executionResult.isError) toolSuccess = false;
+ } else if (toolSource === ToolSource.FRONTEND) {
+ const args = this.parseToolArguments(toolCall.function.arguments);
+ const executionResult = await SandboxService.executeTool(toolName, args, signal);
+
+ result = executionResult.content;
+
+ if (executionResult.isError) toolSuccess = false;
+ } else {
+ const mcpCall: MCPToolCall = {
+ id: toolCall.id,
+ function: { name: toolName, arguments: toolCall.function.arguments }
+ };
+ const executionResult = await mcpStore.executeTool(mcpCall, signal);
+
+ result = executionResult.content;
+ }
+ } catch (error) {
+ if (isAbortError(error)) {
+ onFlowComplete?.(this.buildFinalTimings(capturedTimings, agenticTimings));
+ return;
+ }
+ result = `Error: ${error instanceof Error ? error.message : String(error)}`;
+ toolSuccess = false;
+ }
+ }
+
+ const toolDurationMs = performance.now() - toolStartTime;
+ const toolTiming: ChatMessageToolCallTiming = {
+ name: toolCall.function.name,
+ duration_ms: Math.round(toolDurationMs),
+ success: toolSuccess
+ };
+
+ agenticTimings.toolCalls!.push(toolTiming);
+ agenticTimings.toolCallsCount++;
+ agenticTimings.toolsMs += Math.round(toolDurationMs);
+ turnStats.toolCalls.push(toolTiming);
+ turnStats.toolsMs += Math.round(toolDurationMs);
+
+ if (signal?.aborted) {
+ onFlowComplete?.(this.buildFinalTimings(capturedTimings, agenticTimings));
+ return;
+ }
+
+ const { cleanedResult, attachments } = this.extractBase64Attachments(result);
+
+ // Create the tool result message in the DB
+ let toolResultMessage: DatabaseMessage | undefined;
+ if (createToolResultMessage) {
+ toolResultMessage = await createToolResultMessage(
+ toolCall.id,
+ cleanedResult,
+ attachments.length > 0 ? attachments : undefined
+ );
+ }
+
+ if (attachments.length > 0 && toolResultMessage) {
+ onAttachments?.(toolResultMessage.id, attachments);
+ }
+
+ // Build content parts for session history (including images for vision models)
+ const contentParts: ApiChatMessageContentPart[] = [
+ { type: ContentPartType.TEXT, text: cleanedResult }
+ ];
+ for (const attachment of attachments) {
+ if (attachment.type === AttachmentType.IMAGE) {
+ if (modelsStore.modelSupportsVision(effectiveModel)) {
+ contentParts.push({
+ type: ContentPartType.IMAGE_URL,
+ image_url: {
+ url: (attachment as DatabaseMessageExtraImageFile).base64Url
+ }
+ });
+ } else {
+ console.info(
+ `[AgenticStore] Skipping image attachment (model "${effectiveModel}" does not support vision)`
+ );
+ }
+ }
+ }
+
+ sessionMessages.push({
+ role: MessageRole.TOOL,
+ tool_call_id: toolCall.id,
+ content: contentParts.length === 1 ? cleanedResult : contentParts
+ });
+ }
+
+ if (turnStats.toolCalls.length > 0) {
+ agenticTimings.perTurn!.push(turnStats);
+
+ const intermediateTimings = this.buildFinalTimings(capturedTimings, agenticTimings);
+ if (intermediateTimings) onTurnComplete?.(intermediateTimings);
+ }
+
+ // If tools were interrupted by a steering message, exit now instead of starting another LLM turn
+ if (this._steeringMessages.has(conversationId)) {
+ console.log(
+ '[AgenticStore] Steering message detected after tool execution, exiting agentic flow'
+ );
+ onFlowComplete?.(this.buildFinalTimings(capturedTimings, agenticTimings));
+ return;
+ }
+
+ turn++;
+ }
+ }
+
+ private buildFinalTimings(
+ capturedTimings: ChatMessageTimings | undefined,
+ agenticTimings: ChatMessageAgenticTimings
+ ): ChatMessageTimings | undefined {
+ if (agenticTimings.toolCallsCount === 0) return capturedTimings;
+ return {
+ predicted_n: capturedTimings?.predicted_n,
+ predicted_ms: capturedTimings?.predicted_ms,
+ prompt_n: capturedTimings?.prompt_n,
+ prompt_ms: capturedTimings?.prompt_ms,
+ cache_n: capturedTimings?.cache_n,
+ agentic: agenticTimings
+ };
+ }
+
+ private normalizeToolCalls(toolCalls: ApiChatCompletionToolCall[]): AgenticToolCallList {
+ if (!toolCalls) return [];
+ return toolCalls.map((call, index) => ({
+ id: call?.id ?? `tool_${index}`,
+ type: (call?.type as ToolCallType.FUNCTION) ?? ToolCallType.FUNCTION,
+ function: {
+ name: call?.function?.name ?? '',
+ arguments: call?.function?.arguments ?? ''
+ }
+ }));
+ }
+
+ private extractBase64Attachments(result: string): {
+ cleanedResult: string;
+ attachments: DatabaseMessageExtra[];
+ } {
+ if (!result.trim()) {
+ return { cleanedResult: result, attachments: [] };
+ }
+
+ const lines = result.split(NEWLINE_SEPARATOR);
+ const attachments: DatabaseMessageExtra[] = [];
+ let attachmentIndex = 0;
+
+ const cleanedLines = lines.map((line) => {
+ const trimmedLine = line.trim();
+
+ const match = trimmedLine.match(DATA_URI_BASE64_REGEX);
+ if (!match) {
+ return line;
+ }
+
+ const mimeType = match[1].toLowerCase();
+ const base64Data = match[2];
+
+ if (!base64Data) {
+ return line;
+ }
+
+ attachmentIndex += 1;
+ const name = this.buildAttachmentName(mimeType, attachmentIndex);
+
+ if (mimeType.startsWith(MimeTypePrefix.IMAGE)) {
+ attachments.push({ type: AttachmentType.IMAGE, name, base64Url: trimmedLine });
+
+ return `[Attachment saved: ${name}]`;
+ }
+
+ return line;
+ });
+
+ return { cleanedResult: cleanedLines.join(NEWLINE_SEPARATOR), attachments };
+ }
+
+ private buildAttachmentName(mimeType: string, index: number): string {
+ const extension = IMAGE_MIME_TO_EXTENSION[mimeType] ?? DEFAULT_IMAGE_EXTENSION;
+
+ return `${MCP_ATTACHMENT_NAME_PREFIX}-${Date.now()}-${index}.${extension}`;
+ }
+}
+
+export const agenticStore = new AgenticStore();
+
+export function agenticIsRunning(conversationId: string) {
+ return agenticStore.isRunning(conversationId);
+}
+
+export function agenticCurrentTurn(conversationId: string) {
+ return agenticStore.currentTurn(conversationId);
+}
+
+export function agenticTotalToolCalls(conversationId: string) {
+ return agenticStore.totalToolCalls(conversationId);
+}
+
+export function agenticLastError(conversationId: string) {
+ return agenticStore.lastError(conversationId);
+}
+
+export function agenticStreamingToolCall(conversationId: string) {
+ return agenticStore.streamingToolCall(conversationId);
+}
+
+export function agenticPendingPermissionRequest(conversationId: string) {
+ return agenticStore.pendingPermissionRequest(conversationId);
+}
+
+export function agenticResolvePermission(conversationId: string, decision: ToolPermissionDecision) {
+ agenticStore.resolvePermission(conversationId, decision);
+}
+
+export function agenticPendingContinueRequest(conversationId: string) {
+ return agenticStore.pendingContinueRequest(conversationId);
+}
+
+export function agenticResolveContinue(conversationId: string, shouldContinue: boolean) {
+ agenticStore.resolveContinue(conversationId, shouldContinue);
+}
+
+export function agenticHasPendingSteeringMessage(conversationId: string) {
+ return agenticStore.hasPendingSteeringMessage(conversationId);
+}
+
+export function agenticInjectSteeringMessage(
+ conversationId: string,
+ content: string,
+ extras?: DatabaseMessageExtra[]
+) {
+ agenticStore.injectSteeringMessage(conversationId, content, extras);
+}
+
+export function agenticPendingSteeringMessageContent(conversationId: string) {
+ return agenticStore.pendingSteeringMessageContent(conversationId);
+}
+
+export function agenticPendingSteeringMessageExtras(conversationId: string) {
+ return agenticStore.pendingSteeringMessageExtras(conversationId);
+}
+
+export function agenticClearSteeringMessage(conversationId: string) {
+ agenticStore.clearSteeringMessage(conversationId);
+}
+
+export function agenticIsAnyRunning() {
+ return agenticStore.isAnyRunning;
+}
diff --git a/llama.cpp/tools/ui/src/lib/stores/build-info.svelte.ts b/llama.cpp/tools/ui/src/lib/stores/build-info.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..a137be2367ec5b23bcd4d964d332445d7a7d1f98
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/stores/build-info.svelte.ts
@@ -0,0 +1,42 @@
+/**
+ * buildInfoStore - llama.cpp build information
+ *
+ * Reads the build version from `build.json` — embedded at llama.cpp build time
+ * with the llama.cpp build number (LLAMA_BUILD_NUMBER). Shown in the UI when
+ * `showBuildVersion` is enabled.
+ *
+ * In dev mode (via `npm run dev`), falls back to `import.meta.env.DEV`'s truthy
+ * value since the artifact is not produced.
+ */
+
+import { browser } from '$app/environment';
+import { base } from '$app/paths';
+
+let build = $state('');
+
+async function loadBuild() {
+ if (!browser) return;
+
+ if (import.meta.env.DEV) {
+ build = 'dev';
+ return;
+ }
+
+ try {
+ const res = await fetch(`${base}/build.json`, { cache: 'no-store' });
+ if (res.ok) {
+ const data = await res.json();
+ build = data.version ?? '';
+ }
+ } catch {
+ // build.json missing or unreachable - leave as empty string
+ }
+}
+
+loadBuild();
+
+export const buildInfoStore = {
+ get value(): string {
+ return build;
+ }
+};
diff --git a/llama.cpp/tools/ui/src/lib/stores/chat.svelte.ts b/llama.cpp/tools/ui/src/lib/stores/chat.svelte.ts
new file mode 100644
index 0000000000000000000000000000000000000000..6949047b50b95d714f310081b77e5241e52684ad
--- /dev/null
+++ b/llama.cpp/tools/ui/src/lib/stores/chat.svelte.ts
@@ -0,0 +1,2458 @@
+/**
+ * chatStore - Reactive State Store for Chat Operations
+ *
+ * Manages chat lifecycle, streaming, message operations, and processing state.
+ *
+ * **Architecture & Relationships:**
+ * - **ChatService**: Stateless API layer (sendMessage, streaming)
+ * - **chatStore** (this): Reactive state + business logic
+ * - **conversationsStore**: Conversation persistence and navigation
+ *
+ * @see ChatService in services/chat.service.ts for API operations
+ */
+
+import { SvelteMap, SvelteSet } from 'svelte/reactivity';
+import { DatabaseService } from '$lib/services/database.service';
+import { ChatService } from '$lib/services/chat.service';
+import { streamIdentity } from '$lib/utils/stream-identity';
+import { getAuthHeaders } from '$lib/utils/api-headers';
+import { conversationsStore } from '$lib/stores/conversations.svelte';
+import { config } from '$lib/stores/settings.svelte';
+import { agenticStore } from '$lib/stores/agentic.svelte';
+import { mcpStore } from '$lib/stores/mcp.svelte';
+import { contextSize, isRouterMode } from '$lib/stores/server.svelte';
+import {
+ selectedModelName,
+ modelsStore,
+ selectedModelContextSize
+} from '$lib/stores/models.svelte';
+import {
+ normalizeModelName,
+ filterByLeafNodeId,
+ findDescendantMessages,
+ findLeafNode,
+ findMessageById,
+ isAbortError,
+ generateConversationTitle
+} from '$lib/utils';
+import { classifyContinueIntent } from '$lib/utils/agentic';
+import {
+ MAX_INACTIVE_CONVERSATION_STATES,
+ INACTIVE_CONVERSATION_STATE_MAX_AGE_MS,
+ SYSTEM_MESSAGE_PLACEHOLDER,
+ TITLE_GENERATION
+} from '$lib/constants';
+import type {
+ ChatMessageTimings,
+ ChatMessagePromptProgress,
+ ChatStreamCallbacks,
+ ErrorDialogState
+} from '$lib/types/chat';
+import type {
+ ApiChatMessageData,
+ ApiProcessingState,
+ ApiStreamSession,
+ DatabaseMessage,
+ DatabaseMessageExtra
+} from '$lib/types';
+import {
+ ContinueIntentKind,
+ ErrorDialogType,
+ MessageRole,
+ MessageType,
+ StreamConnectionState
+} from '$lib/enums';
+
+interface ConversationStateEntry {
+ lastAccessed: number;
+}
+
+class ChatStore {
+ activeProcessingState = $state(null);
+ currentResponse = $state('');
+ errorDialogState = $state(null);
+ isLoading = $state(false);
+ // true while the active conversation streams reasoning content but no visible content yet
+ isReasoning = $state(false);
+ // resumable stream connection state for the active conversation
+ // streaming -> bytes flowing normally, resuming -> waiting on /v1/stream/:id reconnect, lost -> unrecoverable
+ streamConnectionState = $state(StreamConnectionState.STREAMING);
+ chatLoadingStates = new SvelteMap();
+ chatReasoningStates = new SvelteMap();
+ chatStreamingStates = new SvelteMap<
+ string,
+ { response: string; messageId: string; model?: string | null }
+ >();
+ // convs that the backend reports as having a running session, populated by the global sync
+ // at app mount and on visibilitychange. it does not overlap with chatLoadingStates which
+ // tracks inferences driven by this browser, both are unioned to feed the sidebar spinners
+ private remoteRunningConvs = new SvelteSet();
+ // per conv attach lifecycle, used to derive the global streaming flag without flipping it
+ // off when one conv finishes while another is still streaming. mirrors chatLoadingStates
+ // in scope but tracks the attach + tee replay path specifically
+ private attachingConvs = new SvelteSet();
+ // in-flight discoverActiveStream guard, keyed by conv id
+ private discoveringConvs = new SvelteSet();
+ private abortControllers = new SvelteMap();
+ private preEncodeAbortController: AbortController | null = null;
+ private processingStates = new SvelteMap();
+ private conversationStateTimestamps = new SvelteMap();
+ private activeConversationId = $state(null);
+ private isStreamingActive = $state(false);
+ private isEditModeActive = $state(false);
+ private addFilesHandler: ((files: File[]) => void) | null = $state(null);
+ pendingEditMessageId = $state(null);
+ private messageUpdateCallback:
+ | ((messageId: string, updates: Partial) => void)
+ | null = null;
+ private _pendingDraftMessage = $state('');
+ private _pendingDraftFiles = $state([]);
+
+ /** Reactive: queued pending messages for non-agentic streaming */
+ private _pendingMessages = new SvelteMap<
+ string,
+ { content: string; extras?: DatabaseMessageExtra[] }
+ >();
+
+ private setChatLoading(convId: string, loading: boolean): void {
+ this.touchConversationState(convId);
+ if (loading) {
+ this.chatLoadingStates.set(convId, true);
+ if (convId === conversationsStore.activeConversation?.id) this.isLoading = true;
+ } else {
+ this.chatLoadingStates.delete(convId);
+ if (convId === conversationsStore.activeConversation?.id) this.isLoading = false;
+ this.setChatReasoning(convId, false);
+ // the local pipe is the authoritative observer of session end: when it finishes (clean
+ // onComplete or explicit Stop), the backend session is finalized too, so we drop the
+ // sidebar hint for this conv right away instead of waiting for the next visibilitychange
+ // snapshot. without this the spinner ghosts until the user toggles the tab
+ this.remoteRunningConvs.delete(convId);
+ }
+ }
+
+ private setChatReasoning(convId: string, reasoning: boolean): void {
+ if (reasoning) {
+ this.chatReasoningStates.set(convId, true);
+ if (convId === conversationsStore.activeConversation?.id) this.isReasoning = true;
+ } else {
+ this.chatReasoningStates.delete(convId);
+ if (convId === conversationsStore.activeConversation?.id) this.isReasoning = false;
+ }
+ }
+ private setChatStreaming(
+ convId: string,
+ response: string,
+ messageId: string,
+ model?: string | null
+ ): void {
+ this.touchConversationState(convId);
+ this.chatStreamingStates.set(convId, {
+ response,
+ messageId,
+ model: model ?? this.chatStreamingStates.get(convId)?.model
+ });
+ if (convId === conversationsStore.activeConversation?.id) this.currentResponse = response;
+ }
+ private clearChatStreaming(convId: string, messageId?: string): void {
+ // session aware: a stale generation must not wipe a newer one's streaming state on the
+ // same conversation, that would drop the frozen stop identity and stop the wrong session
+ if (messageId !== undefined) {
+ const cur = this.chatStreamingStates.get(convId);
+ if (cur && cur.messageId !== messageId) return;
+ }
+ this.chatStreamingStates.delete(convId);
+ if (convId === conversationsStore.activeConversation?.id) this.currentResponse = '';
+ }
+ private getChatStreaming(convId: string): { response: string; messageId: string } | undefined {
+ return this.chatStreamingStates.get(convId);
+ }
+ syncLoadingStateForChat(convId: string): void {
+ this.isLoading = this.chatLoadingStates.get(convId) || false;
+ this.isReasoning = this.chatReasoningStates.get(convId) || false;
+ const s = this.chatStreamingStates.get(convId);
+ this.currentResponse = s?.response || '';
+ this.isStreamingActive = s !== undefined;
+ this.setActiveProcessingConversation(convId);
+ // Sync streaming content to activeMessages so UI displays current content
+ if (s?.response && s?.messageId) {
+ const idx = conversationsStore.findMessageIndex(s.messageId);
+ if (idx !== -1) {
+ conversationsStore.updateMessageAtIndex(idx, { content: s.response });
+ }
+ }
+ }
+ /**
+ * Server side stream discovery, split in three pieces:
+ *
+ * probeServerStream(convId) -> hits POST /v1/streams/lookup with the conv id, returns the session to attach
+ * to or null. Pure read, no side effect, no UI lock. Safe to fire in parallel with anything.
+ *
+ * attachServerStream(convId) -> flips the spinner immediately, fetches the replay stream
+ * from byte 0, finds the assistant slot to splice into (creates a placeholder if the conv has
+ * no assistant message yet, for cross device or fresh local DB cases), and pipes the SSE bytes
+ * into the message via handleStreamResponse.
+ *
+ * discoverActiveStream(convId) -> probe + attach in one call. Used by callers that do not need
+ * to overlap the probe with other async work.
+ *
+ * The mount of the chat page in +page.svelte calls probeServerStream in parallel with
+ * loadConversation, then attachServerStream once both have settled. This gives the earliest
+ * possible time to spinner and avoids racing against an empty activeMessages array.
+ */
+ async probeServerStream(convId: string): Promise {
+ if (!convId) return null;
+ let listResp: Response;
+ try {
+ // POST the one conv id we are probing
+ listResp = await fetch(`./v1/streams/lookup`, {
+ method: 'POST',
+ headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
+ body: JSON.stringify({ conversation_ids: [convId] })
+ });
+ } catch (e) {
+ console.warn('probeServerStream fetch failed:', e);
+ return null;
+ }
+ if (!listResp.ok) {
+ console.warn(`probeServerStream got HTTP ${listResp.status} for conv ${convId}`);
+ return null;
+ }
+ let sessions: ApiStreamSession[];
+ try {
+ sessions = (await listResp.json()) as ApiStreamSession[];
+ } catch (e) {
+ console.warn('probeServerStream JSON parse failed:', e);
+ return null;
+ }
+ return ChatService.selectActiveStream(sessions);
+ }
+
+ async attachServerStream(convId: string, streamId?: string): Promise {
+ if (!convId) return;
+ if (this.chatStreamingStates.has(convId)) return;
+
+ // flip the spinner immediately, the user sees activity as soon as the conv becomes active.
+ // the global isStreamingActive flag is derived from attachingConvs.size, so adding here
+ // turns it on, and removing in unlock only turns it off when this is the last attach
+ this.setChatLoading(convId, true);
+ this.attachingConvs.add(convId);
+ this.setStreamingActive(true);
+ // only set the active processing conv if we are looking at it, otherwise a background
+ // attach would steal the indicator from the conv the user is currently viewing
+ if (convId === conversationsStore.activeConversation?.id) {
+ this.setActiveProcessingConversation(convId);
+ }
+
+ const unlock = () => {
+ this.attachingConvs.delete(convId);
+ // flip the global flag off only when no other conv is still attaching
+ if (this.attachingConvs.size === 0) {
+ this.setStreamingActive(false);
+ }
+ this.setChatLoading(convId, false);
+ this.clearChatStreaming(convId);
+ };
+
+ // fetch the replay stream from byte 0, rebuild the assistant message from scratch.
+ // resolve the server side identity, fall back to streamIdentity when the caller does not
+ // pass a streamId. probeServerStream returns the full id (with ::model suffix when present)
+ const id = streamId || streamIdentity(convId, selectedModelName());
+ let response: Response;
+ try {
+ response = await fetch(`./v1/stream/${encodeURIComponent(id)}?from=0`, {
+ headers: getAuthHeaders()
+ });
+ } catch (e) {
+ console.error('attachServerStream replay fetch failed:', e);
+ unlock();
+ return;
+ }
+ if (!response.ok) {
+ console.warn(`attachServerStream replay got HTTP ${response.status} for conv ${convId}`);
+ unlock();
+ return;
+ }
+
+ // load the target conversation messages by id, not via the active store. when multiple
+ // attaches run in parallel the active store may reflect another conv and writing through
+ // its index mixes content across convs (CoT flicker, message bleed). by going through the
+ // DB we stay isolated, and only mirror into the active store when the attached conv is
+ // the one currently displayed
+ let messages: DatabaseMessage[];
+ try {
+ messages = await DatabaseService.getConversationMessages(convId);
+ } catch (e) {
+ console.error('attachServerStream load messages failed:', e);
+ unlock();
+ return;
+ }
+
+ // locate the slot to splice into, create a placeholder assistant message if there is none.
+ // we use the conv-scoped findLastAssistantIdx helpers, they only depend on the array
+ let targetIdx = this.findLastAssistantIdx(messages);
+ if (targetIdx === -1) {
+ const lastUserIdx = this.findLastUserIdx(messages);
+ if (lastUserIdx === -1) {
+ console.warn(
+ `attachServerStream: conv ${convId} has no user or assistant message, cannot splice`
+ );
+ unlock();
+ return;
+ }
+ try {
+ const placeholder = await DatabaseService.createMessageBranch(
+ {
+ convId,
+ role: MessageRole.ASSISTANT,
+ content: '',
+ type: MessageType.TEXT,
+ timestamp: Date.now(),
+ parent: messages[lastUserIdx].id,
+ children: [],
+ toolCalls: ''
+ } as Omit