okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ff18fbb86
main
1/** 2* What a task that ran as the agent shows in Tasks ("Last result"): the agent's own answer, then a short plain list of what it 3* did, then - if it hit something that needed a person's yes - exactly what that was. Pure, so it is tested without an agent. 4*/ 5const clip = ( value , max ) => { 6const text = String ( value ?? '' ). replace ( / \s +/ g , ' ' ). trim (); 7return text . length > max ?` ${ text . slice ( 0 , max - 1 )} …` :text ; 8}; 9 10/** One card as a line a person can read. */ 11export function cardLine ( card ) { 12const what = clip ( card . effect || card . preview || card . name , 140 ); 13const detail = card . preview && card . effect && card . preview !== card . effect ?` ( ${ clip ( card . preview , 100 )} )` :'' ; 14if ( card . state === 'denied' ) return `- Could not do this by itself: ${ what } ${ detail } . Nothing was changed.` ; 15if ( card . state === 'error' ) return `- Tried: ${ what } ${ detail } . It did not work.` ; 16return `- ${ what } ${ detail } ` ; 17} 18 19/** 20* @param { {text:string, cards:Array} } run 21* @returns { {text:string, attention:string|null, demo:boolean} } 22*/ 23export function taskReport ({ text, cards}) { 24const done = cards . filter ( c => c . state !== 'proposed' ); 25const refused = cards . filter ( c => c . state === 'denied' ); 26const lines = done . map ( cardLine ); 27const answer = String ( text ?? '' ). trim (); 28let out = answer || ( cards . length ?'The task ran.' :'The task ran and had nothing to report.' ); 29if ( lines . length ) out += `\n\n---\n\n**What it did**\n\n ${ lines . join ( '\n' )} ` ; 30const attention = refused . length 31 ?`This task wanted to change something and needed a yes, which nobody could give while it ran by itself: ${ refused . map ( c => clip ( c . effect || c . name , 100 )). join ( '; ' )} . Nothing was changed. Edit the task and let it make changes if you want it to.` 32 :null ; 33if ( attention ) out += `\n\n> ${ attention } ` ; 34return { text :out , attention, demo :false }; 35} 36 37/** 38* One plain line for a board card: the first sentence of the answer, with the markdown taken off and the "What it did" list and the 39* "needed a yes" note left out. '' when there is nothing to say. 40*/ 41export function lastLine ( text , max = 160 ) { 42let body = String ( text ?? '' ). split ( / \n\n---\n\n / )[ 0 ]. replace ( / \n\n> [ \s\S ] * $ / , '' ); 43body = body . replace ( / ``` [ \s\S ] * ?``` / g , ' ' ). replace ( / ! ? \[ ([ ^ \] ] * ) \]\( [ ^ ) ] * \) / g , '$1' ). replace ( / ^\s * ( # { 1 , 6 } | [ -*+ ] | \d + [ .) ] | > ) \s +/ gm , '' ). replace ( / [ *_`~ ] / g , '' ); 44body = body . replace ( / \s +/ g , ' ' ). trim (); 45if ( ! body ) return '' ; 46const sentence = / ^. * ? [ .!? ](? = \s | $ ) / . exec ( body )?.[ 0 ] ?? body ; 47// A very short first sentence ("Done.") says nothing: take the next one too. 48const two = / ^. * ? [ .!? ](? = \s | $ ) \s * . * ? [ .!? ](? = \s | $ ) / . exec ( body )?.[ 0 ] ?? body ; 49const pick = sentence . length < 24 && body . length > sentence . length ?two :sentence ; 50return clip ( pick , max ); 51}