2023-11-18 20:17:53 +01:00
import { callPopup , eventSource , event _types , saveSettings , saveSettingsDebounced , getRequestHeaders , substituteParams , renderTemplate , animation _duration } from "../script.js" ;
2023-10-31 21:16:33 +01:00
import { hideLoader , showLoader } from "./loader.js" ;
2023-08-25 19:34:26 +02:00
import { isSubsetOf } from "./utils.js" ;
2023-07-20 19:32:15 +02:00
export {
getContext ,
getApiUrl ,
loadExtensionSettings ,
runGenerationInterceptors ,
doExtrasFetch ,
modules ,
extension _settings ,
ModuleWorkerWrapper ,
} ;
2023-10-08 22:20:01 +02:00
export let extensionNames = [ ] ;
2023-08-22 21:45:12 +02:00
let manifests = { } ;
2023-07-20 19:32:15 +02:00
const defaultUrl = "http://localhost:5100" ;
2023-08-23 20:32:38 +02:00
let saveMetadataTimeout = null ;
export function saveMetadataDebounced ( ) {
const context = getContext ( ) ;
const groupId = context . groupId ;
const characterId = context . characterId ;
if ( saveMetadataTimeout ) {
console . debug ( 'Clearing save metadata timeout' ) ;
clearTimeout ( saveMetadataTimeout ) ;
}
saveMetadataTimeout = setTimeout ( async ( ) => {
const newContext = getContext ( ) ;
if ( groupId !== newContext . groupId ) {
console . warn ( 'Group changed, not saving metadata' ) ;
return ;
}
if ( characterId !== newContext . characterId ) {
console . warn ( 'Character changed, not saving metadata' ) ;
return ;
}
console . debug ( 'Saving metadata...' ) ;
newContext . saveMetadata ( ) ;
console . debug ( 'Saved metadata...' ) ;
} , 1000 ) ;
}
2023-07-20 19:32:15 +02:00
2023-08-22 16:46:37 +02:00
export const extensionsHandlebars = Handlebars . create ( ) ;
2023-08-25 19:34:26 +02:00
/ * *
* Provides an ability for extensions to render HTML templates .
* Templates sanitation and localization is forced .
* @ param { string } extensionName Extension name
* @ param { string } templateId Template ID
* @ param { object } templateData Additional data to pass to the template
* @ returns { string } Rendered HTML
* /
export function renderExtensionTemplate ( extensionName , templateId , templateData = { } , sanitize = true , localize = true ) {
return renderTemplate ( ` scripts/extensions/ ${ extensionName } / ${ templateId } .html ` , templateData , sanitize , localize , true ) ;
}
2023-08-22 16:46:37 +02:00
/ * *
* Registers a Handlebars helper for use in extensions .
* @ param { string } name Handlebars helper name
* @ param { function } helper Handlebars helper function
* /
export function registerExtensionHelper ( name , helper ) {
extensionsHandlebars . registerHelper ( name , helper ) ;
}
/ * *
* Applies handlebars extension helpers to a message .
* @ param { number } messageId Message index in the chat .
* /
2023-08-22 21:45:12 +02:00
export function processExtensionHelpers ( messageId ) {
2023-08-22 16:46:37 +02:00
const context = getContext ( ) ;
const message = context . chat [ messageId ] ;
if ( ! message ? . mes || typeof message . mes !== 'string' ) {
return ;
}
// Don't waste time if there are no mustaches
2023-08-22 21:45:12 +02:00
if ( ! substituteParams ( message . mes ) . includes ( '{{' ) ) {
2023-08-22 16:46:37 +02:00
return ;
}
try {
2023-08-22 21:45:12 +02:00
const template = extensionsHandlebars . compile ( substituteParams ( message . mes ) , { noEscape : true } ) ;
2023-08-22 16:46:37 +02:00
message . mes = template ( { } ) ;
} catch {
// Ignore
}
}
2023-07-20 19:32:15 +02:00
// Disables parallel updates
class ModuleWorkerWrapper {
constructor ( callback ) {
this . isBusy = false ;
this . callback = callback ;
}
// Called by the extension
2023-11-17 00:30:32 +01:00
async update ( ... args ) {
2023-07-20 19:32:15 +02:00
// Don't touch me I'm busy...
if ( this . isBusy ) {
return ;
}
// I'm free. Let's update!
try {
this . isBusy = true ;
2023-11-17 00:30:32 +01:00
await this . callback ( ... args ) ;
2023-07-20 19:32:15 +02:00
}
finally {
this . isBusy = false ;
}
}
}
const extension _settings = {
apiUrl : defaultUrl ,
apiKey : '' ,
autoConnect : false ,
2023-10-23 15:53:31 +02:00
notifyUpdates : false ,
2023-07-20 19:32:15 +02:00
disabledExtensions : [ ] ,
expressionOverrides : [ ] ,
memory : { } ,
note : {
default : '' ,
chara : [ ] ,
wiAddition : [ ] ,
} ,
caption : {
refine _mode : false ,
} ,
2023-09-14 20:30:02 +02:00
expressions : {
/** @type {string[]} */
custom : [ ] ,
} ,
2023-07-20 19:32:15 +02:00
dice : { } ,
regex : [ ] ,
tts : { } ,
2023-07-22 20:12:23 +02:00
sd : {
prompts : { } ,
2023-07-22 22:57:48 +02:00
character _prompts : { } ,
2023-07-22 20:12:23 +02:00
} ,
2023-07-20 19:32:15 +02:00
chromadb : { } ,
translate : { } ,
objective : { } ,
quickReply : { } ,
randomizer : {
controls : [ ] ,
fluctuation : 0.1 ,
enabled : false ,
} ,
2023-07-27 19:29:36 +02:00
speech _recognition : { } ,
2023-08-10 16:46:04 +02:00
rvc : { } ,
2023-09-07 23:28:06 +02:00
hypebot : { } ,
vectors : { } ,
2023-11-06 21:50:32 +01:00
variables : {
global : { } ,
} ,
2023-07-20 19:32:15 +02:00
} ;
let modules = [ ] ;
let activeExtensions = new Set ( ) ;
const getContext = ( ) => window [ 'SillyTavern' ] . getContext ( ) ;
const getApiUrl = ( ) => extension _settings . apiUrl ;
let connectedToApi = false ;
function showHideExtensionsMenu ( ) {
// Get the number of menu items that are not hidden
const hasMenuItems = $ ( '#extensionsMenu' ) . children ( ) . filter ( ( _ , child ) => $ ( child ) . css ( 'display' ) !== 'none' ) . length > 0 ;
// We have menu items, so we can stop checking
if ( hasMenuItems ) {
clearInterval ( menuInterval ) ;
}
// Show or hide the menu button
$ ( '#extensionsMenuButton' ) . toggle ( hasMenuItems ) ;
}
// Periodically check for new extensions
const menuInterval = setInterval ( showHideExtensionsMenu , 1000 ) ;
async function doExtrasFetch ( endpoint , args ) {
if ( ! args ) {
args = { }
}
if ( ! args . method ) {
Object . assign ( args , { method : 'GET' } ) ;
}
if ( ! args . headers ) {
args . headers = { }
}
Object . assign ( args . headers , {
'Authorization' : ` Bearer ${ extension _settings . apiKey } ` ,
} ) ;
const response = await fetch ( endpoint , args ) ;
return response ;
}
async function discoverExtensions ( ) {
try {
2023-09-16 15:16:48 +02:00
const response = await fetch ( '/api/extensions/discover' ) ;
2023-07-20 19:32:15 +02:00
if ( response . ok ) {
const extensions = await response . json ( ) ;
return extensions ;
}
else {
return [ ] ;
}
}
catch ( err ) {
console . error ( err ) ;
return [ ] ;
}
}
function onDisableExtensionClick ( ) {
const name = $ ( this ) . data ( 'name' ) ;
disableExtension ( name ) ;
}
function onEnableExtensionClick ( ) {
const name = $ ( this ) . data ( 'name' ) ;
enableExtension ( name ) ;
}
async function enableExtension ( name ) {
extension _settings . disabledExtensions = extension _settings . disabledExtensions . filter ( x => x !== name ) ;
await saveSettings ( ) ;
location . reload ( ) ;
}
async function disableExtension ( name ) {
extension _settings . disabledExtensions . push ( name ) ;
await saveSettings ( ) ;
location . reload ( ) ;
}
async function getManifests ( names ) {
const obj = { } ;
const promises = [ ] ;
for ( const name of names ) {
const promise = new Promise ( ( resolve , reject ) => {
fetch ( ` /scripts/extensions/ ${ name } /manifest.json ` ) . then ( async response => {
if ( response . ok ) {
const json = await response . json ( ) ;
obj [ name ] = json ;
resolve ( ) ;
} else {
reject ( ) ;
}
2023-08-22 21:45:12 +02:00
} ) . catch ( err => {
reject ( ) ;
console . log ( 'Could not load manifest.json for ' + name , err ) ;
} ) ;
2023-07-20 19:32:15 +02:00
} ) ;
promises . push ( promise ) ;
}
await Promise . allSettled ( promises ) ;
return obj ;
}
async function activateExtensions ( ) {
const extensions = Object . entries ( manifests ) . sort ( ( a , b ) => a [ 1 ] . loading _order - b [ 1 ] . loading _order ) ;
const promises = [ ] ;
for ( let entry of extensions ) {
const name = entry [ 0 ] ;
const manifest = entry [ 1 ] ;
const elementExists = document . getElementById ( name ) !== null ;
if ( elementExists || activeExtensions . has ( name ) ) {
continue ;
}
// all required modules are active (offline extensions require none)
if ( isSubsetOf ( modules , manifest . requires ) ) {
try {
const isDisabled = extension _settings . disabledExtensions . includes ( name ) ;
const li = document . createElement ( 'li' ) ;
if ( ! isDisabled ) {
const promise = Promise . all ( [ addExtensionScript ( name , manifest ) , addExtensionStyle ( name , manifest ) ] ) ;
promise
. then ( ( ) => activeExtensions . add ( name ) )
. catch ( err => console . log ( 'Could not activate extension: ' + name , err ) ) ;
promises . push ( promise ) ;
}
else {
li . classList . add ( 'disabled' ) ;
}
li . id = name ;
li . innerText = manifest . display _name ;
$ ( '#extensions_list' ) . append ( li ) ;
}
catch ( error ) {
console . error ( ` Could not activate extension: ${ name } ` ) ;
console . error ( error ) ;
}
}
}
await Promise . allSettled ( promises ) ;
}
async function connectClickHandler ( ) {
const baseUrl = $ ( "#extensions_url" ) . val ( ) ;
2023-08-22 21:45:12 +02:00
extension _settings . apiUrl = String ( baseUrl ) ;
2023-07-20 19:32:15 +02:00
const testApiKey = $ ( "#extensions_api_key" ) . val ( ) ;
2023-08-22 21:45:12 +02:00
extension _settings . apiKey = String ( testApiKey ) ;
2023-07-20 19:32:15 +02:00
saveSettingsDebounced ( ) ;
await connectToApi ( baseUrl ) ;
}
function autoConnectInputHandler ( ) {
const value = $ ( this ) . prop ( 'checked' ) ;
extension _settings . autoConnect = ! ! value ;
if ( value && ! connectedToApi ) {
$ ( "#extensions_connect" ) . trigger ( 'click' ) ;
}
saveSettingsDebounced ( ) ;
}
function addExtensionsButtonAndMenu ( ) {
const buttonHTML =
` <div id="extensionsMenuButton" style="display: none;" class="fa-solid fa-magic-wand-sparkles" title="Extras Extensions" /></div> ` ;
const extensionsMenuHTML = ` <div id="extensionsMenu" class="options-content" style="display: none;"></div> ` ;
$ ( document . body ) . append ( extensionsMenuHTML ) ;
2023-11-14 07:53:26 +01:00
$ ( '#leftSendForm' ) . prepend ( buttonHTML ) ;
2023-07-20 19:32:15 +02:00
const button = $ ( '#extensionsMenuButton' ) ;
const dropdown = $ ( '#extensionsMenu' ) ;
//dropdown.hide();
let popper = Popper . createPopper ( button . get ( 0 ) , dropdown . get ( 0 ) , {
2023-11-14 07:53:26 +01:00
placement : 'top-start' ,
2023-07-20 19:32:15 +02:00
} ) ;
$ ( button ) . on ( 'click' , function ( ) {
popper . update ( )
2023-11-18 20:17:53 +01:00
if ( ! dropdown . is ( ':visible' ) ) {
dropdown . fadeIn ( animation _duration ) ;
}
2023-07-20 19:32:15 +02:00
} ) ;
$ ( "html" ) . on ( 'touchstart mousedown' , function ( e ) {
2023-11-18 23:40:21 +01:00
const clickTarget = $ ( e . target ) ;
const noCloseTargets = [ '#sd_gen' ] ;
if ( dropdown . is ( ':visible' ) && ! noCloseTargets . some ( id => clickTarget . closest ( id ) . length > 0 ) ) {
2023-11-18 20:17:53 +01:00
$ ( dropdown ) . fadeOut ( animation _duration ) ;
2023-07-20 19:32:15 +02:00
}
} ) ;
}
2023-10-23 15:53:31 +02:00
function notifyUpdatesInputHandler ( ) {
extension _settings . notifyUpdates = ! ! $ ( '#extensions_notify_updates' ) . prop ( 'checked' ) ;
saveSettingsDebounced ( ) ;
if ( extension _settings . notifyUpdates ) {
checkForExtensionUpdates ( true ) ;
}
}
2023-07-20 19:32:15 +02:00
/ * $ ( d o c u m e n t ) . o n ( ' c l i c k ' , f u n c t i o n ( e ) {
const target = $ ( e . target ) ;
if ( target . is ( dropdown ) ) return ;
if ( target . is ( button ) && dropdown . is ( ':hidden' ) ) {
dropdown . toggle ( 200 ) ;
popper . update ( ) ;
}
if ( target !== dropdown &&
target !== button &&
dropdown . is ( ":visible" ) ) {
dropdown . hide ( 200 ) ;
}
} ) ;
} * /
async function connectToApi ( baseUrl ) {
if ( ! baseUrl ) {
return ;
}
const url = new URL ( baseUrl ) ;
url . pathname = '/api/modules' ;
try {
const getExtensionsResult = await doExtrasFetch ( url ) ;
if ( getExtensionsResult . ok ) {
const data = await getExtensionsResult . json ( ) ;
modules = data . modules ;
await activateExtensions ( ) ;
eventSource . emit ( event _types . EXTRAS _CONNECTED , modules ) ;
}
updateStatus ( getExtensionsResult . ok ) ;
}
catch {
updateStatus ( false ) ;
}
}
function updateStatus ( success ) {
connectedToApi = success ;
const _text = success ? 'Connected to API' : 'Could not connect to API' ;
const _class = success ? 'success' : 'failure' ;
$ ( '#extensions_status' ) . text ( _text ) ;
$ ( '#extensions_status' ) . attr ( 'class' , _class ) ;
}
function addExtensionStyle ( name , manifest ) {
if ( manifest . css ) {
return new Promise ( ( resolve , reject ) => {
const url = ` /scripts/extensions/ ${ name } / ${ manifest . css } ` ;
if ( $ ( ` link[id=" ${ name } "] ` ) . length === 0 ) {
const link = document . createElement ( 'link' ) ;
link . id = name ;
link . rel = "stylesheet" ;
link . type = "text/css" ;
link . href = url ;
link . onload = function ( ) {
resolve ( ) ;
}
link . onerror = function ( e ) {
reject ( e ) ;
}
document . head . appendChild ( link ) ;
}
} ) ;
}
return Promise . resolve ( ) ;
}
function addExtensionScript ( name , manifest ) {
if ( manifest . js ) {
return new Promise ( ( resolve , reject ) => {
const url = ` /scripts/extensions/ ${ name } / ${ manifest . js } ` ;
let ready = false ;
if ( $ ( ` script[id=" ${ name } "] ` ) . length === 0 ) {
const script = document . createElement ( 'script' ) ;
script . id = name ;
script . type = 'module' ;
script . src = url ;
script . async = true ;
script . onerror = function ( err ) {
reject ( err , script ) ;
} ;
script . onload = script . onreadystatechange = function ( ) {
// console.log(this.readyState); // uncomment this line to see which ready states are called.
if ( ! ready && ( ! this . readyState || this . readyState == 'complete' ) ) {
ready = true ;
resolve ( ) ;
}
} ;
document . body . appendChild ( script ) ;
}
} ) ;
}
return Promise . resolve ( ) ;
}
/ * *
* Generates HTML string for displaying an extension in the UI .
*
* @ param { string } name - The name of the extension .
* @ param { object } manifest - The manifest of the extension .
* @ param { boolean } isActive - Whether the extension is active or not .
* @ param { boolean } isDisabled - Whether the extension is disabled or not .
* @ param { boolean } isExternal - Whether the extension is external or not .
* @ param { string } checkboxClass - The class for the checkbox HTML element .
* @ return { string } - The HTML string that represents the extension .
* /
async function generateExtensionHtml ( name , manifest , isActive , isDisabled , isExternal , checkboxClass ) {
const displayName = manifest . display _name ;
let displayVersion = manifest . version ? ` v ${ manifest . version } ` : "" ;
let isUpToDate = true ;
let updateButton = '' ;
let originHtml = '' ;
if ( isExternal ) {
let data = await getExtensionVersion ( name . replace ( 'third-party' , '' ) ) ;
let branch = data . currentBranchName ;
let commitHash = data . currentCommitHash ;
let origin = data . remoteUrl
isUpToDate = data . isUpToDate ;
displayVersion = ` ( ${ branch } - ${ commitHash . substring ( 0 , 7 ) } ) ` ;
updateButton = isUpToDate ?
2023-11-19 00:33:54 +01:00
` <span class="update-button"><button class="btn_update menu_button" data-name=" ${ name . replace ( 'third-party' , '' ) } " title="Up to date"><i class="fa-solid fa-code-commit fa-fw"></i></button></span> ` :
` <span class="update-button"><button class="btn_update menu_button" data-name=" ${ name . replace ( 'third-party' , '' ) } " title="Update available"><i class="fa-solid fa-download fa-fw"></i></button></span> ` ;
2023-07-20 19:32:15 +02:00
originHtml = ` <a href=" ${ origin } " target="_blank" rel="noopener noreferrer"> ` ;
}
let toggleElement = isActive || isDisabled ?
` <input type="checkbox" title="Click to toggle" data-name=" ${ name } " class=" ${ isActive ? 'toggle_disable' : 'toggle_enable' } ${ checkboxClass } " ${ isActive ? 'checked' : '' } > ` :
` <input type="checkbox" title="Cannot enable extension" data-name=" ${ name } " class="extension_missing ${ checkboxClass } " disabled> ` ;
let deleteButton = isExternal ? ` <span class="delete-button"><button class="btn_delete menu_button" data-name=" ${ name . replace ( 'third-party' , '' ) } " title="Delete"><i class="fa-solid fa-trash-can"></i></button></span> ` : '' ;
// if external, wrap the name in a link to the repo
let extensionHtml = ` <hr>
< h4 >
$ { updateButton }
$ { deleteButton }
$ { originHtml }
< span class = "${isActive ? " extension _enabled " : isDisabled ? " extension _disabled " : " extension _missing "}" >
$ { DOMPurify . sanitize ( displayName ) } $ { displayVersion }
< / s p a n >
$ { isExternal ? '</a>' : '' }
< span style = "float:right;" > $ { toggleElement } < / s p a n >
< / h 4 > ` ;
if ( isActive && Array . isArray ( manifest . optional ) ) {
const optional = new Set ( manifest . optional ) ;
modules . forEach ( x => optional . delete ( x ) ) ;
if ( optional . size > 0 ) {
const optionalString = DOMPurify . sanitize ( [ ... optional ] . join ( ', ' ) ) ;
extensionHtml += ` <p>Optional modules: <span class="optional"> ${ optionalString } </span></p> ` ;
}
} else if ( ! isDisabled ) { // Neither active nor disabled
const requirements = new Set ( manifest . requires ) ;
modules . forEach ( x => requirements . delete ( x ) ) ;
const requirementsString = DOMPurify . sanitize ( [ ... requirements ] . join ( ', ' ) ) ;
extensionHtml += ` <p>Missing modules: <span class="failure"> ${ requirementsString } </span></p> ` ;
}
return extensionHtml ;
}
/ * *
* Gets extension data and generates the corresponding HTML for displaying the extension .
*
* @ param { Array } extension - An array where the first element is the extension name and the second element is the extension manifest .
2023-08-22 21:45:12 +02:00
* @ return { Promise < object > } - An object with 'isExternal' indicating whether the extension is external , and 'extensionHtml' for the extension ' s HTML string .
2023-07-20 19:32:15 +02:00
* /
async function getExtensionData ( extension ) {
const name = extension [ 0 ] ;
const manifest = extension [ 1 ] ;
const isActive = activeExtensions . has ( name ) ;
const isDisabled = extension _settings . disabledExtensions . includes ( name ) ;
const isExternal = name . startsWith ( 'third-party' ) ;
const checkboxClass = isDisabled ? "checkbox_disabled" : "" ;
const extensionHtml = await generateExtensionHtml ( name , manifest , isActive , isDisabled , isExternal , checkboxClass ) ;
return { isExternal , extensionHtml } ;
}
/ * *
* Gets the module information to be displayed .
*
* @ return { string } - The HTML string for the module information .
* /
function getModuleInformation ( ) {
let moduleInfo = modules . length ? ` <p> ${ DOMPurify . sanitize ( modules . join ( ', ' ) ) } </p> ` : '<p class="failure">Not connected to the API!</p>' ;
return `
2023-10-31 21:16:33 +01:00
< h3 > Modules provided by your Extras API : < / h 3 >
2023-07-20 19:32:15 +02:00
$ { moduleInfo }
` ;
}
/ * *
* Generates the HTML strings for all extensions and displays them in a popup .
* /
async function showExtensionsDetails ( ) {
2023-11-14 07:53:26 +01:00
try {
2023-11-10 09:25:34 +01:00
showLoader ( ) ;
let htmlDefault = '<h3>Built-in Extensions:</h3>' ;
let htmlExternal = '<h3>Installed Extensions:</h3>' ;
2023-07-20 19:32:15 +02:00
2023-11-10 09:25:34 +01:00
const extensions = Object . entries ( manifests ) . sort ( ( a , b ) => a [ 1 ] . loading _order - b [ 1 ] . loading _order ) ;
const promises = [ ] ;
2023-07-20 19:32:15 +02:00
2023-11-10 09:25:34 +01:00
for ( const extension of extensions ) {
promises . push ( getExtensionData ( extension ) ) ;
}
2023-07-20 19:32:15 +02:00
2023-11-10 09:25:34 +01:00
const settledPromises = await Promise . allSettled ( promises ) ;
2023-10-23 15:53:31 +02:00
2023-11-10 09:25:34 +01:00
settledPromises . forEach ( promise => {
if ( promise . status === 'fulfilled' ) {
const { isExternal , extensionHtml } = promise . value ;
if ( isExternal ) {
htmlExternal += extensionHtml ;
} else {
htmlDefault += extensionHtml ;
}
2023-10-23 15:53:31 +02:00
}
2023-11-10 09:25:34 +01:00
} ) ;
2023-10-23 15:53:31 +02:00
2023-11-10 09:25:34 +01:00
const html = `
$ { getModuleInformation ( ) }
$ { htmlDefault }
$ { htmlExternal }
` ;
callPopup ( ` <div class="extensions_info"> ${ html } </div> ` , 'text' ) ;
} catch ( error ) {
toastr . error ( 'Error loading extensions. See browser console for details.' ) ;
console . error ( error ) ;
} finally {
hideLoader ( ) ;
}
2023-07-20 19:32:15 +02:00
}
/ * *
* Handles the click event for the update button of an extension .
* This function makes a POST request to '/update_extension' with the extension ' s name .
* If the extension is already up to date , it displays a success message .
* If the extension is not up to date , it updates the extension and displays a success message with the new commit hash .
* /
async function onUpdateClick ( ) {
const extensionName = $ ( this ) . data ( 'name' ) ;
2023-11-19 00:33:54 +01:00
$ ( this ) . find ( 'i' ) . addClass ( 'fa-spin' ) ;
2023-10-09 22:45:09 +02:00
await updateExtension ( extensionName , false ) ;
}
/ * *
* Updates a third - party extension via the API .
* @ param { string } extensionName Extension folder name
* @ param { boolean } quiet If true , don ' t show a success message
* /
async function updateExtension ( extensionName , quiet ) {
2023-07-20 19:32:15 +02:00
try {
2023-09-21 19:42:06 +02:00
const response = await fetch ( '/api/extensions/update' , {
2023-07-20 19:32:15 +02:00
method : 'POST' ,
headers : getRequestHeaders ( ) ,
body : JSON . stringify ( { extensionName } )
} ) ;
const data = await response . json ( ) ;
2023-11-19 00:33:54 +01:00
if ( ! quiet ) {
showExtensionsDetails ( ) ;
}
2023-07-20 19:32:15 +02:00
if ( data . isUpToDate ) {
2023-10-09 22:45:09 +02:00
if ( ! quiet ) {
toastr . success ( 'Extension is already up to date' ) ;
}
2023-07-20 19:32:15 +02:00
} else {
2023-10-09 22:45:09 +02:00
toastr . success ( ` Extension ${ extensionName } updated to ${ data . shortCommitHash } ` ) ;
}
2023-07-20 19:32:15 +02:00
} catch ( error ) {
console . error ( 'Error:' , error ) ;
}
2023-10-09 22:45:09 +02:00
}
2023-07-20 19:32:15 +02:00
/ * *
* Handles the click event for the delete button of an extension .
2023-09-16 15:16:48 +02:00
* This function makes a POST request to '/api/extensions/delete' with the extension ' s name .
2023-07-20 19:32:15 +02:00
* If the extension is deleted , it displays a success message .
* Creates a popup for the user to confirm before delete .
* /
async function onDeleteClick ( ) {
const extensionName = $ ( this ) . data ( 'name' ) ;
// use callPopup to create a popup for the user to confirm before delete
const confirmation = await callPopup ( ` Are you sure you want to delete ${ extensionName } ? ` , 'delete_extension' ) ;
if ( confirmation ) {
2023-10-08 22:20:01 +02:00
await deleteExtension ( extensionName ) ;
2023-07-20 19:32:15 +02:00
}
} ;
2023-10-08 22:20:01 +02:00
export async function deleteExtension ( extensionName ) {
try {
const response = await fetch ( '/api/extensions/delete' , {
method : 'POST' ,
headers : getRequestHeaders ( ) ,
body : JSON . stringify ( { extensionName } )
} ) ;
} catch ( error ) {
console . error ( 'Error:' , error ) ;
}
2023-07-20 19:32:15 +02:00
2023-10-08 22:20:01 +02:00
toastr . success ( ` Extension ${ extensionName } deleted ` ) ;
showExtensionsDetails ( ) ;
// reload the page to remove the extension from the list
location . reload ( ) ;
}
2023-07-20 19:32:15 +02:00
/ * *
* Fetches the version details of a specific extension .
*
* @ param { string } extensionName - The name of the extension .
2023-08-22 21:45:12 +02:00
* @ return { Promise < object > } - An object containing the extension ' s version details .
2023-07-20 19:32:15 +02:00
* This object includes the currentBranchName , currentCommitHash , isUpToDate , and remoteUrl .
* @ throws { error } - If there is an error during the fetch operation , it logs the error to the console .
* /
async function getExtensionVersion ( extensionName ) {
try {
2023-09-16 15:16:48 +02:00
const response = await fetch ( '/api/extensions/version' , {
2023-07-20 19:32:15 +02:00
method : 'POST' ,
headers : getRequestHeaders ( ) ,
body : JSON . stringify ( { extensionName } )
} ) ;
const data = await response . json ( ) ;
return data ;
} catch ( error ) {
console . error ( 'Error:' , error ) ;
}
}
2023-10-08 22:20:01 +02:00
/ * *
* Installs a third - party extension via the API .
* @ param { string } url Extension repository URL
* @ returns { Promise < void > }
* /
export async function installExtension ( url ) {
2023-10-23 15:53:31 +02:00
console . debug ( 'Extension installation started' , url ) ;
2023-07-20 19:32:15 +02:00
2023-10-23 15:53:31 +02:00
toastr . info ( 'Please wait...' , 'Installing extension' ) ;
2023-10-23 12:27:04 +02:00
2023-10-08 22:20:01 +02:00
const request = await fetch ( '/api/extensions/install' , {
method : 'POST' ,
headers : getRequestHeaders ( ) ,
body : JSON . stringify ( { url } ) ,
} ) ;
if ( ! request . ok ) {
2023-10-27 12:07:56 +02:00
const text = await request . text ( ) ;
toastr . warning ( text || request . statusText , 'Extension installation failed' , { timeOut : 5000 } ) ;
console . error ( 'Extension installation failed' , request . status , request . statusText , text ) ;
2023-10-08 22:20:01 +02:00
return ;
}
const response = await request . json ( ) ;
2023-10-23 15:53:31 +02:00
toastr . success ( ` Extension " ${ response . display _name } " by ${ response . author } (version ${ response . version } ) has been installed successfully! ` , 'Extension installation successful' ) ;
console . debug ( ` Extension " ${ response . display _name } " has been installed successfully at ${ response . extensionPath } ` ) ;
2023-10-09 22:45:09 +02:00
await loadExtensionSettings ( { } , false ) ;
2023-10-08 22:20:01 +02:00
eventSource . emit ( event _types . EXTENSION _SETTINGS _LOADED ) ;
}
2023-07-20 19:32:15 +02:00
2023-10-09 22:45:09 +02:00
/ * *
* Loads extension settings from the app settings .
* @ param { object } settings App Settings
* @ param { boolean } versionChanged Is this a version change ?
* /
async function loadExtensionSettings ( settings , versionChanged ) {
2023-07-20 19:32:15 +02:00
if ( settings . extension _settings ) {
Object . assign ( extension _settings , settings . extension _settings ) ;
}
$ ( "#extensions_url" ) . val ( extension _settings . apiUrl ) ;
$ ( "#extensions_api_key" ) . val ( extension _settings . apiKey ) ;
$ ( "#extensions_autoconnect" ) . prop ( 'checked' , extension _settings . autoConnect ) ;
2023-10-23 15:53:31 +02:00
$ ( "#extensions_notify_updates" ) . prop ( 'checked' , extension _settings . notifyUpdates ) ;
2023-07-20 19:32:15 +02:00
// Activate offline extensions
eventSource . emit ( event _types . EXTENSIONS _FIRST _LOAD ) ;
extensionNames = await discoverExtensions ( ) ;
manifests = await getManifests ( extensionNames )
2023-10-09 22:45:09 +02:00
if ( versionChanged ) {
await autoUpdateExtensions ( ) ;
}
2023-07-20 19:32:15 +02:00
await activateExtensions ( ) ;
if ( extension _settings . autoConnect && extension _settings . apiUrl ) {
connectToApi ( extension _settings . apiUrl ) ;
}
2023-10-27 20:23:58 +02:00
}
2023-10-09 22:45:09 +02:00
2023-10-27 20:23:58 +02:00
export function doDailyExtensionUpdatesCheck ( ) {
setTimeout ( ( ) => {
if ( extension _settings . notifyUpdates ) {
checkForExtensionUpdates ( false ) ;
}
} , 1 ) ;
2023-10-23 15:53:31 +02:00
}
/ * *
* Checks if there are updates available for 3 rd - party extensions .
* @ param { boolean } force Skip nag check
* @ returns { Promise < any > }
* /
async function checkForExtensionUpdates ( force ) {
if ( ! force ) {
const STORAGE _NAG _KEY = 'extension_update_nag' ;
const currentDate = new Date ( ) . toDateString ( ) ;
// Don't nag more than once a day
if ( localStorage . getItem ( STORAGE _NAG _KEY ) === currentDate ) {
return ;
}
localStorage . setItem ( STORAGE _NAG _KEY , currentDate ) ;
}
const updatesAvailable = [ ] ;
const promises = [ ] ;
for ( const [ id , manifest ] of Object . entries ( manifests ) ) {
if ( manifest . auto _update && id . startsWith ( 'third-party' ) ) {
const promise = new Promise ( async ( resolve , reject ) => {
try {
const data = await getExtensionVersion ( id . replace ( 'third-party' , '' ) ) ;
if ( data . isUpToDate === false ) {
updatesAvailable . push ( manifest . display _name ) ;
}
resolve ( ) ;
} catch ( error ) {
console . error ( 'Error checking for extension updates' , error ) ;
reject ( ) ;
}
} ) ;
promises . push ( promise ) ;
}
}
await Promise . allSettled ( promises ) ;
if ( updatesAvailable . length > 0 ) {
toastr . info ( ` ${ updatesAvailable . map ( x => ` • ${ x } ` ) . join ( '\n' ) } ` , 'Extension updates available' ) ;
}
2023-10-09 22:45:09 +02:00
}
async function autoUpdateExtensions ( ) {
2023-11-21 23:58:06 +01:00
toastr . info ( 'Auto-updating extensions. This may take several minutes.' , 'Please wait...' , { timeOut : 10000 , extendedTimeOut : 20000 } ) ;
const promises = [ ] ;
2023-10-09 22:45:09 +02:00
for ( const [ id , manifest ] of Object . entries ( manifests ) ) {
if ( manifest . auto _update && id . startsWith ( 'third-party' ) ) {
console . debug ( ` Auto-updating 3rd-party extension: ${ manifest . display _name } ( ${ id } ) ` ) ;
2023-11-21 23:58:06 +01:00
promises . push ( updateExtension ( id . replace ( 'third-party' , '' ) , true ) ) ;
2023-10-09 22:45:09 +02:00
}
}
2023-11-21 23:58:06 +01:00
await Promise . allSettled ( promises ) ;
2023-07-20 19:32:15 +02:00
}
2023-10-29 17:29:10 +01:00
/ * *
* Runs the generate interceptors for all extensions .
* @ param { any [ ] } chat Chat array
* @ param { number } contextSize Context size
* @ returns { Promise < boolean > } True if generation should be aborted
* /
2023-07-20 19:32:15 +02:00
async function runGenerationInterceptors ( chat , contextSize ) {
2023-10-29 17:29:10 +01:00
let aborted = false ;
let exitImmediately = false ;
const abort = ( /** @type {boolean} */ immediately ) => {
aborted = true ;
exitImmediately = immediately ;
} ;
2023-07-20 19:32:15 +02:00
for ( const manifest of Object . values ( manifests ) ) {
const interceptorKey = manifest . generate _interceptor ;
if ( typeof window [ interceptorKey ] === 'function' ) {
try {
2023-10-29 17:29:10 +01:00
await window [ interceptorKey ] ( chat , contextSize , abort ) ;
2023-07-20 19:32:15 +02:00
} catch ( e ) {
console . error ( ` Failed running interceptor for ${ manifest . display _name } ` , e ) ;
}
}
2023-10-29 17:29:10 +01:00
if ( exitImmediately ) {
break ;
}
2023-07-20 19:32:15 +02:00
}
2023-10-29 17:29:10 +01:00
return aborted ;
2023-07-20 19:32:15 +02:00
}
2023-08-22 16:46:37 +02:00
jQuery ( function ( ) {
2023-08-24 22:52:03 +02:00
addExtensionsButtonAndMenu ( ) ;
$ ( "#extensionsMenuButton" ) . css ( "display" , "flex" ) ;
2023-07-20 19:32:15 +02:00
$ ( "#extensions_connect" ) . on ( 'click' , connectClickHandler ) ;
$ ( "#extensions_autoconnect" ) . on ( 'input' , autoConnectInputHandler ) ;
$ ( "#extensions_details" ) . on ( 'click' , showExtensionsDetails ) ;
2023-10-23 15:53:31 +02:00
$ ( "#extensions_notify_updates" ) . on ( 'input' , notifyUpdatesInputHandler ) ;
2023-07-20 19:32:15 +02:00
$ ( document ) . on ( 'click' , '.toggle_disable' , onDisableExtensionClick ) ;
$ ( document ) . on ( 'click' , '.toggle_enable' , onEnableExtensionClick ) ;
$ ( document ) . on ( 'click' , '.btn_update' , onUpdateClick ) ;
$ ( document ) . on ( 'click' , '.btn_delete' , onDeleteClick ) ;
2023-10-23 15:53:31 +02:00
/ * *
* Handles the click event for the third - party extension import button .
* Prompts the user to enter the Git URL of the extension to import .
* After obtaining the Git URL , makes a POST request to '/api/extensions/install' to import the extension .
* If the extension is imported successfully , a success message is displayed .
* If the extension import fails , an error message is displayed and the error is logged to the console .
* After successfully importing the extension , the extension settings are reloaded and a 'EXTENSION_SETTINGS_LOADED' event is emitted .
*
* @ listens # third _party _extension _button # click - The click event of the '#third_party_extension_button' element .
* /
$ ( '#third_party_extension_button' ) . on ( 'click' , async ( ) => {
const html = ` <h3>Enter the Git URL of the extension to install</h3>
< br >
< p > < b > Disclaimer : < / b > P l e a s e b e a w a r e t h a t u s i n g e x t e r n a l e x t e n s i o n s c a n h a v e u n i n t e n d e d s i d e e f f e c t s a n d m a y p o s e s e c u r i t y r i s k s . A l w a y s m a k e s u r e y o u t r u s t t h e s o u r c e b e f o r e i m p o r t i n g a n e x t e n s i o n . W e a r e n o t r e s p o n s i b l e f o r a n y d a m a g e c a u s e d b y t h i r d - p a r t y e x t e n s i o n s . < / p >
< br >
< p > Example : < tt > https : //github.com/author/extension-name </tt></p>`
const input = await callPopup ( html , 'input' ) ;
if ( ! input ) {
console . debug ( 'Extension install cancelled' ) ;
return ;
}
const url = input . trim ( ) ;
await installExtension ( url ) ;
} ) ;
2023-07-20 19:32:15 +02:00
} ) ;