char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
e21152dc9
char/aliceai-t5-moe
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 { 12return 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)' , () => { 38it ( 'strips a complete tool call block, leaving surrounding text' , () => { 39const input = 'Before.' + COMPLETE_BLOCK + 'After.' ; 40const result = stripLegacyContextMarkers ( input ); 41 42expect ( result ). not . toContain ( '<<<' ); 43expect ( result ). toContain ( 'Before.' ); 44expect ( result ). toContain ( 'After.' ); 45}); 46 47it ( 'strips multiple complete tool call blocks' , () => { 48const input = 'A' + COMPLETE_BLOCK + 'B' + COMPLETE_BLOCK + 'C' ; 49const result = stripLegacyContextMarkers ( input ); 50 51expect ( result ). not . toContain ( '<<<' ); 52expect ( result ). toContain ( 'A' ); 53expect ( result ). toContain ( 'B' ); 54expect ( result ). toContain ( 'C' ); 55}); 56 57it ( 'strips an open/partial tool call block (no END marker)' , () => { 58const input = 'Lead text.' + OPEN_BLOCK ; 59const result = stripLegacyContextMarkers ( input ); 60 61expect ( result ). toBe ( 'Lead text.' ); 62expect ( result ). not . toContain ( '<<<' ); 63}); 64 65it ( 'does not alter content with no markers' , () => { 66const input = 'Just a normal assistant response.' ; 67 68expect ( stripLegacyContextMarkers ( input )). toBe ( input ); 69}); 70 71it ( 'strips reasoning block independently' , () => { 72const input = '<<<reasoning_content_start>>>think hard<<<reasoning_content_end>>>Answer.' ; 73 74expect ( stripLegacyContextMarkers ( input )). toBe ( 'Answer.' ); 75}); 76 77it ( 'strips both reasoning and agentic blocks together' , () => { 78const input = 79'<<<reasoning_content_start>>>plan<<<reasoning_content_end>>>' + 80'Some text.' + 81COMPLETE_BLOCK ; 82 83expect ( stripLegacyContextMarkers ( input )). not . toContain ( '<<<' ); 84expect ( stripLegacyContextMarkers ( input )). toContain ( 'Some text.' ); 85}); 86 87it ( 'empty string survives' , () => { 88expect ( stripLegacyContextMarkers ( '' )). toBe ( '' ); 89}); 90 91it ( 'detects legacy markers' , () => { 92expect ( LEGACY_AGENTIC_REGEX . HAS_LEGACY_MARKERS . test ( 'normal text' )). toBe ( false ); 93expect ( 94LEGACY_AGENTIC_REGEX . HAS_LEGACY_MARKERS . test ( 'text<<<AGENTIC_TOOL_CALL_START>>>more' ) 95). toBe ( true ); 96expect ( LEGACY_AGENTIC_REGEX . HAS_LEGACY_MARKERS . test ( '<<<reasoning_content_start>>>think' )). toBe ( 97true 98); 99}); 100});