okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1// A message too large for the window, made small enough to send: the text as a file in the chat's folder, or a shortened preview. 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { existsSync , mkdtempSync , readFileSync , rmSync } from 'node:fs' ; 5import { tmpdir } from 'node:os' ; 6import { join } from 'node:path' ; 7import { prepareOversized } from '../server/oversize.mjs' ; 8 9const big = 'q=0x1f ok;' . repeat ( 11000 ); // 110,000 characters 10const engine = ( tokens ) => async () => ({ ok :true , json :async () => ({ input_tokens :tokens }) }); 11const instance = { port :18500 , key :'k' , apiModelId :'m' , context :32768 }; 12 13test ( 'file: the whole text is saved in the chat folder, the message names it and shows how it starts' , async ( t ) => { 14const root = mkdtempSync ( join ( tmpdir (), 'strata-oversize-' )); 15t . after (() => rmSync ( root , { recursive :true , force :true })); 16const r = await prepareOversized ({ mode :'file' , text :big , root, instance, fetchImpl :engine ( 36000 ), now :new Date ( 2026 , 9 , 22 , 14 , 5 , 9 ) }); 17assert . equal ( r . mode , 'file' ); 18assert . equal ( r . tokens , 36000 ); 19assert . equal ( r . path , join ( root , 'pasted-message-20261022-140509.txt' )); 20assert . equal ( readFileSync ( r . path , 'utf8' ), big , 'nothing is lost: the file holds the whole text' ); 21assert . ok ( r . message . includes ( r . path )); 22assert . ok ( r . message . length < 2000 , 'the message that is sent is short' ); 23assert . ok ( r . message . includes ( 'q=0x1f ok;q=0x1f ok;' ), 'and it shows how the text starts' ); 24// The same second twice never overwrites a file. 25await assert . rejects ( prepareOversized ({ mode :'file' , text :'other' , root, instance, fetchImpl :engine ( 5 ), now :new Date ( 2026 , 9 , 22 , 14 , 5 , 9 ) })); 26}); 27 28test ( 'file: a chat with no folder says so instead of guessing one' , async () => { 29await assert . rejects ( prepareOversized ({ mode :'file' , text :big , root :null , instance :null }), / no folder / ); 30}); 31 32test ( 'preview: the start and the end, cut to what the window can hold, using the engine\'s own count when it has one' , async () => { 33const r = await prepareOversized ({ mode :'preview' , text :big , root :null , instance, fetchImpl :engine ( 36000 ) }); 34assert . equal ( r . mode , 'preview' ); 35assert . ok ( r . message . length < big . length / 2 && r . message . length > 20000 , `kept a good part ( ${ r . message . length } )` ); 36assert . match ( r . message , / characters from the middle are left out because the message was too long to send / ); 37assert . ok ( r . message . startsWith ( 'q=0x1f ok;' ) && r . message . endsWith ( 'q=0x1f ok;' )); 38// Counted by the engine: 36,000 tokens for 110,000 characters is 3.05 characters a token; the kept text is about 70% of the room that is left. 39assert . ok ( r . kept < ( 32768 - 12000 - 4096 ) * 3.05 , 'the preview fits the window by the engine\'s own ratio' ); 40}); 41 42test ( 'without an engine to count, three characters a token is assumed (code and logs are denser than prose)' , async () => { 43const r = await prepareOversized ({ mode :'preview' , text :big , root :null , instance :null }); 44assert . equal ( r . tokens , null ); 45assert . ok ( r . message . length < big . length ); 46const down = await prepareOversized ({ mode :'preview' , text :big , root :null , instance, fetchImpl :async () => { throw new Error ( 'refused' ); } }); 47assert . equal ( down . tokens , null , 'a server that cannot be asked is not an error' ); 48assert . ok ( down . message . length < big . length ); 49}); 50 51test ( 'a short text is returned as it is' , async () => { 52const r = await prepareOversized ({ mode :'preview' , text :'short' , root :null , instance :null }); 53assert . equal ( r . message , 'short' ); 54}); 55 56test ( 'the host serves it at /api/chat/oversize, from the chat\'s own folder and the loaded model' , () => { 57const index = readFileSync ( new URL ( '../server/index.mjs' , import . meta. url ), 'utf8' ). replace ( / \r\n / g , '\n' ); 58const at = index . indexOf ( "path === '/api/chat/oversize'" ); 59assert . ok ( at > 0 ); 60const route = index . slice ( at , at + 900 ); 61assert . match ( route , / prepareOversized\( / ); 62assert . match ( route , / chatRoot\(String\(body\.sessionId / ); 63assert . match ( route , / native\.resolveInstance\(body\.nativeModelId / ); 64assert . ok ( ! existsSync ( '/nonexistent' ), 'sanity' ); 65});