2023-12-02 19:04:51 +01:00
import { getStringHash , debounce , waitUntilCondition , extractAllWords } from '../../utils.js' ;
2024-03-31 13:27:43 +02:00
import { getContext , getApiUrl , extension _settings , doExtrasFetch , modules , renderExtensionTemplate } from '../../extensions.js' ;
2024-03-23 18:18:43 +01:00
import { animation _duration , eventSource , event _types , extension _prompt _roles , extension _prompt _types , generateQuietPrompt , is _send _press , saveSettingsDebounced , substituteParams } from '../../../script.js' ;
2023-12-02 19:04:51 +01:00
import { is _group _generating , selected _group } from '../../group-chats.js' ;
import { registerSlashCommand } from '../../slash-commands.js' ;
2023-10-09 21:49:35 +02:00
import { loadMovingUIState } from '../../power-user.js' ;
2023-12-02 19:04:51 +01:00
import { dragElement } from '../../RossAscends-mods.js' ;
2023-12-20 00:56:35 +01:00
import { getTextTokens , tokenizers } from '../../tokenizers.js' ;
2023-07-20 19:32:15 +02:00
export { MODULE _NAME } ;
const MODULE _NAME = '1_memory' ;
let lastCharacterId = null ;
let lastGroupId = null ;
let lastChatId = null ;
let lastMessageHash = null ;
let lastMessageId = null ;
let inApiCall = false ;
2023-07-30 22:10:37 +02:00
const formatMemoryValue = function ( value ) {
if ( ! value ) {
return '' ;
}
value = value . trim ( ) ;
if ( extension _settings . memory . template ) {
let result = extension _settings . memory . template . replace ( /{{summary}}/i , value ) ;
return substituteParams ( result ) ;
} else {
return ` Summary: ${ value } ` ;
}
2023-12-02 20:11:06 +01:00
} ;
2023-07-30 22:10:37 +02:00
2023-07-20 19:32:15 +02:00
const saveChatDebounced = debounce ( ( ) => getContext ( ) . saveChat ( ) , 2000 ) ;
const summary _sources = {
'extras' : 'extras' ,
'main' : 'main' ,
} ;
const defaultPrompt = '[Pause your roleplay. Summarize the most important facts and events that have happened in the chat so far. If a summary already exists in your memory, use that as a base and expand with new facts. Limit the summary to {{words}} words or less. Your response should include nothing but the summary.]' ;
2023-07-30 22:10:37 +02:00
const defaultTemplate = '[Summary: {{summary}}]' ;
2023-07-20 19:32:15 +02:00
const defaultSettings = {
memoryFrozen : false ,
2023-10-11 12:44:22 +02:00
SkipWIAN : false ,
2023-07-20 19:32:15 +02:00
source : summary _sources . extras ,
prompt : defaultPrompt ,
2023-07-30 22:10:37 +02:00
template : defaultTemplate ,
2023-09-08 00:26:26 +02:00
position : extension _prompt _types . IN _PROMPT ,
2024-03-23 18:18:43 +01:00
role : extension _prompt _roles . SYSTEM ,
2023-07-30 22:10:37 +02:00
depth : 2 ,
2023-07-20 19:32:15 +02:00
promptWords : 200 ,
promptMinWords : 25 ,
promptMaxWords : 1000 ,
promptWordsStep : 25 ,
promptInterval : 10 ,
2023-08-21 22:56:22 +02:00
promptMinInterval : 0 ,
2023-07-20 19:32:15 +02:00
promptMaxInterval : 100 ,
promptIntervalStep : 1 ,
2023-07-30 22:10:37 +02:00
promptForceWords : 0 ,
promptForceWordsStep : 100 ,
promptMinForceWords : 0 ,
promptMaxForceWords : 10000 ,
2023-07-20 19:32:15 +02:00
} ;
function loadSettings ( ) {
if ( Object . keys ( extension _settings . memory ) . length === 0 ) {
Object . assign ( extension _settings . memory , defaultSettings ) ;
}
2023-07-30 22:10:37 +02:00
for ( const key of Object . keys ( defaultSettings ) ) {
if ( extension _settings . memory [ key ] === undefined ) {
extension _settings . memory [ key ] = defaultSettings [ key ] ;
}
2023-07-20 19:32:15 +02:00
}
$ ( '#summary_source' ) . val ( extension _settings . memory . source ) . trigger ( 'change' ) ;
$ ( '#memory_frozen' ) . prop ( 'checked' , extension _settings . memory . memoryFrozen ) . trigger ( 'input' ) ;
2023-10-11 12:44:22 +02:00
$ ( '#memory_skipWIAN' ) . prop ( 'checked' , extension _settings . memory . SkipWIAN ) . trigger ( 'input' ) ;
2023-07-20 19:32:15 +02:00
$ ( '#memory_prompt' ) . val ( extension _settings . memory . prompt ) . trigger ( 'input' ) ;
$ ( '#memory_prompt_words' ) . val ( extension _settings . memory . promptWords ) . trigger ( 'input' ) ;
$ ( '#memory_prompt_interval' ) . val ( extension _settings . memory . promptInterval ) . trigger ( 'input' ) ;
2023-07-30 22:10:37 +02:00
$ ( '#memory_template' ) . val ( extension _settings . memory . template ) . trigger ( 'input' ) ;
$ ( '#memory_depth' ) . val ( extension _settings . memory . depth ) . trigger ( 'input' ) ;
2024-03-23 18:18:43 +01:00
$ ( '#memory_role' ) . val ( extension _settings . memory . role ) . trigger ( 'input' ) ;
2023-07-30 22:10:37 +02:00
$ ( ` input[name="memory_position"][value=" ${ extension _settings . memory . position } "] ` ) . prop ( 'checked' , true ) . trigger ( 'input' ) ;
$ ( '#memory_prompt_words_force' ) . val ( extension _settings . memory . promptForceWords ) . trigger ( 'input' ) ;
2023-12-15 20:35:25 +01:00
switchSourceControls ( extension _settings . memory . source ) ;
2023-07-20 19:32:15 +02:00
}
function onSummarySourceChange ( event ) {
const value = event . target . value ;
extension _settings . memory . source = value ;
2023-12-15 20:35:25 +01:00
switchSourceControls ( value ) ;
saveSettingsDebounced ( ) ;
}
function switchSourceControls ( value ) {
2024-03-31 13:27:43 +02:00
$ ( '#memory_settings [data-summary-source]' ) . each ( ( _ , element ) => {
const source = $ ( element ) . data ( 'summary-source' ) ;
2023-07-20 19:32:15 +02:00
$ ( element ) . toggle ( source === value ) ;
} ) ;
}
function onMemoryFrozenInput ( ) {
const value = Boolean ( $ ( this ) . prop ( 'checked' ) ) ;
extension _settings . memory . memoryFrozen = value ;
saveSettingsDebounced ( ) ;
}
2023-10-11 12:44:22 +02:00
function onMemorySkipWIANInput ( ) {
const value = Boolean ( $ ( this ) . prop ( 'checked' ) ) ;
extension _settings . memory . SkipWIAN = value ;
saveSettingsDebounced ( ) ;
}
2023-07-20 19:32:15 +02:00
function onMemoryPromptWordsInput ( ) {
const value = $ ( this ) . val ( ) ;
extension _settings . memory . promptWords = Number ( value ) ;
$ ( '#memory_prompt_words_value' ) . text ( extension _settings . memory . promptWords ) ;
saveSettingsDebounced ( ) ;
}
function onMemoryPromptIntervalInput ( ) {
const value = $ ( this ) . val ( ) ;
extension _settings . memory . promptInterval = Number ( value ) ;
$ ( '#memory_prompt_interval_value' ) . text ( extension _settings . memory . promptInterval ) ;
saveSettingsDebounced ( ) ;
}
function onMemoryPromptInput ( ) {
const value = $ ( this ) . val ( ) ;
extension _settings . memory . prompt = value ;
saveSettingsDebounced ( ) ;
}
2023-07-30 22:10:37 +02:00
function onMemoryTemplateInput ( ) {
const value = $ ( this ) . val ( ) ;
extension _settings . memory . template = value ;
2023-09-21 20:13:24 +02:00
reinsertMemory ( ) ;
2023-07-30 22:10:37 +02:00
saveSettingsDebounced ( ) ;
}
function onMemoryDepthInput ( ) {
const value = $ ( this ) . val ( ) ;
extension _settings . memory . depth = Number ( value ) ;
2023-09-21 20:13:24 +02:00
reinsertMemory ( ) ;
2023-07-30 22:10:37 +02:00
saveSettingsDebounced ( ) ;
}
2024-03-23 18:18:43 +01:00
function onMemoryRoleInput ( ) {
const value = $ ( this ) . val ( ) ;
extension _settings . memory . role = Number ( value ) ;
reinsertMemory ( ) ;
saveSettingsDebounced ( ) ;
}
2023-07-30 22:10:37 +02:00
function onMemoryPositionChange ( e ) {
const value = e . target . value ;
extension _settings . memory . position = value ;
2023-09-21 20:13:24 +02:00
reinsertMemory ( ) ;
2023-07-30 22:10:37 +02:00
saveSettingsDebounced ( ) ;
}
function onMemoryPromptWordsForceInput ( ) {
const value = $ ( this ) . val ( ) ;
extension _settings . memory . promptForceWords = Number ( value ) ;
$ ( '#memory_prompt_words_force_value' ) . text ( extension _settings . memory . promptForceWords ) ;
saveSettingsDebounced ( ) ;
}
2023-07-20 19:32:15 +02:00
function saveLastValues ( ) {
const context = getContext ( ) ;
lastGroupId = context . groupId ;
lastCharacterId = context . characterId ;
lastChatId = context . chatId ;
lastMessageId = context . chat ? . length ? ? null ;
lastMessageHash = getStringHash ( ( context . chat . length && context . chat [ context . chat . length - 1 ] [ 'mes' ] ) ? ? '' ) ;
}
function getLatestMemoryFromChat ( chat ) {
if ( ! Array . isArray ( chat ) || ! chat . length ) {
return '' ;
}
const reversedChat = chat . slice ( ) . reverse ( ) ;
reversedChat . shift ( ) ;
for ( let mes of reversedChat ) {
if ( mes . extra && mes . extra . memory ) {
return mes . extra . memory ;
}
}
return '' ;
}
async function onChatEvent ( ) {
// Module not enabled
if ( extension _settings . memory . source === summary _sources . extras ) {
if ( ! modules . includes ( 'summarize' ) ) {
return ;
}
}
const context = getContext ( ) ;
const chat = context . chat ;
// no characters or group selected
if ( ! context . groupId && context . characterId === undefined ) {
return ;
}
// Generation is in progress, summary prevented
if ( is _send _press ) {
return ;
}
// Chat/character/group changed
if ( ( context . groupId && lastGroupId !== context . groupId ) || ( context . characterId !== lastCharacterId ) || ( context . chatId !== lastChatId ) ) {
const latestMemory = getLatestMemoryFromChat ( chat ) ;
setMemoryContext ( latestMemory , false ) ;
saveLastValues ( ) ;
return ;
}
// Currently summarizing or frozen state - skip
if ( inApiCall || extension _settings . memory . memoryFrozen ) {
return ;
}
// No new messages - do nothing
if ( chat . length === 0 || ( lastMessageId === chat . length && getStringHash ( chat [ chat . length - 1 ] . mes ) === lastMessageHash ) ) {
return ;
}
// Messages has been deleted - rewrite the context with the latest available memory
if ( chat . length < lastMessageId ) {
const latestMemory = getLatestMemoryFromChat ( chat ) ;
setMemoryContext ( latestMemory , false ) ;
}
// Message has been edited / regenerated - delete the saved memory
if ( chat . length
&& chat [ chat . length - 1 ] . extra
&& chat [ chat . length - 1 ] . extra . memory
&& lastMessageId === chat . length
&& getStringHash ( chat [ chat . length - 1 ] . mes ) !== lastMessageHash ) {
delete chat [ chat . length - 1 ] . extra . memory ;
}
try {
await summarizeChat ( context ) ;
}
catch ( error ) {
console . log ( error ) ;
}
finally {
saveLastValues ( ) ;
}
}
async function forceSummarizeChat ( ) {
2023-12-16 21:33:09 +01:00
if ( extension _settings . memory . source === summary _sources . extras ) {
toastr . warning ( 'Force summarization is not supported for Extras API' ) ;
return ;
}
2023-07-20 19:32:15 +02:00
const context = getContext ( ) ;
2023-12-02 20:11:06 +01:00
const skipWIAN = extension _settings . memory . SkipWIAN ;
console . log ( ` Skipping WIAN? ${ skipWIAN } ` ) ;
2023-07-20 19:32:15 +02:00
if ( ! context . chatId ) {
toastr . warning ( 'No chat selected' ) ;
return ;
}
toastr . info ( 'Summarizing chat...' , 'Please wait' ) ;
2023-10-11 12:44:22 +02:00
const value = await summarizeChatMain ( context , true , skipWIAN ) ;
2023-07-20 19:32:15 +02:00
if ( ! value ) {
toastr . warning ( 'Failed to summarize chat' ) ;
return ;
}
}
async function summarizeChat ( context ) {
2023-12-02 20:11:06 +01:00
const skipWIAN = extension _settings . memory . SkipWIAN ;
2023-07-20 19:32:15 +02:00
switch ( extension _settings . memory . source ) {
case summary _sources . extras :
await summarizeChatExtras ( context ) ;
break ;
case summary _sources . main :
2023-10-11 12:44:22 +02:00
await summarizeChatMain ( context , false , skipWIAN ) ;
2023-07-20 19:32:15 +02:00
break ;
default :
break ;
}
}
2023-10-11 12:44:22 +02:00
async function summarizeChatMain ( context , force , skipWIAN ) {
2023-08-21 22:56:22 +02:00
if ( extension _settings . memory . promptInterval === 0 && ! force ) {
console . debug ( 'Prompt interval is set to 0, skipping summarization' ) ;
return ;
}
2023-07-20 19:32:15 +02:00
try {
2023-08-03 16:17:58 +02:00
// Wait for group to finish generating
if ( selected _group ) {
await waitUntilCondition ( ( ) => is _group _generating === false , 1000 , 10 ) ;
}
2023-07-20 19:32:15 +02:00
// Wait for the send button to be released
2023-08-01 14:53:10 +02:00
waitUntilCondition ( ( ) => is _send _press === false , 30000 , 100 ) ;
2023-07-20 19:32:15 +02:00
} catch {
console . debug ( 'Timeout waiting for is_send_press' ) ;
return ;
}
if ( ! context . chat . length ) {
console . debug ( 'No messages in chat to summarize' ) ;
return ;
}
if ( context . chat . length < extension _settings . memory . promptInterval && ! force ) {
console . debug ( ` Not enough messages in chat to summarize (chat: ${ context . chat . length } , interval: ${ extension _settings . memory . promptInterval } ) ` ) ;
return ;
}
let messagesSinceLastSummary = 0 ;
2023-07-30 22:10:37 +02:00
let wordsSinceLastSummary = 0 ;
let conditionSatisfied = false ;
2023-07-20 19:32:15 +02:00
for ( let i = context . chat . length - 1 ; i >= 0 ; i -- ) {
if ( context . chat [ i ] . extra && context . chat [ i ] . extra . memory ) {
break ;
}
messagesSinceLastSummary ++ ;
2023-07-30 22:10:37 +02:00
wordsSinceLastSummary += extractAllWords ( context . chat [ i ] . mes ) . length ;
}
if ( messagesSinceLastSummary >= extension _settings . memory . promptInterval ) {
conditionSatisfied = true ;
}
if ( extension _settings . memory . promptForceWords && wordsSinceLastSummary >= extension _settings . memory . promptForceWords ) {
conditionSatisfied = true ;
2023-07-20 19:32:15 +02:00
}
2023-07-30 22:10:37 +02:00
if ( ! conditionSatisfied && ! force ) {
console . debug ( ` Summary conditions not satisfied (messages: ${ messagesSinceLastSummary } , interval: ${ extension _settings . memory . promptInterval } , words: ${ wordsSinceLastSummary } , force words: ${ extension _settings . memory . promptForceWords } ) ` ) ;
2023-07-20 19:32:15 +02:00
return ;
}
2023-07-30 22:10:37 +02:00
console . log ( 'Summarizing chat, messages since last summary: ' + messagesSinceLastSummary , 'words since last summary: ' + wordsSinceLastSummary ) ;
2023-08-20 20:37:17 +02:00
const prompt = extension _settings . memory . prompt ? . replace ( /{{words}}/gi , extension _settings . memory . promptWords ) ;
2023-07-20 19:32:15 +02:00
if ( ! prompt ) {
console . debug ( 'Summarization prompt is empty. Skipping summarization.' ) ;
return ;
}
2023-12-02 20:11:06 +01:00
console . log ( 'sending summary prompt' ) ;
2023-10-11 12:44:22 +02:00
const summary = await generateQuietPrompt ( prompt , false , skipWIAN ) ;
2023-07-20 19:32:15 +02:00
const newContext = getContext ( ) ;
// something changed during summarization request
if ( newContext . groupId !== context . groupId
|| newContext . chatId !== context . chatId
|| ( ! newContext . groupId && ( newContext . characterId !== context . characterId ) ) ) {
console . log ( 'Context changed, summary discarded' ) ;
return ;
}
setMemoryContext ( summary , true ) ;
return summary ;
}
async function summarizeChatExtras ( context ) {
function getMemoryString ( ) {
return ( longMemory + '\n\n' + memoryBuffer . slice ( ) . reverse ( ) . join ( '\n\n' ) ) . trim ( ) ;
}
const chat = context . chat ;
const longMemory = getLatestMemoryFromChat ( chat ) ;
const reversedChat = chat . slice ( ) . reverse ( ) ;
reversedChat . shift ( ) ;
2023-12-20 00:56:35 +01:00
const memoryBuffer = [ ] ;
const CONTEXT _SIZE = 1024 - 64 ;
2023-07-20 19:32:15 +02:00
2023-12-20 00:56:35 +01:00
for ( const message of reversedChat ) {
2023-07-20 19:32:15 +02:00
// we reached the point of latest memory
2023-12-20 00:56:35 +01:00
if ( longMemory && message . extra && message . extra . memory == longMemory ) {
2023-07-20 19:32:15 +02:00
break ;
}
// don't care about system
2023-12-20 00:56:35 +01:00
if ( message . is _system ) {
2023-07-20 19:32:15 +02:00
continue ;
}
// determine the sender's name
2023-12-20 00:56:35 +01:00
const entry = ` ${ message . name } : \n ${ message . mes } ` ;
2023-07-20 19:32:15 +02:00
memoryBuffer . push ( entry ) ;
// check if token limit was reached
2023-12-20 00:56:35 +01:00
const tokens = getTextTokens ( tokenizers . GPT2 , getMemoryString ( ) ) . length ;
if ( tokens >= CONTEXT _SIZE ) {
2023-07-20 19:32:15 +02:00
break ;
}
}
const resultingString = getMemoryString ( ) ;
2023-12-20 00:56:35 +01:00
const resultingTokens = getTextTokens ( tokenizers . GPT2 , resultingString ) . length ;
2023-07-20 19:32:15 +02:00
2023-12-20 00:56:35 +01:00
if ( ! resultingString || resultingTokens < CONTEXT _SIZE ) {
console . debug ( 'Not enough context to summarize' ) ;
2023-07-20 19:32:15 +02:00
return ;
}
// perform the summarization API call
try {
inApiCall = true ;
const url = new URL ( getApiUrl ( ) ) ;
url . pathname = '/api/summarize' ;
const apiResult = await doExtrasFetch ( url , {
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
'Bypass-Tunnel-Reminder' : 'bypass' ,
} ,
body : JSON . stringify ( {
text : resultingString ,
2023-12-20 00:56:35 +01:00
params : { } ,
2023-12-02 21:06:57 +01:00
} ) ,
2023-07-20 19:32:15 +02:00
} ) ;
if ( apiResult . ok ) {
const data = await apiResult . json ( ) ;
const summary = data . summary ;
const newContext = getContext ( ) ;
// something changed during summarization request
if ( newContext . groupId !== context . groupId
|| newContext . chatId !== context . chatId
|| ( ! newContext . groupId && ( newContext . characterId !== context . characterId ) ) ) {
console . log ( 'Context changed, summary discarded' ) ;
return ;
}
setMemoryContext ( summary , true ) ;
}
}
catch ( error ) {
console . log ( error ) ;
}
finally {
inApiCall = false ;
}
}
function onMemoryRestoreClick ( ) {
const context = getContext ( ) ;
const content = $ ( '#memory_contents' ) . val ( ) ;
const reversedChat = context . chat . slice ( ) . reverse ( ) ;
reversedChat . shift ( ) ;
for ( let mes of reversedChat ) {
if ( mes . extra && mes . extra . memory == content ) {
delete mes . extra . memory ;
break ;
}
}
const newContent = getLatestMemoryFromChat ( context . chat ) ;
setMemoryContext ( newContent , false ) ;
}
function onMemoryContentInput ( ) {
const value = $ ( this ) . val ( ) ;
setMemoryContext ( value , true ) ;
}
2023-09-21 20:13:24 +02:00
function reinsertMemory ( ) {
const existingValue = $ ( '#memory_contents' ) . val ( ) ;
setMemoryContext ( existingValue , false ) ;
}
2023-07-20 19:32:15 +02:00
function setMemoryContext ( value , saveToMessage ) {
const context = getContext ( ) ;
2024-03-23 18:18:43 +01:00
context . setExtensionPrompt ( MODULE _NAME , formatMemoryValue ( value ) , extension _settings . memory . position , extension _settings . memory . depth , false , extension _settings . memory . role ) ;
2023-07-20 19:32:15 +02:00
$ ( '#memory_contents' ) . val ( value ) ;
2023-07-30 22:10:37 +02:00
console . log ( 'Summary set to: ' + value ) ;
console . debug ( 'Position: ' + extension _settings . memory . position ) ;
console . debug ( 'Depth: ' + extension _settings . memory . depth ) ;
2024-03-23 18:18:43 +01:00
console . debug ( 'Role: ' + extension _settings . memory . role ) ;
2023-07-20 19:32:15 +02:00
if ( saveToMessage && context . chat . length ) {
const idx = context . chat . length - 2 ;
const mes = context . chat [ idx < 0 ? 0 : idx ] ;
if ( ! mes . extra ) {
mes . extra = { } ;
}
mes . extra . memory = value ;
saveChatDebounced ( ) ;
}
}
2023-10-09 21:49:35 +02:00
function doPopout ( e ) {
const target = e . target ;
//repurposes the zoomed avatar template to server as a floating div
2023-12-02 19:04:51 +01:00
if ( $ ( '#summaryExtensionPopout' ) . length === 0 ) {
2023-12-02 20:11:06 +01:00
console . debug ( 'did not see popout yet, creating' ) ;
const originalHTMLClone = $ ( target ) . parent ( ) . parent ( ) . parent ( ) . find ( '.inline-drawer-content' ) . html ( ) ;
const originalElement = $ ( target ) . parent ( ) . parent ( ) . parent ( ) . find ( '.inline-drawer-content' ) ;
2023-10-09 21:49:35 +02:00
const template = $ ( '#zoomed_avatar_template' ) . html ( ) ;
const controlBarHtml = ` <div class="panelControlBar flex-container">
< div id = "summaryExtensionPopoutheader" class = "fa-solid fa-grip drag-grabber hoverglow" > < / d i v >
2023-10-13 17:29:41 +02:00
< div id = "summaryExtensionPopoutClose" class = "fa-solid fa-circle-xmark hoverglow dragClose" > < / d i v >
2023-12-02 20:11:06 +01:00
< / d i v > ` ;
2023-10-09 21:49:35 +02:00
const newElement = $ ( template ) ;
2023-10-13 17:29:41 +02:00
newElement . attr ( 'id' , 'summaryExtensionPopout' )
. removeClass ( 'zoomed_avatar' )
. addClass ( 'draggable' )
2023-12-02 20:11:06 +01:00
. empty ( ) ;
2023-10-09 21:49:35 +02:00
const prevSummaryBoxContents = $ ( '#memory_contents' ) . val ( ) ; //copy summary box before emptying
originalElement . empty ( ) ;
2023-12-02 20:11:06 +01:00
originalElement . html ( '<div class="flex-container alignitemscenter justifyCenter wide100p"><small>Currently popped out</small></div>' ) ;
newElement . append ( controlBarHtml ) . append ( originalHTMLClone ) ;
2023-10-09 21:49:35 +02:00
$ ( 'body' ) . append ( newElement ) ;
2023-12-02 20:11:06 +01:00
$ ( '#summaryExtensionDrawerContents' ) . addClass ( 'scrollableInnerFull' ) ;
2023-10-09 21:49:35 +02:00
setMemoryContext ( prevSummaryBoxContents , false ) ; //paste prev summary box contents into popout box
setupListeners ( ) ;
loadSettings ( ) ;
loadMovingUIState ( ) ;
2023-12-11 15:23:21 +01:00
$ ( '#summaryExtensionPopout' ) . fadeIn ( animation _duration ) ;
2023-10-09 21:49:35 +02:00
dragElement ( newElement ) ;
//setup listener for close button to restore extensions menu
$ ( '#summaryExtensionPopoutClose' ) . off ( 'click' ) . on ( 'click' , function ( ) {
2023-12-02 20:11:06 +01:00
$ ( '#summaryExtensionDrawerContents' ) . removeClass ( 'scrollableInnerFull' ) ;
const summaryPopoutHTML = $ ( '#summaryExtensionDrawerContents' ) ;
2023-12-11 15:23:21 +01:00
$ ( '#summaryExtensionPopout' ) . fadeOut ( animation _duration , ( ) => {
2023-10-09 21:49:35 +02:00
originalElement . empty ( ) ;
originalElement . html ( summaryPopoutHTML ) ;
2023-12-02 20:11:06 +01:00
$ ( '#summaryExtensionPopout' ) . remove ( ) ;
} ) ;
2023-10-09 21:49:35 +02:00
loadSettings ( ) ;
2023-12-02 20:11:06 +01:00
} ) ;
2023-10-09 21:49:35 +02:00
} else {
2023-12-02 20:11:06 +01:00
console . debug ( 'saw existing popout, removing' ) ;
2023-12-11 15:23:21 +01:00
$ ( '#summaryExtensionPopout' ) . fadeOut ( animation _duration , ( ) => { $ ( '#summaryExtensionPopoutClose' ) . trigger ( 'click' ) ; } ) ;
2023-10-09 21:49:35 +02:00
}
}
function setupListeners ( ) {
//setup shared listeners for popout and regular ext menu
$ ( '#memory_restore' ) . off ( 'click' ) . on ( 'click' , onMemoryRestoreClick ) ;
$ ( '#memory_contents' ) . off ( 'click' ) . on ( 'input' , onMemoryContentInput ) ;
$ ( '#memory_frozen' ) . off ( 'click' ) . on ( 'input' , onMemoryFrozenInput ) ;
2023-10-11 12:44:22 +02:00
$ ( '#memory_skipWIAN' ) . off ( 'click' ) . on ( 'input' , onMemorySkipWIANInput ) ;
2023-10-09 21:49:35 +02:00
$ ( '#summary_source' ) . off ( 'click' ) . on ( 'change' , onSummarySourceChange ) ;
$ ( '#memory_prompt_words' ) . off ( 'click' ) . on ( 'input' , onMemoryPromptWordsInput ) ;
$ ( '#memory_prompt_interval' ) . off ( 'click' ) . on ( 'input' , onMemoryPromptIntervalInput ) ;
$ ( '#memory_prompt' ) . off ( 'click' ) . on ( 'input' , onMemoryPromptInput ) ;
$ ( '#memory_force_summarize' ) . off ( 'click' ) . on ( 'click' , forceSummarizeChat ) ;
$ ( '#memory_template' ) . off ( 'click' ) . on ( 'input' , onMemoryTemplateInput ) ;
$ ( '#memory_depth' ) . off ( 'click' ) . on ( 'input' , onMemoryDepthInput ) ;
2024-03-23 18:18:43 +01:00
$ ( '#memory_role' ) . off ( 'click' ) . on ( 'input' , onMemoryRoleInput ) ;
2023-10-09 21:49:35 +02:00
$ ( 'input[name="memory_position"]' ) . off ( 'click' ) . on ( 'change' , onMemoryPositionChange ) ;
$ ( '#memory_prompt_words_force' ) . off ( 'click' ) . on ( 'input' , onMemoryPromptWordsForceInput ) ;
2023-12-02 19:04:51 +01:00
$ ( '#summarySettingsBlockToggle' ) . off ( 'click' ) . on ( 'click' , function ( ) {
2023-12-02 20:11:06 +01:00
console . log ( 'saw settings button click' ) ;
2023-12-02 19:04:51 +01:00
$ ( '#summarySettingsBlock' ) . slideToggle ( 200 , 'swing' ) ; //toggleClass("hidden");
2023-10-09 21:49:35 +02:00
} ) ;
}
2023-07-20 19:32:15 +02:00
jQuery ( function ( ) {
function addExtensionControls ( ) {
2024-03-31 13:27:43 +02:00
const settingsHtml = renderExtensionTemplate ( 'memory' , 'settings' , { defaultSettings } ) ;
2023-07-20 19:32:15 +02:00
$ ( '#extensions_settings2' ) . append ( settingsHtml ) ;
2023-10-09 21:49:35 +02:00
setupListeners ( ) ;
2023-12-02 19:04:51 +01:00
$ ( '#summaryExtensionPopoutButton' ) . off ( 'click' ) . on ( 'click' , function ( e ) {
2023-10-09 21:49:35 +02:00
doPopout ( e ) ;
e . stopPropagation ( ) ;
} ) ;
2023-07-20 19:32:15 +02:00
}
addExtensionControls ( ) ;
loadSettings ( ) ;
eventSource . on ( event _types . MESSAGE _RECEIVED , onChatEvent ) ;
eventSource . on ( event _types . MESSAGE _DELETED , onChatEvent ) ;
eventSource . on ( event _types . MESSAGE _EDITED , onChatEvent ) ;
eventSource . on ( event _types . MESSAGE _SWIPED , onChatEvent ) ;
eventSource . on ( event _types . CHAT _CHANGED , onChatEvent ) ;
2023-10-07 18:25:36 +02:00
registerSlashCommand ( 'summarize' , forceSummarizeChat , [ ] , '– forces the summarization of the current chat using the Main API' , true , true ) ;
2023-07-20 19:32:15 +02:00
} ) ;