okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
dca9770ef
main
1import { tr , localeTag } from './i18n' ; 2import type { Task } from './tasks' ; 3import type { Session } from './types' ; 4import { loopEndNote } from './loop' ; 5import { sessionTitle } from './session-title' ; 6 7/** 8* "Continue in a new chat" on a task: the first message of the new chat. It carries what the task found, what it did and the files it made, in the 9* words the person reads, so the conversation can go on from the result (a finished task, or one that stopped and needs a person). 10*/ 11const when = ( iso ?:string ) => ( iso ?new Date ( iso ). toLocaleString ( localeTag (), { dateStyle :'medium' , timeStyle :'short' }) :'' ); 12 13/** The task's saved answer as its parts: the answer, the list of what it did, and the note that something needed a yes ("What it did" is how the host writes it, server/task-report.mjs). */ 14export function splitTaskOutput ( output :string ) :{ found :string ; did :string []; attention :string } { 15let text = String ( output ?? '' ). trim (); 16let attention = '' ; 17const note = / \n\n> ([\s\S]*)$ / . exec ( text ); 18if ( note ) { attention = note [ 1 ]. replace ( / \n> / g , '\n' ). trim (); text = text . slice ( 0 , note . index ). trim (); } 19const cut = / \n\n---\n\n\*\*What it did\*\*\n\n([\s\S]*)$ / . exec ( text ); 20const did = cut ?cut [ 1 ]. split ( '\n' ). map ( l => l . replace ( / ^-\s* / , '' ). trim ()). filter ( Boolean ) :[]; 21return { found :( cut ?text . slice ( 0 , cut . index ) :text ). trim (), did, attention}; 22} 23 24const MAX_STEPS = 10 , MAX_STEP = 160 ; 25 26export const taskHandoverTitle = ( task :Task ) => tr ( 'Continuing from the task “{0}”' , [ task . title ]); 27 28export function taskHandoverMessage ( task :Task ) :string { 29const failed = !! task . error ; 30const ran = when ( task . lastRunAt ); 31const head = `** ${ taskHandoverTitle ( task )} ** ${ ran ? ` ${ failed ? tr ( '(ran {0}, did not finish).' , [ ran ]) : tr ( '(ran {0}, finished).' , [ ran ])} ` : '' } ` ; 32const parts = [ head ]; 33if ( failed ) parts . push ( `** ${ tr ( 'What went wrong' )} **\n\n ${ String ( task . error ). trim ()} ` ); 34const { found, did, attention} = splitTaskOutput ( task . output ?? '' ); 35if ( found ) parts . push ( `** ${ tr ( 'What it found' )} **\n\n ${ found } ` ); 36if ( attention ) parts . push ( `** ${ tr ( 'Needs your attention' )} **\n\n ${ attention } ` ); 37if ( did . length ) { 38// the steps of a long run are many and long: the first ones, shortened 39const shown = did . slice ( 0 , MAX_STEPS ). map ( l => `- ${ l . length > MAX_STEP ? ` ${ l . slice ( 0 , MAX_STEP - 1 )} …` : l } ` ); 40if ( did . length > MAX_STEPS ) shown . push ( `- ${ tr ( '…and {0} more steps' , [ did . length - MAX_STEPS ])} ` ); 41parts . push ( `** ${ tr ( 'What it did' )} **\n\n ${ shown . join ( '\n' )} ` ); 42} 43const files = ( task . files ?? []). filter ( f => f && typeof f . path === 'string' && f . path ); 44if ( files . length ) parts . push ( `** ${ tr ( 'Files' )} **\n\n ${ files . map ( f => `- ${ f . path } ` ). join ( '\n' )} ` ); 45parts . push ( tr ( 'Please help me take it from here.' )); 46return parts . join ( '\n\n' ); 47} 48 49export const chatHandoverTitle = ( session :Pick < Session , 'title' | 'messages' | 'defaultTitle' >) => tr ( 'Continuing from the chat “{0}”' , [ sessionTitle ( session )]); 50 51/** 52* "Continue in a new chat" on a chat or a loop that went wrong (the board): what was asked last, what went wrong or what the last answer 53* was, and, for a loop, how it ended, so the new chat can take it from there. 54*/ 55export function chatHandoverMessage ( session :Session ) :string { 56const real = session . messages . filter ( m => ! m . local ); 57const answer = [ ...real ]. reverse (). find ( m => m . role === 'assistant' ); 58const asked = [ ...real ]. reverse (). find ( m => m . role === 'user' ); 59const parts = [ `** ${ chatHandoverTitle ( session )} **` ]; 60const loop = session . loop ; 61if ( loop ) { 62const ended = loop . endedWhy && loop . endedWhy !== 'stopped' && loop . endedWhy !== 'closed' ?`\n\n ${ loopEndNote ( loop . endedWhy , loop . answers )} ` :'' ; 63parts . push ( `** ${ tr ( 'The loop' )} **\n\n ${ loop . prompt } ${ ended } ` ); 64} else if ( asked ?. content ) parts . push ( `** ${ tr ( 'What I asked' )} **\n\n ${ asked . content . trim (). slice ( 0 , 1500 )} ` ); 65if ( answer ?. error ) parts . push ( `** ${ tr ( 'What went wrong' )} **\n\n ${ String ( answer . error ). trim ()} ` ); 66const said = ( answer ?. content ?? '' ). trim () || ( loop ?. lastText ?? '' ); 67if ( said ) parts . push ( `** ${ tr ( 'The last answer' )} **\n\n ${ said . slice ( 0 , 2000 )} ` ); 68parts . push ( tr ( 'Please help me take it from here.' )); 69return parts . join ( '\n\n' ); 70}