char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Constants refactor (#26908)e21152dc9

char/aliceai-t5-moe
3.4 KiB100 linesraw
1import { LEGACY_AGENTIC_REGEX } from '$lib/constants';
2import { describe, expect, it } from 'vitest';
3
4/**
5 * Tests for legacy marker stripping (used in migration).
6 * The new system does not embed markers in content - these tests verify
7 * the legacy regex patterns still work for the migration code.
8 */
9
10// Mirror the legacy stripping logic used during migration
11function stripLegacyContextMarkers(content: string): string {
12	return content
13		.replace(new RegExp(LEGACY_AGENTIC_REGEX.REASONING_BLOCK.source, 'g'), '')
14		.replace(LEGACY_AGENTIC_REGEX.REASONING_OPEN, '')
15		.replace(new RegExp(LEGACY_AGENTIC_REGEX.AGENTIC_TOOL_CALL_BLOCK.source, 'g'), '')
16		.replace(LEGACY_AGENTIC_REGEX.AGENTIC_TOOL_CALL_OPEN, '');
17}
18
19// A realistic complete tool call block as stored in old message.content
20const COMPLETE_BLOCK =
21	'\n\n<<<AGENTIC_TOOL_CALL_START>>>\n' +
22	'<<<TOOL_NAME:bash_tool>>>\n' +
23	'<<<TOOL_ARGS_START>>>\n' +
24	'{"command":"ls /tmp","description":"list tmp"}\n' +
25	'<<<TOOL_ARGS_END>>>\n' +
26	'file1.txt\nfile2.txt\n' +
27	'<<<AGENTIC_TOOL_CALL_END>>>\n';
28// Partial block: streaming was cut before END arrived.
29const OPEN_BLOCK =
30	'\n\n<<<AGENTIC_TOOL_CALL_START>>>\n' +
31	'<<<TOOL_NAME:bash_tool>>>\n' +
32	'<<<TOOL_ARGS_START>>>\n' +
33	'{"command":"ls /tmp","description":"list tmp"}\n' +
34	'<<<TOOL_ARGS_END>>>\n' +
35	'partial output...';
36
37describe('legacy agentic marker stripping (for migration)', () => {
38	it('strips a complete tool call block, leaving surrounding text', () => {
39		const input = 'Before.' + COMPLETE_BLOCK + 'After.';
40		const result = stripLegacyContextMarkers(input);
41
42		expect(result).not.toContain('<<<');
43		expect(result).toContain('Before.');
44		expect(result).toContain('After.');
45	});
46
47	it('strips multiple complete tool call blocks', () => {
48		const input = 'A' + COMPLETE_BLOCK + 'B' + COMPLETE_BLOCK + 'C';
49		const result = stripLegacyContextMarkers(input);
50
51		expect(result).not.toContain('<<<');
52		expect(result).toContain('A');
53		expect(result).toContain('B');
54		expect(result).toContain('C');
55	});
56
57	it('strips an open/partial tool call block (no END marker)', () => {
58		const input = 'Lead text.' + OPEN_BLOCK;
59		const result = stripLegacyContextMarkers(input);
60
61		expect(result).toBe('Lead text.');
62		expect(result).not.toContain('<<<');
63	});
64
65	it('does not alter content with no markers', () => {
66		const input = 'Just a normal assistant response.';
67
68		expect(stripLegacyContextMarkers(input)).toBe(input);
69	});
70
71	it('strips reasoning block independently', () => {
72		const input = '<<<reasoning_content_start>>>think hard<<<reasoning_content_end>>>Answer.';
73
74		expect(stripLegacyContextMarkers(input)).toBe('Answer.');
75	});
76
77	it('strips both reasoning and agentic blocks together', () => {
78		const input =
79			'<<<reasoning_content_start>>>plan<<<reasoning_content_end>>>' +
80			'Some text.' +
81			COMPLETE_BLOCK;
82
83		expect(stripLegacyContextMarkers(input)).not.toContain('<<<');
84		expect(stripLegacyContextMarkers(input)).toContain('Some text.');
85	});
86
87	it('empty string survives', () => {
88		expect(stripLegacyContextMarkers('')).toBe('');
89	});
90
91	it('detects legacy markers', () => {
92		expect(LEGACY_AGENTIC_REGEX.HAS_LEGACY_MARKERS.test('normal text')).toBe(false);
93		expect(
94			LEGACY_AGENTIC_REGEX.HAS_LEGACY_MARKERS.test('text<<<AGENTIC_TOOL_CALL_START>>>more')
95		).toBe(true);
96		expect(LEGACY_AGENTIC_REGEX.HAS_LEGACY_MARKERS.test('<<<reasoning_content_start>>>think')).toBe(
97			true
98		);
99	});
100});