Add appendLastPath util function and use it in hypamemory

This commit is contained in:
sub-hub
2024-04-27 22:09:23 +09:00
parent e5a88c4ecb
commit 96622482b3
2 changed files with 32 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ import localforage from "localforage";
import { globalFetch } from "src/ts/storage/globalApi";
import { runEmbedding } from "../transformers";
import { alertError } from "src/ts/alert";
import { appendLastPath } from "src/ts/util";
export class HypaProcesser{
@@ -50,11 +51,7 @@ export class HypaProcesser{
return [0]
}
let replaceUrl = new URL(this.customEmbeddingUrl)
if(replaceUrl.pathname !== '/embeddings'){
replaceUrl.pathname = '/embeddings'
}
const replaceUrl = appendLastPath(this.customEmbeddingUrl,'embeddings')
gf = await globalFetch(replaceUrl.toString(), {
body:{

View File

@@ -492,4 +492,34 @@ export function trimUntilPunctuation(s:string){
result = result.slice(0, -1)
}
return result
}
/**
* Appends the given last path to the provided URL.
*
* @param {string} url - The base URL to which the last path will be appended.
* @param {string} lastPath - The path to be appended to the URL.
* @returns {string} The modified URL with the last path appended.
*
* @example
* appendLastPath("https://github.com/kwaroran/RisuAI","/commits/main")
* return 'https://github.com/kwaroran/RisuAI/commits/main'
*
* @example
* appendLastPath("https://github.com/kwaroran/RisuAI/","/commits/main")
* return 'https://github.com/kwaroran/RisuAI/commits/main
*
* @example
* appendLastPath("http://127.0.0.1:7997","embeddings")
* return 'http://127.0.0.1:7997/embeddings'
*/
export function appendLastPath(url, lastPath) {
// Remove trailing slash from url if exists
url = url.replace(/\/$/, '');
// Remove leading slash from lastPath if exists
lastPath = lastPath.replace(/^\//, '');
// Concat the url and lastPath
return url + '/' + lastPath;
}