Add types for SillyTavern.getContext
| @@ -54,6 +54,7 @@ module.exports = { | ||
| 54 | 54 | }, |
| 55 | 55 | // These scripts are loaded in HTML; tell ESLint not to complain about them being undefined |
| 56 | 56 | globals: { |
| 57 | + globalThis: 'readonly', | |
| 57 | 58 | ePub: 'readonly', |
| 58 | 59 | pdfjsLib: 'readonly', |
| 59 | 60 | toastr: 'readonly', |
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import libs from './lib'; |
| 2 | +import getContext from './scripts/st-context'; | |
| 2 | 3 | |
| 3 | 4 | // Global namespace modules |
| 4 | 5 | declare var ai; |
| @@ -6,7 +7,7 @@ declare var pdfjsLib; | ||
| 6 | 7 | declare var ePub; |
| 7 | 8 | |
| 8 | 9 | declare var SillyTavern: { |
| 9 | 10 | getContext(): anytypeof getContext; |
| 10 | 11 | llm: any; |
| 11 | 12 | libs: typeof libs; |
| 12 | 13 | }; |
| @@ -13,7 +13,7 @@ import { | ||
| 13 | 13 | default as libs, |
| 14 | 14 | } from './lib.js'; |
| 15 | 15 | |
| 16 | 16 | import { humanizedDateTime, favsToHotswap, getMessageTimeStamp, dragElement, isMobile, initRossMods, shouldSendOnEnter } from './scripts/RossAscends-mods.js'; |
| 17 | 17 | import { userStatsHandler, statMesProcess, initStats } from './scripts/stats.js'; |
| 18 | 18 | import { |
| 19 | 19 | generateKoboldWithStreaming, |
| @@ -37,8 +37,6 @@ import { | ||
| 37 | 37 | parseTabbyLogprobs, |
| 38 | 38 | } from './scripts/textgen-settings.js'; |
| 39 | 39 | |
| 40 | -const { MANCER, TOGETHERAI, OOBA, VLLM, APHRODITE, TABBY, OLLAMA, INFERMATICAI, DREAMGEN, OPENROUTER, FEATHERLESS } = textgen_types; | |
| 41 | - | |
| 42 | 40 | import { |
| 43 | 41 | world_info, |
| 44 | 42 | getWorldInfoPrompt, |
| @@ -67,9 +65,7 @@ import { | ||
| 67 | 65 | getGroupChat, |
| 68 | 66 | renameGroupMember, |
| 69 | 67 | createNewGroupChat, |
| 70 | - getGroupPastChats, | |
| 71 | 68 | getGroupAvatar, |
| 72 | - openGroupChat, | |
| 73 | 69 | editGroup, |
| 74 | 70 | deleteGroupChat, |
| 75 | 71 | renameGroupChat, |
| @@ -174,8 +170,8 @@ import { | ||
| 174 | 170 | } from './scripts/utils.js'; |
| 175 | 171 | import { debounce_timeout } from './scripts/constants.js'; |
| 176 | 172 | |
| 177 | 173 | import { ModuleWorkerWrapper, doDailyExtensionUpdatesCheck, extension_settings, getContext, initExtensions, loadExtensionSettings, renderExtensionTemplate, renderExtensionTemplateAsync, runGenerationInterceptors, saveMetadataDebounced, writeExtensionField } from './scripts/extensions.js'; |
| 178 | 174 | import { COMMENT_NAME_DEFAULT, executeSlashCommands, executeSlashCommandsOnChatInput, executeSlashCommandsWithOptions, getSlashCommandsHelp, initDefaultSlashCommands, isExecutingCommandsFromChatInput, pauseScriptExecution, processChatSlashCommands, registerSlashCommand, stopScriptExecution } from './scripts/slash-commands.js'; |
| 179 | 175 | import { |
| 180 | 176 | tag_map, |
| 181 | 177 | tags, |
| @@ -242,11 +238,11 @@ import { BulkEditOverlay, CharacterContextMenu } from './scripts/BulkEditOverlay | ||
| 242 | 238 | import { loadFeatherlessModels, loadMancerModels, loadOllamaModels, loadTogetherAIModels, loadInfermaticAIModels, loadOpenRouterModels, loadVllmModels, loadAphroditeModels, loadDreamGenModels, initTextGenModels, loadTabbyModels } from './scripts/textgen-models.js'; |
| 243 | 239 | import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, decodeStyleTags, encodeStyleTags, isExternalMediaAllowed, getCurrentEntityId, preserveNeutralChat, restoreNeutralChat } from './scripts/chats.js'; |
| 244 | 240 | import { getPresetManager, initPresetManager } from './scripts/preset-manager.js'; |
| 245 | 241 | import { MacrosParser, evaluateMacros, getLastMessageId, initMacros } from './scripts/macros.js'; |
| 246 | 242 | import { currentUser, setUserControls } from './scripts/user.js'; |
| 247 | 243 | import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup, fixToastrForDialogs } from './scripts/popup.js'; |
| 248 | 244 | import { renderTemplate, renderTemplateAsync } from './scripts/templates.js'; |
| 249 | 245 | import { initScrapers, ScraperManager } from './scripts/scrapers.js'; |
| 250 | 246 | import { SlashCommandParser } from './scripts/slash-commands/SlashCommandParser.js'; |
| 251 | 247 | import { SlashCommand } from './scripts/slash-commands/SlashCommand.js'; |
| 252 | 248 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './scripts/slash-commands/SlashCommandArgument.js'; |
| @@ -268,6 +264,13 @@ import { initServerHistory } from './scripts/server-history.js'; | ||
| 268 | 264 | import { initSettingsSearch } from './scripts/setting-search.js'; |
| 269 | 265 | import { initBulkEdit } from './scripts/bulk-edit.js'; |
| 270 | 266 | import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js'; |
| 267 | +import { getContext } from './scripts/st-context.js'; | |
| 268 | + | |
| 269 | +// API OBJECT FOR EXTERNAL WIRING | |
| 270 | +globalThis.SillyTavern = { | |
| 271 | + libs, | |
| 272 | + getContext, | |
| 273 | +}; | |
| 271 | 274 | |
| 272 | 275 | //exporting functions and vars for mods |
| 273 | 276 | export { |
| @@ -427,10 +430,6 @@ DOMPurify.addHook('uponSanitizeElement', (node, _, config) => { | ||
| 427 | 430 | } |
| 428 | 431 | }); |
| 429 | 432 | |
| 430 | -// API OBJECT FOR EXTERNAL WIRING | |
| 431 | -window['SillyTavern'] = {}; | |
| 432 | -window['SillyTavern'].libs = libs; | |
| 433 | - | |
| 434 | 433 | // Event source init |
| 435 | 434 | export const event_types = { |
| 436 | 435 | APP_READY: 'app_ready', |
| @@ -812,7 +811,7 @@ export let menu_type = ''; | ||
| 812 | 811 | export let selected_button = ''; //which button pressed |
| 813 | 812 | |
| 814 | 813 | //create pole save |
| 815 | 814 | export let create_save = { |
| 816 | 815 | name: '', |
| 817 | 816 | description: '', |
| 818 | 817 | creator_notes: '', |
| @@ -864,7 +863,7 @@ export let amount_gen = 80; //default max length of AI generated responses | ||
| 864 | 863 | export let max_context = 2048; |
| 865 | 864 | |
| 866 | 865 | var swipes = true; |
| 867 | 866 | export let extension_prompts = {}; |
| 868 | 867 | |
| 869 | 868 | export let main_api;// = "kobold"; |
| 870 | 869 | //novel settings |
| @@ -1175,7 +1174,7 @@ async function getStatusTextgen() { | ||
| 1175 | 1174 | return resultCheckStatus(); |
| 1176 | 1175 | } |
| 1177 | 1176 | |
| 1178 | 1177 | if (textgen_settings.type == textgen_types.OOBA && textgen_settings.bypass_status_check) { |
| 1179 | 1178 | setOnlineStatus('Status check bypassed'); |
| 1180 | 1179 | return resultCheckStatus(); |
| 1181 | 1180 | } |
| @@ -1193,34 +1192,34 @@ async function getStatusTextgen() { | ||
| 1193 | 1192 | |
| 1194 | 1193 | const data = await response.json(); |
| 1195 | 1194 | |
| 1196 | 1195 | if (textgen_settings.type === textgen_types.MANCER) { |
| 1197 | 1196 | loadMancerModels(data?.data); |
| 1198 | 1197 | setOnlineStatus(textgen_settings.mancer_model); |
| 1199 | 1198 | } else if (textgen_settings.type === textgen_types.TOGETHERAI) { |
| 1200 | 1199 | loadTogetherAIModels(data?.data); |
| 1201 | 1200 | setOnlineStatus(textgen_settings.togetherai_model); |
| 1202 | 1201 | } else if (textgen_settings.type === textgen_types.OLLAMA) { |
| 1203 | 1202 | loadOllamaModels(data?.data); |
| 1204 | 1203 | setOnlineStatus(textgen_settings.ollama_model || 'Connected'); |
| 1205 | 1204 | } else if (textgen_settings.type === textgen_types.INFERMATICAI) { |
| 1206 | 1205 | loadInfermaticAIModels(data?.data); |
| 1207 | 1206 | setOnlineStatus(textgen_settings.infermaticai_model); |
| 1208 | 1207 | } else if (textgen_settings.type === textgen_types.DREAMGEN) { |
| 1209 | 1208 | loadDreamGenModels(data?.data); |
| 1210 | 1209 | setOnlineStatus(textgen_settings.dreamgen_model); |
| 1211 | 1210 | } else if (textgen_settings.type === textgen_types.OPENROUTER) { |
| 1212 | 1211 | loadOpenRouterModels(data?.data); |
| 1213 | 1212 | setOnlineStatus(textgen_settings.openrouter_model); |
| 1214 | 1213 | } else if (textgen_settings.type === textgen_types.VLLM) { |
| 1215 | 1214 | loadVllmModels(data?.data); |
| 1216 | 1215 | setOnlineStatus(textgen_settings.vllm_model); |
| 1217 | 1216 | } else if (textgen_settings.type === textgen_types.APHRODITE) { |
| 1218 | 1217 | loadAphroditeModels(data?.data); |
| 1219 | 1218 | setOnlineStatus(textgen_settings.aphrodite_model); |
| 1220 | 1219 | } else if (textgen_settings.type === textgen_types.FEATHERLESS) { |
| 1221 | 1220 | loadFeatherlessModels(data?.data); |
| 1222 | 1221 | setOnlineStatus(textgen_settings.featherless_model); |
| 1223 | 1222 | } else if (textgen_settings.type === textgen_types.TABBY) { |
| 1224 | 1223 | loadTabbyModels(data?.data); |
| 1225 | 1224 | setOnlineStatus(textgen_settings.tabby_model || data?.result); |
| 1226 | 1225 | } else { |
| @@ -5542,7 +5541,7 @@ function extractMultiSwipes(data, type) { | ||
| 5542 | 5541 | return swipes; |
| 5543 | 5542 | } |
| 5544 | 5543 | |
| 5545 | 5544 | if (main_api === 'openai' || (main_api === 'textgenerationwebui' && [textgen_types.MANCER, textgen_types.VLLM, textgen_types.APHRODITE, textgen_types.TABBY, textgen_types.INFERMATICAI].includes(textgen_settings.type))) { |
| 5546 | 5545 | if (!Array.isArray(data.choices)) { |
| 5547 | 5546 | return swipes; |
| 5548 | 5547 | } |
| @@ -5705,7 +5704,7 @@ export function cleanUpMessage(getMessage, isImpersonate, isContinue, displayInc | ||
| 5705 | 5704 | return getMessage; |
| 5706 | 5705 | } |
| 5707 | 5706 | |
| 5708 | 5707 | export async function saveReply(type, getMessage, fromStreaming, title, swipes) { |
| 5709 | 5708 | if (type != 'append' && type != 'continue' && type != 'appendFinal' && chat.length && (chat[chat.length - 1]['swipe_id'] === undefined || |
| 5710 | 5709 | chat[chat.length - 1]['is_user'])) { |
| 5711 | 5710 | type = 'normal'; |
| @@ -8237,102 +8236,6 @@ async function createOrEditCharacter(e) { | ||
| 8237 | 8236 | } |
| 8238 | 8237 | } |
| 8239 | 8238 | |
| 8240 | -window['SillyTavern'].getContext = function () { | |
| 8241 | - return { | |
| 8242 | - chat: chat, | |
| 8243 | - characters: characters, | |
| 8244 | - groups: groups, | |
| 8245 | - name1: name1, | |
| 8246 | - name2: name2, | |
| 8247 | - characterId: this_chid, | |
| 8248 | - groupId: selected_group, | |
| 8249 | - chatId: selected_group | |
| 8250 | - ? groups.find(x => x.id == selected_group)?.chat_id | |
| 8251 | - : (this_chid && characters[this_chid] && characters[this_chid].chat), | |
| 8252 | - getCurrentChatId: getCurrentChatId, | |
| 8253 | - getRequestHeaders: getRequestHeaders, | |
| 8254 | - reloadCurrentChat: reloadCurrentChat, | |
| 8255 | - renameChat: renameChat, | |
| 8256 | - saveSettingsDebounced: saveSettingsDebounced, | |
| 8257 | - onlineStatus: online_status, | |
| 8258 | - maxContext: Number(max_context), | |
| 8259 | - chatMetadata: chat_metadata, | |
| 8260 | - streamingProcessor, | |
| 8261 | - eventSource: eventSource, | |
| 8262 | - eventTypes: event_types, | |
| 8263 | - addOneMessage: addOneMessage, | |
| 8264 | - generate: Generate, | |
| 8265 | - sendStreamingRequest: sendStreamingRequest, | |
| 8266 | - sendGenerationRequest: sendGenerationRequest, | |
| 8267 | - stopGeneration: stopGeneration, | |
| 8268 | - getTokenCount: getTokenCount, | |
| 8269 | - extensionPrompts: extension_prompts, | |
| 8270 | - setExtensionPrompt: setExtensionPrompt, | |
| 8271 | - updateChatMetadata: updateChatMetadata, | |
| 8272 | - saveChat: saveChatConditional, | |
| 8273 | - openCharacterChat: openCharacterChat, | |
| 8274 | - openGroupChat: openGroupChat, | |
| 8275 | - saveMetadata: saveMetadata, | |
| 8276 | - sendSystemMessage: sendSystemMessage, | |
| 8277 | - activateSendButtons, | |
| 8278 | - deactivateSendButtons, | |
| 8279 | - saveReply, | |
| 8280 | - substituteParams, | |
| 8281 | - substituteParamsExtended, | |
| 8282 | - SlashCommandParser, | |
| 8283 | - SlashCommand, | |
| 8284 | - SlashCommandArgument, | |
| 8285 | - SlashCommandNamedArgument, | |
| 8286 | - ARGUMENT_TYPE, | |
| 8287 | - executeSlashCommandsWithOptions, | |
| 8288 | - /** @deprecated Use SlashCommandParser.addCommandObject() instead */ | |
| 8289 | - registerSlashCommand: registerSlashCommand, | |
| 8290 | - /** @deprecated Use executeSlashCommandWithOptions instead */ | |
| 8291 | - executeSlashCommands: executeSlashCommands, | |
| 8292 | - timestampToMoment: timestampToMoment, | |
| 8293 | - /** @deprecated Handlebars for extensions are no longer supported. */ | |
| 8294 | - registerHelper: () => { }, | |
| 8295 | - registerMacro: MacrosParser.registerMacro.bind(MacrosParser), | |
| 8296 | - unregisterMacro: MacrosParser.unregisterMacro.bind(MacrosParser), | |
| 8297 | - registerFunctionTool: ToolManager.registerFunctionTool.bind(ToolManager), | |
| 8298 | - unregisterFunctionTool: ToolManager.unregisterFunctionTool.bind(ToolManager), | |
| 8299 | - isToolCallingSupported: ToolManager.isToolCallingSupported.bind(ToolManager), | |
| 8300 | - canPerformToolCalls: ToolManager.canPerformToolCalls.bind(ToolManager), | |
| 8301 | - registerDebugFunction: registerDebugFunction, | |
| 8302 | - /** @deprecated Use renderExtensionTemplateAsync instead. */ | |
| 8303 | - renderExtensionTemplate: renderExtensionTemplate, | |
| 8304 | - renderExtensionTemplateAsync: renderExtensionTemplateAsync, | |
| 8305 | - registerDataBankScraper: ScraperManager.registerDataBankScraper, | |
| 8306 | - /** @deprecated Use callGenericPopup or Popup instead. */ | |
| 8307 | - callPopup: callPopup, | |
| 8308 | - callGenericPopup: callGenericPopup, | |
| 8309 | - showLoader: showLoader, | |
| 8310 | - hideLoader: hideLoader, | |
| 8311 | - mainApi: main_api, | |
| 8312 | - extensionSettings: extension_settings, | |
| 8313 | - ModuleWorkerWrapper: ModuleWorkerWrapper, | |
| 8314 | - getTokenizerModel: getTokenizerModel, | |
| 8315 | - generateQuietPrompt: generateQuietPrompt, | |
| 8316 | - writeExtensionField: writeExtensionField, | |
| 8317 | - getThumbnailUrl: getThumbnailUrl, | |
| 8318 | - selectCharacterById: selectCharacterById, | |
| 8319 | - messageFormatting: messageFormatting, | |
| 8320 | - shouldSendOnEnter: shouldSendOnEnter, | |
| 8321 | - isMobile: isMobile, | |
| 8322 | - t: t, | |
| 8323 | - translate: translate, | |
| 8324 | - tags: tags, | |
| 8325 | - tagMap: tag_map, | |
| 8326 | - menuType: menu_type, | |
| 8327 | - createCharacterData: create_save, | |
| 8328 | - /** @deprecated Legacy snake-case naming, compatibility with old extensions */ | |
| 8329 | - event_types: event_types, | |
| 8330 | - Popup: Popup, | |
| 8331 | - POPUP_TYPE: POPUP_TYPE, | |
| 8332 | - POPUP_RESULT: POPUP_RESULT, | |
| 8333 | - }; | |
| 8334 | -}; | |
| 8335 | - | |
| 8336 | 8239 | /** |
| 8337 | 8240 | * Formats a counter for a swipe view. |
| 8338 | 8241 | * @param {number} current The current number of items. |
| @@ -5,6 +5,7 @@ import { showLoader } from './loader.js'; | ||
| 5 | 5 | import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup } from './popup.js'; |
| 6 | 6 | import { renderTemplate, renderTemplateAsync } from './templates.js'; |
| 7 | 7 | import { isSubsetOf, setValueByPath } from './utils.js'; |
| 8 | +import { getContext } from './st-context.js'; | |
| 8 | 9 | export { |
| 9 | 10 | getContext, |
| 10 | 11 | getApiUrl, |
| @@ -174,7 +175,6 @@ const extension_settings = { | ||
| 174 | 175 | let modules = []; |
| 175 | 176 | let activeExtensions = new Set(); |
| 176 | 177 | |
| 177 | -const getContext = () => window['SillyTavern'].getContext(); | |
| 178 | 178 | const getApiUrl = () => extension_settings.apiUrl; |
| 179 | 179 | let connectedToApi = false; |
| 180 | 180 | |
| @@ -0,0 +1,170 @@ | ||
| 1 | +import { | |
| 2 | + activateSendButtons, | |
| 3 | + addOneMessage, | |
| 4 | + callPopup, | |
| 5 | + characters, | |
| 6 | + chat, | |
| 7 | + chat_metadata, | |
| 8 | + create_save, | |
| 9 | + deactivateSendButtons, | |
| 10 | + event_types, | |
| 11 | + eventSource, | |
| 12 | + extension_prompts, | |
| 13 | + Generate, | |
| 14 | + generateQuietPrompt, | |
| 15 | + getCurrentChatId, | |
| 16 | + getRequestHeaders, | |
| 17 | + getThumbnailUrl, | |
| 18 | + main_api, | |
| 19 | + max_context, | |
| 20 | + menu_type, | |
| 21 | + messageFormatting, | |
| 22 | + name1, | |
| 23 | + name2, | |
| 24 | + online_status, | |
| 25 | + openCharacterChat, | |
| 26 | + reloadCurrentChat, | |
| 27 | + renameChat, | |
| 28 | + saveChatConditional, | |
| 29 | + saveMetadata, | |
| 30 | + saveReply, | |
| 31 | + saveSettingsDebounced, | |
| 32 | + selectCharacterById, | |
| 33 | + sendGenerationRequest, | |
| 34 | + sendStreamingRequest, | |
| 35 | + sendSystemMessage, | |
| 36 | + setExtensionPrompt, | |
| 37 | + stopGeneration, | |
| 38 | + streamingProcessor, | |
| 39 | + substituteParams, | |
| 40 | + substituteParamsExtended, | |
| 41 | + this_chid, | |
| 42 | + updateChatMetadata, | |
| 43 | +} from '../script.js'; | |
| 44 | +import { | |
| 45 | + extension_settings, | |
| 46 | + ModuleWorkerWrapper, | |
| 47 | + renderExtensionTemplate, | |
| 48 | + renderExtensionTemplateAsync, | |
| 49 | + writeExtensionField, | |
| 50 | +} from './extensions.js'; | |
| 51 | +import { groups, openGroupChat, selected_group } from './group-chats.js'; | |
| 52 | +import { t, translate } from './i18n.js'; | |
| 53 | +import { hideLoader, showLoader } from './loader.js'; | |
| 54 | +import { MacrosParser } from './macros.js'; | |
| 55 | +import { oai_settings } from './openai.js'; | |
| 56 | +import { callGenericPopup, Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js'; | |
| 57 | +import { registerDebugFunction } from './power-user.js'; | |
| 58 | +import { isMobile, shouldSendOnEnter } from './RossAscends-mods.js'; | |
| 59 | +import { ScraperManager } from './scrapers.js'; | |
| 60 | +import { executeSlashCommands, executeSlashCommandsWithOptions, registerSlashCommand } from './slash-commands.js'; | |
| 61 | +import { SlashCommand } from './slash-commands/SlashCommand.js'; | |
| 62 | +import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js'; | |
| 63 | +import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; | |
| 64 | +import { tag_map, tags } from './tags.js'; | |
| 65 | +import { textgenerationwebui_settings } from './textgen-settings.js'; | |
| 66 | +import { getTokenCount, getTokenCountAsync, getTokenizerModel } from './tokenizers.js'; | |
| 67 | +import { ToolManager } from './tool-calling.js'; | |
| 68 | +import { timestampToMoment } from './utils.js'; | |
| 69 | + | |
| 70 | +export function getContext() { | |
| 71 | + return { | |
| 72 | + chatCompletionSettings: oai_settings, | |
| 73 | + textCompletionSettings: textgenerationwebui_settings, | |
| 74 | + chat, | |
| 75 | + characters, | |
| 76 | + groups, | |
| 77 | + name1, | |
| 78 | + name2, | |
| 79 | + characterId: this_chid, | |
| 80 | + groupId: selected_group, | |
| 81 | + chatId: selected_group | |
| 82 | + ? groups.find(x => x.id == selected_group)?.chat_id | |
| 83 | + : (this_chid && characters[this_chid] && characters[this_chid].chat), | |
| 84 | + getCurrentChatId, | |
| 85 | + getRequestHeaders, | |
| 86 | + reloadCurrentChat, | |
| 87 | + renameChat, | |
| 88 | + saveSettingsDebounced, | |
| 89 | + onlineStatus: online_status, | |
| 90 | + maxContext: Number(max_context), | |
| 91 | + chatMetadata: chat_metadata, | |
| 92 | + streamingProcessor, | |
| 93 | + eventSource, | |
| 94 | + eventTypes: event_types, | |
| 95 | + addOneMessage, | |
| 96 | + generate: Generate, | |
| 97 | + sendStreamingRequest, | |
| 98 | + sendGenerationRequest, | |
| 99 | + stopGeneration, | |
| 100 | + /** @deprecated Use getTokenCountAsync instead */ | |
| 101 | + getTokenCount, | |
| 102 | + getTokenCountAsync, | |
| 103 | + extensionPrompts: extension_prompts, | |
| 104 | + setExtensionPrompt: setExtensionPrompt, | |
| 105 | + updateChatMetadata: updateChatMetadata, | |
| 106 | + saveChat: saveChatConditional, | |
| 107 | + openCharacterChat, | |
| 108 | + openGroupChat, | |
| 109 | + saveMetadata, | |
| 110 | + sendSystemMessage, | |
| 111 | + activateSendButtons, | |
| 112 | + deactivateSendButtons, | |
| 113 | + saveReply, | |
| 114 | + substituteParams, | |
| 115 | + substituteParamsExtended, | |
| 116 | + SlashCommandParser, | |
| 117 | + SlashCommand, | |
| 118 | + SlashCommandArgument, | |
| 119 | + SlashCommandNamedArgument, | |
| 120 | + ARGUMENT_TYPE, | |
| 121 | + executeSlashCommandsWithOptions, | |
| 122 | + /** @deprecated Use SlashCommandParser.addCommandObject() instead */ | |
| 123 | + registerSlashCommand, | |
| 124 | + /** @deprecated Use executeSlashCommandWithOptions instead */ | |
| 125 | + executeSlashCommands, | |
| 126 | + timestampToMoment, | |
| 127 | + /** @deprecated Handlebars for extensions are no longer supported. */ | |
| 128 | + registerHelper: () => { }, | |
| 129 | + registerMacro: MacrosParser.registerMacro.bind(MacrosParser), | |
| 130 | + unregisterMacro: MacrosParser.unregisterMacro.bind(MacrosParser), | |
| 131 | + registerFunctionTool: ToolManager.registerFunctionTool.bind(ToolManager), | |
| 132 | + unregisterFunctionTool: ToolManager.unregisterFunctionTool.bind(ToolManager), | |
| 133 | + isToolCallingSupported: ToolManager.isToolCallingSupported.bind(ToolManager), | |
| 134 | + canPerformToolCalls: ToolManager.canPerformToolCalls.bind(ToolManager), | |
| 135 | + registerDebugFunction: registerDebugFunction, | |
| 136 | + /** @deprecated Use renderExtensionTemplateAsync instead. */ | |
| 137 | + renderExtensionTemplate: renderExtensionTemplate, | |
| 138 | + renderExtensionTemplateAsync: renderExtensionTemplateAsync, | |
| 139 | + registerDataBankScraper: ScraperManager.registerDataBankScraper, | |
| 140 | + /** @deprecated Use callGenericPopup or Popup instead. */ | |
| 141 | + callPopup, | |
| 142 | + callGenericPopup, | |
| 143 | + showLoader, | |
| 144 | + hideLoader, | |
| 145 | + mainApi: main_api, | |
| 146 | + extensionSettings: extension_settings, | |
| 147 | + ModuleWorkerWrapper, | |
| 148 | + getTokenizerModel, | |
| 149 | + generateQuietPrompt, | |
| 150 | + writeExtensionField, | |
| 151 | + getThumbnailUrl, | |
| 152 | + selectCharacterById, | |
| 153 | + messageFormatting, | |
| 154 | + shouldSendOnEnter, | |
| 155 | + isMobile, | |
| 156 | + t, | |
| 157 | + translate, | |
| 158 | + tags, | |
| 159 | + tagMap: tag_map, | |
| 160 | + menuType: menu_type, | |
| 161 | + createCharacterData: create_save, | |
| 162 | + /** @deprecated Legacy snake-case naming, compatibility with old extensions */ | |
| 163 | + event_types: event_types, | |
| 164 | + Popup: Popup, | |
| 165 | + POPUP_TYPE, | |
| 166 | + POPUP_RESULT, | |
| 167 | + }; | |
| 168 | +} | |
| 169 | + | |
| 170 | +export default getContext; | |
| @@ -18,13 +18,6 @@ import { getCurrentDreamGenModelTokenizer, getCurrentOpenRouterModelTokenizer } | ||
| 18 | 18 | import { ENCODE_TOKENIZERS, TEXTGEN_TOKENIZERS, getTextTokens, tokenizers } from './tokenizers.js'; |
| 19 | 19 | import { getSortableDelay, onlyUnique } from './utils.js'; |
| 20 | 20 | |
| 21 | -export { | |
| 22 | - settings as textgenerationwebui_settings, | |
| 23 | - loadTextGenSettings, | |
| 24 | - generateTextGenWithStreaming, | |
| 25 | - formatTextGenURL, | |
| 26 | -}; | |
| 27 | - | |
| 28 | 21 | export const textgen_types = { |
| 29 | 22 | OOBA: 'ooba', |
| 30 | 23 | MANCER: 'mancer', |
| @@ -197,6 +190,10 @@ const settings = { | ||
| 197 | 190 | featherless_model: '', |
| 198 | 191 | }; |
| 199 | 192 | |
| 193 | +export { | |
| 194 | + settings as textgenerationwebui_settings, | |
| 195 | +}; | |
| 196 | + | |
| 200 | 197 | export let textgenerationwebui_banned_in_macros = []; |
| 201 | 198 | |
| 202 | 199 | export let textgenerationwebui_presets = []; |
| @@ -327,7 +324,7 @@ async function selectPreset(name) { | ||
| 327 | 324 | saveSettingsDebounced(); |
| 328 | 325 | } |
| 329 | 326 | |
| 330 | 327 | export function formatTextGenURL(value) { |
| 331 | 328 | try { |
| 332 | 329 | const noFormatTypes = [MANCER, TOGETHERAI, INFERMATICAI, DREAMGEN, OPENROUTER]; |
| 333 | 330 | if (noFormatTypes.includes(settings.type)) { |
| @@ -465,7 +462,7 @@ function calculateLogitBias() { | ||
| 465 | 462 | return result; |
| 466 | 463 | } |
| 467 | 464 | |
| 468 | 465 | export function loadTextGenSettings(data, loadedSettings) { |
| 469 | 466 | textgenerationwebui_presets = convertPresets(data.textgenerationwebui_presets); |
| 470 | 467 | textgenerationwebui_preset_names = data.textgenerationwebui_preset_names ?? []; |
| 471 | 468 | Object.assign(settings, loadedSettings.textgenerationwebui_settings ?? {}); |
| @@ -889,7 +886,7 @@ function setSettingByName(setting, value, trigger) { | ||
| 889 | 886 | * @returns {Promise<(function(): AsyncGenerator<{swipes: [], text: string, toolCalls: [], logprobs: {token: string, topLogprobs: Candidate[]}|null}, void, *>)|*>} |
| 890 | 887 | * @throws {Error} - If the response status is not OK, or from within the generator |
| 891 | 888 | */ |
| 892 | 889 | export async function generateTextGenWithStreaming(generate_data, signal) { |
| 893 | 890 | generate_data.stream = true; |
| 894 | 891 | |
| 895 | 892 | const response = await fetch('/api/backends/text-completions/generate', { |
| @@ -8,8 +8,6 @@ import { kai_flags } from './kai-settings.js'; | ||
| 8 | 8 | import { textgen_types, textgenerationwebui_settings as textgen_settings, getTextGenServer, getTextGenModel } from './textgen-settings.js'; |
| 9 | 9 | import { getCurrentDreamGenModelTokenizer, getCurrentOpenRouterModelTokenizer, openRouterModels } from './textgen-models.js'; |
| 10 | 10 | |
| 11 | -const { OOBA, TABBY, KOBOLDCPP, VLLM, APHRODITE, LLAMACPP, OPENROUTER, DREAMGEN } = textgen_types; | |
| 12 | - | |
| 13 | 11 | export const CHARACTERS_PER_TOKEN_RATIO = 3.35; |
| 14 | 12 | export const TOKENIZER_WARNING_KEY = 'tokenizationWarningShown'; |
| 15 | 13 | export const TOKENIZER_SUPPORTED_KEY = 'tokenizationSupported'; |
| @@ -52,8 +50,12 @@ export const ENCODE_TOKENIZERS = [ | ||
| 52 | 50 | //tokenizers.NERD2, |
| 53 | 51 | ]; |
| 54 | 52 | |
| 55 | -// A list of Text Completion sources that support remote tokenization. | |
| 53 | +/** | |
| 56 | -export const TEXTGEN_TOKENIZERS = [OOBA, TABBY, KOBOLDCPP, LLAMACPP, VLLM, APHRODITE]; | |
| 54 | + * A list of Text Completion sources that support remote tokenization. | |
| 55 | + * Populated in initTokenziers due to circular dependencies. | |
| 56 | + * @type {string[]} | |
| 57 | + */ | |
| 58 | +export const TEXTGEN_TOKENIZERS = []; | |
| 57 | 59 | |
| 58 | 60 | const TOKENIZER_URLS = { |
| 59 | 61 | [tokenizers.GPT2]: { |
| @@ -287,7 +289,7 @@ export function getTokenizerBestMatch(forApi) { | ||
| 287 | 289 | const hasTokenizerError = sessionStorage.getItem(TOKENIZER_WARNING_KEY); |
| 288 | 290 | const hasValidEndpoint = sessionStorage.getItem(TOKENIZER_SUPPORTED_KEY); |
| 289 | 291 | const isConnected = online_status !== 'no_connection'; |
| 290 | 292 | const isTokenizerSupported = TEXTGEN_TOKENIZERS.includes(textgen_settings.type) && (textgen_settings.type !== textgen_types.OOBA || hasValidEndpoint); |
| 291 | 293 | |
| 292 | 294 | if (!hasTokenizerError && isConnected) { |
| 293 | 295 | if (forApi === 'kobold' && kai_flags.can_use_tokenization) { |
| @@ -297,10 +299,10 @@ export function getTokenizerBestMatch(forApi) { | ||
| 297 | 299 | if (forApi === 'textgenerationwebui' && isTokenizerSupported) { |
| 298 | 300 | return tokenizers.API_TEXTGENERATIONWEBUI; |
| 299 | 301 | } |
| 300 | 302 | if (forApi === 'textgenerationwebui' && textgen_settings.type === textgen_types.OPENROUTER) { |
| 301 | 303 | return getCurrentOpenRouterModelTokenizer(); |
| 302 | 304 | } |
| 303 | 305 | if (forApi === 'textgenerationwebui' && textgen_settings.type === textgen_types.DREAMGEN) { |
| 304 | 306 | return getCurrentDreamGenModelTokenizer(); |
| 305 | 307 | } |
| 306 | 308 | } |
| @@ -576,7 +578,7 @@ export function getTokenizerModel() { | ||
| 576 | 578 | |
| 577 | 579 | // And for OpenRouter (if not a site model, then it's impossible to determine the tokenizer) |
| 578 | 580 | if (main_api == 'openai' && oai_settings.chat_completion_source == chat_completion_sources.OPENROUTER && oai_settings.openrouter_model || |
| 579 | 581 | main_api == 'textgenerationwebui' && textgen_settings.type === textgen_types.OPENROUTER && textgen_settings.openrouter_model) { |
| 580 | 582 | const model = main_api == 'openai' |
| 581 | 583 | ? model_list.find(x => x.id === oai_settings.openrouter_model) |
| 582 | 584 | : openRouterModels.find(x => x.id === textgen_settings.openrouter_model); |
| @@ -652,7 +654,7 @@ export function getTokenizerModel() { | ||
| 652 | 654 | return oai_settings.custom_model; |
| 653 | 655 | } |
| 654 | 656 | |
| 655 | 657 | if (oai_settings.chat_completion_source === chat_completion_sources.PERPLEXITY) { |
| 656 | 658 | if (oai_settings.perplexity_model.includes('llama-3') || oai_settings.perplexity_model.includes('llama3')) { |
| 657 | 659 | return llama3Tokenizer; |
| 658 | 660 | } |
| @@ -680,7 +682,7 @@ export function getTokenizerModel() { | ||
| 680 | 682 | return yiTokenizer; |
| 681 | 683 | } |
| 682 | 684 | |
| 683 | 685 | if (oai_settings.chat_completion_source === chat_completion_sources.BLOCKENTROPY) { |
| 684 | 686 | if (oai_settings.blockentropy_model.includes('llama3')) { |
| 685 | 687 | return llama3Tokenizer; |
| 686 | 688 | } |
| @@ -1121,6 +1123,14 @@ export function decodeTextTokens(tokenizerType, ids) { | ||
| 1121 | 1123 | } |
| 1122 | 1124 | |
| 1123 | 1125 | export async function initTokenizers() { |
| 1126 | + TEXTGEN_TOKENIZERS.push( | |
| 1127 | + textgen_types.OOBA, | |
| 1128 | + textgen_types.TABBY, | |
| 1129 | + textgen_types.KOBOLDCPP, | |
| 1130 | + textgen_types.LLAMACPP, | |
| 1131 | + textgen_types.VLLM, | |
| 1132 | + textgen_types.APHRODITE, | |
| 1133 | + ); | |
| 1124 | 1134 | await loadTokenCache(); |
| 1125 | 1135 | registerDebugFunction('resetTokenCache', 'Reset token cache', 'Purges the calculated token counts. Use this if you want to force a full re-tokenization of all chats or suspect the token counts are wrong.', resetTokenCache); |
| 1126 | 1136 | } |