From dfc1719c3f50669d048f4bd34c7719e33c68fc9e Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 8 Feb 2024 00:04:48 +0200 Subject: [PATCH] Use fuzzy name matching --- public/scripts/openai.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 4c1cbc87f..484250403 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -3935,6 +3935,28 @@ $('#delete_proxy').on('click', async function () { } }); +function runProxyCallback(_, value) { + if (!value) { + toastr.warning('Proxy preset name is required'); + return ''; + } + + const proxyNames = proxies.map(preset => preset.name); + const fuse = new Fuse(proxyNames); + const result = fuse.search(value); + + if (result.length === 0) { + toastr.warning(`Proxy preset "${value}" not found`); + return ''; + } + + const foundName = result[0].item; + $('#openai_proxy_preset').val(foundName).trigger('change'); + return foundName; +} + +registerSlashCommand('proxy', runProxyCallback, [], '(name) – sets a proxy preset by name'); + $(document).ready(async function () { $('#test_api_button').on('click', testApiConnection); @@ -4322,12 +4344,3 @@ $(document).ready(async function () { $('#customize_additional_parameters').on('click', onCustomizeParametersClick); $('#openai_proxy_preset').on('change', onProxyPresetChange); }); -function runProxyCallback(args,value) { - if ($('#openai_proxy_preset:contains("'+value+'")').length>=1) { - $('#openai_proxy_preset').val(value); - onProxyPresetChange(); - } else { - toastr.warning("The selected preset does not exist"); - } -} -registerSlashCommand('proxy',runProxyCallback,[],"(name) - Sets a proxy preset by name");