* 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)
139 lines
5.2 KiB
TypeScript
139 lines
5.2 KiB
TypeScript
// Tests for the memoized parseToolCalls and O(1) tool message lookup in
|
|
// deriveAgenticSections. These were added to prevent regressions where
|
|
// streaming text tokens trigger redundant JSON.parse calls on unchanged
|
|
// tool call data.
|
|
|
|
import { AgenticSectionType, MessageRole } from '#lib/enums/index.ts';
|
|
import type { ApiChatCompletionToolCall } from '#lib/types/api.js';
|
|
import type { DatabaseMessage } from '#lib/types/database.js';
|
|
import { deriveAgenticSections } from '#lib/utils/agentic.ts';
|
|
import { describe, expect, it, vi } from 'vitest';
|
|
|
|
function makeMessage(overrides: Partial<DatabaseMessage>): DatabaseMessage {
|
|
return {
|
|
children: [],
|
|
content: '',
|
|
convId: 'c1',
|
|
id: 'm1',
|
|
parent: null,
|
|
role: MessageRole.ASSISTANT,
|
|
timestamp: 0,
|
|
type: 'text',
|
|
...overrides
|
|
} as DatabaseMessage;
|
|
}
|
|
|
|
describe('parseToolCalls memoization', () => {
|
|
it('returns the same array reference for the same JSON string', () => {
|
|
// parseToolCalls is not exported, but deriveAgenticSections uses it
|
|
// internally. We verify memoization through behavior: calling
|
|
// deriveAgenticSections twice with the same toolCalls should not
|
|
// re-parse (which we verify by checking the returned sections
|
|
// are equivalent).
|
|
const toolCallsJson = JSON.stringify([
|
|
{ function: { arguments: '{}', name: 'test' }, id: 'call_1', type: 'function' }
|
|
]);
|
|
const msg = makeMessage({ content: 'hello', toolCalls: toolCallsJson });
|
|
const sections1 = deriveAgenticSections(msg, [], [], false);
|
|
const sections2 = deriveAgenticSections(msg, [], [], false);
|
|
|
|
expect(sections1).toHaveLength(sections2.length);
|
|
expect(sections1[0].type).toBe(sections2[0].type);
|
|
});
|
|
|
|
it('does not re-parse JSON on cache hit', () => {
|
|
const toolCallsJson = JSON.stringify([
|
|
{ function: { arguments: '{}', name: 'test' }, id: 'call_1', type: 'function' }
|
|
]);
|
|
const msg = makeMessage({ content: 'hello', toolCalls: toolCallsJson });
|
|
const spy = vi.spyOn(JSON, 'parse');
|
|
|
|
deriveAgenticSections(msg, [], [], false);
|
|
const callsAfterFirst = spy.mock.calls.length;
|
|
|
|
deriveAgenticSections(msg, [], [], false);
|
|
expect(spy.mock.calls.length).toBe(callsAfterFirst);
|
|
|
|
spy.mockRestore();
|
|
});
|
|
|
|
it('handles empty/undefined toolCalls without error', () => {
|
|
const msg = makeMessage({ content: 'hello' });
|
|
const sections = deriveAgenticSections(msg, [], [], false);
|
|
|
|
expect(sections).toHaveLength(1);
|
|
expect(sections[0].type).toBe(AgenticSectionType.TEXT);
|
|
});
|
|
|
|
it('handles invalid JSON gracefully', () => {
|
|
const msg = makeMessage({ content: 'hello', toolCalls: '{invalid' });
|
|
const sections = deriveAgenticSections(msg, [], [], false);
|
|
|
|
// Should return just the text section, no tool call sections
|
|
expect(sections).toHaveLength(1);
|
|
expect(sections[0].type).toBe(AgenticSectionType.TEXT);
|
|
});
|
|
});
|
|
|
|
describe('deriveAgenticSections O(1) tool message lookup', () => {
|
|
it('matches tool messages to tool calls by toolCallId', () => {
|
|
const toolCallsJson = JSON.stringify([
|
|
{ function: { arguments: '{}', name: 'test_1' }, id: 'call_1', type: 'function' },
|
|
{ function: { arguments: '{}', name: 'test_2' }, id: 'call_2', type: 'function' }
|
|
]);
|
|
const toolMessages = [
|
|
makeMessage({ content: 'result_1', role: MessageRole.TOOL, toolCallId: 'call_1' }),
|
|
makeMessage({ content: 'result_2', role: MessageRole.TOOL, toolCallId: 'call_2' })
|
|
];
|
|
const msg = makeMessage({ content: 'hello', toolCalls: toolCallsJson });
|
|
const sections = deriveAgenticSections(msg, toolMessages, [], false);
|
|
// Expect: TEXT + 2 TOOL_CALL sections
|
|
const toolCallSections = sections.filter((s) => s.type === AgenticSectionType.TOOL_CALL);
|
|
|
|
expect(toolCallSections).toHaveLength(2);
|
|
expect(toolCallSections[0].toolResult).toBe('result_1');
|
|
expect(toolCallSections[1].toolResult).toBe('result_2');
|
|
});
|
|
|
|
it('handles missing tool messages (pending calls during streaming)', () => {
|
|
const toolCallsJson = JSON.stringify([
|
|
{ function: { arguments: '{}', name: 'test' }, id: 'call_1', type: 'function' }
|
|
]);
|
|
const msg = makeMessage({ content: '', toolCalls: toolCallsJson });
|
|
const sections = deriveAgenticSections(msg, [], [], true);
|
|
const toolCallSection = sections.find((s) => s.type === AgenticSectionType.TOOL_CALL_PENDING);
|
|
|
|
expect(toolCallSection).toBeDefined();
|
|
expect(toolCallSection?.content).toBe('');
|
|
});
|
|
|
|
it('scales with many tool calls (no O(n^2) blowup)', () => {
|
|
const N = 100;
|
|
const toolCalls = Array.from(
|
|
{ length: N },
|
|
(_, i): ApiChatCompletionToolCall => ({
|
|
function: { arguments: '{}', name: `tool_${i}` },
|
|
id: `call_${i}`,
|
|
type: 'function'
|
|
})
|
|
);
|
|
const toolCallsJson = JSON.stringify(toolCalls);
|
|
const toolMessages = Array.from({ length: N }, (_, i) =>
|
|
makeMessage({
|
|
content: `result_${i}`,
|
|
role: MessageRole.TOOL,
|
|
toolCallId: `call_${i}`
|
|
})
|
|
);
|
|
const msg = makeMessage({ content: 'hello', toolCalls: toolCallsJson });
|
|
// If the lookup were still O(n^2), this would be noticeably slow
|
|
const start = Date.now();
|
|
const sections = deriveAgenticSections(msg, toolMessages, [], false);
|
|
const elapsed = Date.now() - start;
|
|
const toolCallSections = sections.filter((s) => s.type === AgenticSectionType.TOOL_CALL);
|
|
|
|
expect(toolCallSections).toHaveLength(N);
|
|
expect(elapsed).toBeLessThan(100); // Should be fast with O(1) lookup
|
|
});
|
|
});
|