char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
521a64cd0
char/aliceai-t5-moe
1// Pins the contract that makes streaming cheap: updateMessageAtIndex mutates the 2// existing message object instead of replacing it. 3// 4// Replacing it (`{ ...old, ...updates }`) changes the array slot, which 5// invalidates every consumer that merely walks the list - ChatMessages' 6// `displayMessages` rebuilds entries for EVERY message in the conversation. That 7// made per-token cost scale with conversation length (1.26ms at 1 prior message 8// -> 3.07ms at 40). Mutating in place keeps it flat. 9 10import { MessageRole } from '$lib/enums' ; 11import { conversationsStore } from '$lib/stores/conversations/index.svelte' ; 12import type { DatabaseMessage } from '$lib/types' ; 13import { describe , expect , it } from 'vitest' ; 14 15function makeMessage ( id :string ) :DatabaseMessage { 16return { 17children :[], 18content :'' , 19convId :'c1' , 20 id, 21parent :null , 22role :MessageRole . ASSISTANT , 23timestamp :0 , 24type :'text' 25} as DatabaseMessage ; 26} 27 28describe ( 'conversationsStore.updateMessageAtIndex' , () => { 29it ( 'mutates in place, preserving object identity' , () => { 30conversationsStore . activeMessages = [ makeMessage ( 'a' ), makeMessage ( 'b' )]; 31const before = conversationsStore . activeMessages [ 1 ]; 32 33conversationsStore . updateMessageAtIndex ( 1 , { content :'hello' }); 34 35expect ( conversationsStore . activeMessages [ 1 ]. content ). toBe ( 'hello' ); 36expect ( conversationsStore . activeMessages [ 1 ]). toBe ( before ); 37 38conversationsStore . activeMessages = []; 39}); 40 41it ( 'leaves other messages and unrelated fields untouched' , () => { 42conversationsStore . activeMessages = [ makeMessage ( 'a' ), makeMessage ( 'b' )]; 43const untouched = conversationsStore . activeMessages [ 0 ]; 44 45conversationsStore . updateMessageAtIndex ( 1 , { content :'x' , model :'m1' }); 46 47expect ( conversationsStore . activeMessages [ 0 ]). toBe ( untouched ); 48expect ( conversationsStore . activeMessages [ 0 ]. content ). toBe ( '' ); 49expect ( conversationsStore . activeMessages [ 1 ]. model ). toBe ( 'm1' ); 50expect ( conversationsStore . activeMessages [ 1 ]. id ). toBe ( 'b' ); 51 52conversationsStore . activeMessages = []; 53}); 54 55it ( 'is a no-op for an index of -1 or out of range' , () => { 56conversationsStore . activeMessages = [ makeMessage ( 'a' )]; 57 58expect (() => conversationsStore . updateMessageAtIndex ( - 1 , { content :'x' })). not . toThrow (); 59expect (() => conversationsStore . updateMessageAtIndex ( 9 , { content :'x' })). not . toThrow (); 60expect ( conversationsStore . activeMessages [ 0 ]. content ). toBe ( '' ); 61 62conversationsStore . activeMessages = []; 63}); 64});