From 96622482b39a17da1851d171ac2b609177064899 Mon Sep 17 00:00:00 2001 From: sub-hub Date: Sat, 27 Apr 2024 22:09:23 +0900 Subject: [PATCH] Add appendLastPath util function and use it in hypamemory --- src/ts/process/memory/hypamemory.ts | 7 ++----- src/ts/util.ts | 30 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/ts/process/memory/hypamemory.ts b/src/ts/process/memory/hypamemory.ts index 8597d40e..e9691155 100644 --- a/src/ts/process/memory/hypamemory.ts +++ b/src/ts/process/memory/hypamemory.ts @@ -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:{ diff --git a/src/ts/util.ts b/src/ts/util.ts index c93d63f2..8c74681c 100644 --- a/src/ts/util.ts +++ b/src/ts/util.ts @@ -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; } \ No newline at end of file