Add support for Kobold Classic

abdf1f29cfa749a9224af28a6d3fe38210090b8d

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +45 -16Showing whitespace changes
public/script.js+4 -4
@@ -8392,6 +8392,9 @@ const CONNECT_API_MAP = {
83928392 },
83938393};
83948394
8395+// Collect all unique API names in an array
8396+export const UNIQUE_APIS = [...new Set(Object.values(CONNECT_API_MAP).map(x => x.selected))];
8397+
83958398// Fill connections map from textgen_types and chat_completion_sources
83968399for (const textGenType of Object.values(textgen_types)) {
83978400 if (CONNECT_API_MAP[textGenType]) continue;
@@ -8966,9 +8969,6 @@ jQuery(async function () {
89668969 return '';
89678970 }
89688971
8969- // Collect all unique API names in an array
8970- const uniqueAPIs = [...new Set(Object.values(CONNECT_API_MAP).map(x => x.selected))];
8971-
89728972 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
89738973 name: 'dupe',
89748974 callback: duplicateCharacter,
@@ -8983,7 +8983,7 @@ jQuery(async function () {
89838983 description: 'API to connect to',
89848984 typeList: [ARGUMENT_TYPE.STRING],
89858985 enumList: Object.entries(CONNECT_API_MAP).map(([api, { selected }]) =>
89868986 new SlashCommandEnumValue(api, selected, enumTypes.getBasedOnIndex(uniqueAPIsUNIQUE_APIS.findIndex(x => x === selected)),
89878987 selected[0].toUpperCase() ?? enumIcons.default)),
89888988 }),
89898989 ],
public/scripts/slash-commands.js+41 -12
@@ -1,7 +1,9 @@
11import {
22 Generate,
3+ UNIQUE_APIS,
34 activateSendButtons,
45 addOneMessage,
6+ api_server,
57 callPopup,
68 characters,
79 chat,
@@ -1496,7 +1498,8 @@ export function initDefaultSlashCommands() {
14961498 ],
14971499 helpString: 'Sets the specified prompt manager entry/entries on or off.',
14981500 }));
14991501 SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'pick-icon',
1502+ name: 'pick-icon',
15001503 callback: async () => ((await showFontAwesomePicker()) ?? false).toString(),
15011504 returns: 'The chosen icon name or false if cancelled.',
15021505 helpString: `
@@ -1522,8 +1525,9 @@ export function initDefaultSlashCommands() {
15221525 description: 'API to set/get the URL for - if not provided, current API is used',
15231526 typeList: [ARGUMENT_TYPE.STRING],
15241527 enumList: [
15251528 new SlashCommandEnumValue('custom', 'custom openai OpenAI-compatible', enumTypes.getBasedOnIndex(uniqueAPIsUNIQUE_APIS.findIndex(x => x === 'openai')), 'O'),
15261529 ...Object.values(textgen_types).map(api => new SlashCommandEnumValue(api'kobold', null'KoboldAI Classic', enumTypes.getBasedOnIndex(uniqueAPIsUNIQUE_APIS.findIndex(x => x === 'textgenerationwebuikobold')), 'TK')),
1530+ ...Object.values(textgen_types).map(api => new SlashCommandEnumValue(api, null, enumTypes.getBasedOnIndex(UNIQUE_APIS.findIndex(x => x === 'textgenerationwebui')), 'T')),
15271531 ],
15281532 }),
15291533 SlashCommandNamedArgument.fromProps({
@@ -1549,7 +1553,7 @@ export function initDefaultSlashCommands() {
15491553 or consider switching to it with <code>/api</code> first.
15501554 </div>
15511555 <div>
15521556 This slash command works for most of the Text Completion sources, KoboldAI Classic, and also Custom OpenAI compatible for the Chat Completion sources. If unsure which APIs are supported,
15531557 check the auto-completion of the optional <code>api</code> argument of this command.
15541558 </div>
15551559 `,
@@ -3471,27 +3475,52 @@ function setPromptEntryCallback(args, targetState) {
34713475 * @returns {Promise<string>}
34723476 */
34733477async function setApiUrlCallback({ api = null, connect = 'true' }, url) {
3478+ const autoConnect = isTrueBoolean(connect);
3479+
34743480 // Special handling for Chat Completion Custom OpenAI compatible, that one can also support API url handling
34753481 const isCurrentlyCustomOpenai = main_api === 'openai' && oai_settings.chat_completion_source === chat_completion_sources.CUSTOM;
34763482 if (api === chat_completion_sources.CUSTOM || (!api && isCurrentlyCustomOpenai)) {
3477- if (url && !isCurrentlyCustomOpenai) {
3478- toastr.warning('Custom OpenAI API is not the currently selected API, so we cannot do an auto-connect. Consider switching to it via /api beforehand.');
3479- return '';
3480- }
3481-
34823483 if (!url) {
34833484 return oai_settings.custom_url ?? '';
34843485 }
34853486
3487+ if (!isCurrentlyCustomOpenai && autoConnect) {
3488+ toastr.warning('Custom OpenAI API is not the currently selected API, so we cannot do an auto-connect. Consider switching to it via /api beforehand.');
3489+ return '';
3490+ }
3491+
34863492 $('#custom_api_url_text').val(url).trigger('input');
34873493
34883494 if (isTrueBoolean(connect)autoConnect) {
34893495 $('#api_button_openai').trigger('click');
34903496 }
34913497
34923498 return url;
34933499 }
34943500
3501+ // Special handling for Kobold Classic API
3502+ const isCurrentlyKoboldClassic = main_api === 'kobold';
3503+ if (api === 'kobold' || (!api && isCurrentlyKoboldClassic)) {
3504+ if (!url) {
3505+ return api_server ?? '';
3506+ }
3507+
3508+ if (!isCurrentlyKoboldClassic && autoConnect) {
3509+ toastr.warning('Kobold Classic API is not the currently selected API, so we cannot do an auto-connect. Consider switching to it via /api beforehand.');
3510+ return '';
3511+ }
3512+
3513+ $('#api_url_text').val(url).trigger('input');
3514+ // trigger blur debounced, so we hide the autocomplete menu
3515+ setTimeout(() => $('#api_url_text').trigger('blur'), 1);
3516+
3517+ if (autoConnect) {
3518+ $('#api_button').trigger('click');
3519+ }
3520+
3521+ return api_server ?? '';
3522+ }
3523+
34953524 // Do some checks and get the api type we are targeting with this command
34963525 if (api && !Object.values(textgen_types).includes(api)) {
34973526 toastr.warning(`API '${api}' is not a valid text_gen API.`);
@@ -3501,7 +3530,7 @@ async function setApiUrlCallback({ api = null, connect = 'true' }, url) {
35013530 toastr.warning(`API '${textgenerationwebui_settings.type}' is not a valid text_gen API.`);
35023531 return '';
35033532 }
35043533 if (api && url && isTrueBoolean(connect)autoConnect && api !== textgenerationwebui_settings.type) {
35053534 toastr.warning(`API '${api}' is not the currently selected API, so we cannot do an auto-connect. Consider switching to it via /api beforehand.`);
35063535 return '';
35073536 }
@@ -3524,7 +3553,7 @@ async function setApiUrlCallback({ api = null, connect = 'true' }, url) {
35243553 setTimeout(() => $(inputSelector).trigger('blur'), 1);
35253554
35263555 // Trigger the auto connect via connect button, if requested
35273556 if (isTrueBoolean(connect)autoConnect) {
35283557 $('#api_button_textgenerationwebui').trigger('click');
35293558 }
35303559