* ui: replace deprecated packages and fix npm audit findings
Swap the mdast stub for @types/mdast, move the katex override to 0.19.0,
and override glob, source-map and html-encoding-sniffer under
workbox-build and http-server so installs no longer pull deprecated
packages. Apply the non-breaking npm audit fixes to the lockfile.
Assisted-by: Claude (Opus 5.5 Medium)
* ui: migrate Llama Chat to SvelteKit 3 and its built-in service worker
Move to SvelteKit 3, Vite 8, TypeScript 6 and Storybook 10.6. The
SvelteKit options move from svelte.config.js into sveltekit() in
vite.config.ts, $lib imports become #lib subpath imports from
package.json, and $app/environment becomes $app/env. Modules named
server.* or living under a server/ folder are server-only in SvelteKit 3,
so the llama-server components, store and enums move to llama-server
names.
@vite-pwa/sveltekit and Workbox give way to SvelteKit's own service
worker in src/service-worker, registered by usePwa(), which precaches the
app files and leaves API requests to the network. A small plugin emits
manifest.webmanifest. static/sw.js removes the worker of older builds, and
llama-server serves a self-removing service-worker.js as well as sw.js
when the built-in UI is off or replaced.
Assisted-by: Claude (Opus 5.5 Medium)
* ui: use SvelteKit 3's update detection and Svelte 5's current patterns
A page the service worker doesn't control now learns of a new build
from SvelteKit itself: `updated` from $app/state checks
_app/version.json on navigation, on focus and every hour. It replaces
the version Llama Chat kept in localStorage and compared by hand.
Reload on the update prompt pulls in the new worker and activates it
before reloading, so one click lands on the new build, whether or not
a worker controlled the page.
Following Svelte's current guidance:
- usePwa keeps its state in $state rather than a store, and no effect
checks `browser`
- nearViewport, autoResizeTextarea and the custom CSS injection are
attachments ({@attach}) rather than actions (use:)
- class lists use arrays rather than class: directives
- the chat contexts come from createContext, typed, in place of
setContext/getContext with string keys
- ActionIconCopyToClipboard takes $props() rather than `export let`
Assisted-by: Claude (Opus 5.5 Medium)
* ui: move to ESLint 10
ESLint 9 is out of support. Every plugin the app uses supports 10:
typescript-eslint, eslint-plugin-svelte, perfectionist,
simple-import-sort, eslint-plugin-storybook and eslint-config-prettier.
@eslint/js and @eslint/compat follow.
ESLint 10's recommended rules ask for two things the code now does:
errors rethrown from a catch keep the caught error as their `cause`,
and no variable starts with a value that every path overwrites.
Assisted-by: Claude (Opus 5.5 Medium)
* ui: override minimatch to clear npm audit's last finding
ESLint 10.14, @eslint/config-array 0.23.5 and typescript-eslint 8.72
depend on minimatch 10, which GHSA-g3hv-75wg-m249 (denial of service
via globparts amplification) affects below 11.0.1. Their fixed releases
move to minimatch 11 but are all younger than the repo's 7-day
min-release-age, so an override pins minimatch 11.0.5, the newest the
rule allows. The three only call new Minimatch(), match() and
minimatch(), which minimatch 11 keeps. npm audit now finds nothing.
Assisted-by: Claude (Opus 5.5 Medium)
244 lines
7.7 KiB
TypeScript
244 lines
7.7 KiB
TypeScript
// Microbenchmarks for the functions on the agentic-thread streaming hot path.
|
|
//
|
|
// Every function here is invoked from a `$derived` that is invalidated on each
|
|
// streamed token, for every tool-call section in the message. These numbers give
|
|
// the per-call cost; multiply by (tokens x sections) for the real damage.
|
|
//
|
|
// Run: npx vitest bench --project=unit tests/unit/agentic-hotpath.bench.ts
|
|
|
|
import { classifyToolResult, parseToolResultWithMedia } from '#lib/utils/agentic.ts';
|
|
import { detectIncompleteCodeBlock, highlightCode } from '#lib/utils/code.ts';
|
|
import { computeLineDiff } from '#lib/utils/compute-line-diff.ts';
|
|
import { preprocessLaTeX } from '#lib/utils/latex-protection.ts';
|
|
import { parsePartialJsonArgs } from '#lib/utils/parse-partial-json-args.ts';
|
|
import { extractSearchQuery, extractSearchResults } from '#lib/utils/search-results.ts';
|
|
import { all as lowlightAll } from 'lowlight';
|
|
import rehypeHighlight from 'rehype-highlight';
|
|
import rehypeKatex from 'rehype-katex';
|
|
import rehypeStringify from 'rehype-stringify';
|
|
import { remark } from 'remark';
|
|
import remarkBreaks from 'remark-breaks';
|
|
import remarkGfm from 'remark-gfm';
|
|
import remarkMath from 'remark-math';
|
|
import remarkRehype from 'remark-rehype';
|
|
import { bench, describe } from 'vitest';
|
|
|
|
// --- fixtures -------------------------------------------------------------
|
|
|
|
function lines(n: number, seed: string): string {
|
|
const out: string[] = [];
|
|
|
|
for (let i = 0; i < n; i++) out.push(`${seed} line ${i} const value_${i} = compute(${i});`);
|
|
|
|
return out.join('\n');
|
|
}
|
|
|
|
const SHELL_OUTPUT_1KB = lines(12, 'out');
|
|
const SHELL_OUTPUT_200KB = lines(2600, 'out');
|
|
const SHELL_OUTPUT_2MB = lines(26000, 'out');
|
|
// Realistic exec_shell_command result: the exit-code marker is the final line,
|
|
// which is what the un-anchored EXIT_CODE regex has to scan the whole blob for.
|
|
const SHELL_2MB_WITH_EXIT = `${SHELL_OUTPUT_2MB}\n[exit code: 0]`;
|
|
const EDIT_OLD_400 = lines(400, 'old');
|
|
const EDIT_NEW_400 = lines(400, 'new');
|
|
const EDIT_OLD_50 = lines(50, 'old');
|
|
const EDIT_NEW_50 = lines(50, 'new');
|
|
const WRITE_FILE_ARGS = JSON.stringify({
|
|
content: lines(1500, 'src'),
|
|
path: '/src/lib/thing.ts'
|
|
});
|
|
const MARKDOWN_50KB = Array.from(
|
|
{ length: 400 },
|
|
(_, i) =>
|
|
`## Section ${i}\n\nSome **bold** prose with a [link](https://example.com) and \`inline\` code.\n\n- bullet one\n- bullet two\n\n\`\`\`ts\nconst x${i} = ${i};\n\`\`\`\n`
|
|
).join('\n');
|
|
const CODE_BLOCK_5KB = lines(60, 'code');
|
|
|
|
// --- A: computeLineDiff (O(m*n) LCS, allocates a full matrix) --------------
|
|
|
|
describe('computeLineDiff', () => {
|
|
bench('50 x 50 lines', () => {
|
|
computeLineDiff(EDIT_OLD_50, EDIT_NEW_50);
|
|
});
|
|
|
|
bench('400 x 400 lines', () => {
|
|
computeLineDiff(EDIT_OLD_400, EDIT_NEW_400);
|
|
});
|
|
});
|
|
|
|
// --- B: the unified processor, rebuilt per call ---------------------------
|
|
// Mirrors MarkdownContent.svelte:144-176. The local rehype/remark plugins are
|
|
// omitted (they pull in browser-only modules); rehypeHighlight + lowlightAll is
|
|
// the dominant term, so this is a lower bound on the real cost.
|
|
|
|
function buildProcessor() {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
let proc: any = remark().use(remarkGfm);
|
|
|
|
proc = proc.use(remarkMath).use(remarkBreaks).use(remarkRehype).use(rehypeKatex);
|
|
|
|
return proc.use(rehypeHighlight, { languages: lowlightAll }).use(rehypeStringify, {
|
|
allowDangerousHtml: true
|
|
});
|
|
}
|
|
|
|
describe('markdown processor', () => {
|
|
bench('build processor (per processMarkdown call, x2)', () => {
|
|
buildProcessor();
|
|
});
|
|
|
|
const prebuilt = buildProcessor();
|
|
|
|
bench('parse 50KB markdown with a prebuilt processor', () => {
|
|
prebuilt.parse(MARKDOWN_50KB);
|
|
});
|
|
});
|
|
|
|
// Isolates the O(n^2) term measured in Tier 1: processMarkdown re-parses the
|
|
// WHOLE accumulated string every frame, while only the last block can have
|
|
// changed. If parse() dominates at these sizes, incremental parsing (re-parsing
|
|
// only the tail after the last stable block) is the fix; if not, the cost is
|
|
// downstream in transform/stringify/DOM.
|
|
describe('markdown parse scaling (whole-string reparse per frame)', () => {
|
|
const proc = buildProcessor();
|
|
const prose = (kb: number) =>
|
|
Array.from(
|
|
{ length: Math.ceil((kb * 1024) / 64) },
|
|
(_, i) => `The quick brown fox jumps over the lazy dog. Sentence ${i}.`
|
|
).join(' ');
|
|
const MD_3KB = prose(3);
|
|
const MD_11KB = prose(11);
|
|
const MD_26KB = prose(26);
|
|
|
|
bench('parse 3KB', () => {
|
|
proc.parse(MD_3KB);
|
|
});
|
|
|
|
bench('parse 11KB', () => {
|
|
proc.parse(MD_11KB);
|
|
});
|
|
|
|
bench('parse 26KB', () => {
|
|
proc.parse(MD_26KB);
|
|
});
|
|
|
|
bench('parse only a 200-char tail (proposed incremental)', () => {
|
|
proc.parse(MD_26KB.slice(-200));
|
|
});
|
|
});
|
|
|
|
// processMarkdown runs these over the WHOLE accumulated string every frame too,
|
|
// before parse() even starts. Measure them before assuming parse is the target.
|
|
describe('other whole-string passes per frame', () => {
|
|
const prose = (kb: number) =>
|
|
Array.from(
|
|
{ length: Math.ceil((kb * 1024) / 64) },
|
|
(_, i) => `The quick brown fox jumps over the lazy dog. Sentence ${i}.`
|
|
).join(' ');
|
|
const MD_3KB = prose(3);
|
|
const MD_11KB = prose(11);
|
|
const MD_26KB = prose(26);
|
|
const MD_26KB_LATEX = `${MD_26KB} and some math $x^2 + y^2 = z^2$ inline.`;
|
|
|
|
bench('preprocessLaTeX 3KB (no latex present)', () => {
|
|
preprocessLaTeX(MD_3KB);
|
|
});
|
|
|
|
bench('preprocessLaTeX 11KB (no latex present)', () => {
|
|
preprocessLaTeX(MD_11KB);
|
|
});
|
|
|
|
bench('preprocessLaTeX 26KB (no latex present)', () => {
|
|
preprocessLaTeX(MD_26KB);
|
|
});
|
|
|
|
bench('preprocessLaTeX 26KB (latex present)', () => {
|
|
preprocessLaTeX(MD_26KB_LATEX);
|
|
});
|
|
|
|
bench('detectIncompleteCodeBlock 26KB', () => {
|
|
detectIncompleteCodeBlock(MD_26KB);
|
|
});
|
|
});
|
|
|
|
// --- C: extractSearchResults, run for EVERY tool of every type ------------
|
|
|
|
describe('extractSearchResults (only .length > 0 is consumed)', () => {
|
|
bench('1KB non-search tool result', () => {
|
|
extractSearchResults(SHELL_OUTPUT_1KB);
|
|
});
|
|
|
|
bench('200KB non-search tool result', () => {
|
|
extractSearchResults(SHELL_OUTPUT_200KB);
|
|
});
|
|
|
|
bench('2MB non-search tool result', () => {
|
|
extractSearchResults(SHELL_OUTPUT_2MB);
|
|
});
|
|
});
|
|
|
|
describe('extractSearchQuery', () => {
|
|
bench('write_file args (throws on the JSON.parse path)', () => {
|
|
extractSearchQuery(WRITE_FILE_ARGS);
|
|
});
|
|
});
|
|
|
|
// --- E: the un-anchored exit-code regex -----------------------------------
|
|
// Reproduced inline so the bench is independent of the current call site.
|
|
|
|
const EXIT_CODE_TAIL = /\[exit code: (-?\d+)\]\s*$/;
|
|
|
|
describe('exit-code regex', () => {
|
|
bench('whole 2MB blob (current behaviour)', () => {
|
|
SHELL_2MB_WITH_EXIT.match(EXIT_CODE_TAIL);
|
|
});
|
|
|
|
bench('last 64 chars only (proposed)', () => {
|
|
SHELL_2MB_WITH_EXIT.slice(-64).match(EXIT_CODE_TAIL);
|
|
});
|
|
});
|
|
|
|
// --- per-line result parsers ----------------------------------------------
|
|
|
|
describe('parseToolResultWithMedia', () => {
|
|
bench('1KB', () => {
|
|
parseToolResultWithMedia(SHELL_OUTPUT_1KB, []);
|
|
});
|
|
|
|
bench('200KB', () => {
|
|
parseToolResultWithMedia(SHELL_OUTPUT_200KB, []);
|
|
});
|
|
|
|
bench('2MB', () => {
|
|
parseToolResultWithMedia(SHELL_OUTPUT_2MB, []);
|
|
});
|
|
});
|
|
|
|
describe('classifyToolResult', () => {
|
|
bench('200KB plain text (falls through to looksLikeMarkdown)', () => {
|
|
classifyToolResult(SHELL_OUTPUT_200KB);
|
|
});
|
|
});
|
|
|
|
describe('parsePartialJsonArgs', () => {
|
|
bench('write_file args, ~60KB (char-by-char scan)', () => {
|
|
parsePartialJsonArgs(WRITE_FILE_ARGS);
|
|
});
|
|
});
|
|
|
|
// --- F / I: highlight.js ---------------------------------------------------
|
|
|
|
describe('highlightCode', () => {
|
|
bench('short bash command (title row, per token)', () => {
|
|
highlightCode('grep -rn "foo" src/ | head -50', 'bash');
|
|
});
|
|
|
|
bench('5KB known language', () => {
|
|
highlightCode(CODE_BLOCK_5KB, 'typescript');
|
|
});
|
|
|
|
bench('5KB unknown language -> highlightAuto', () => {
|
|
highlightCode(CODE_BLOCK_5KB, 'not-a-language');
|
|
});
|
|
});
|