chore: Update logic of runtime url rewrite

This commit is contained in:
n4ze3m
2024-05-12 21:14:27 +05:30
parent bd84b90e5f
commit 62ffe8346e
11 changed files with 148 additions and 38 deletions

View File

@@ -1,20 +1,39 @@
import { Storage } from "@plasmohq/storage"
const storage = new Storage()
export const isUrlRewriteEnabled = async () => {
const enabled = await storage.get("urlRewriteEnabled")
return enabled === "true"
}
const DEFAULT_URL_REWRITE_URL = "http://127.0.0.1:11434"
export const isUrlRewriteEnabled = async () => {
const enabled = await storage.get<boolean | undefined>("urlRewriteEnabled")
if (typeof enabled === "undefined") {
return true
}
return enabled
}
export const setUrlRewriteEnabled = async (enabled: boolean) => {
await storage.set("urlRewriteEnabled", enabled ? "true" : "false")
}
export const getRewriteUrl = async () => {
const rewriteUrl = await storage.get("rewriteUrl")
if (!rewriteUrl || rewriteUrl.trim() === "") {
return DEFAULT_URL_REWRITE_URL
}
return rewriteUrl
}
export const setRewriteUrl = async (url: string) => {
await storage.set("rewriteUrl", url)
}
export const getAdvancedOllamaSettings = async () => {
const [isEnableRewriteUrl, rewriteUrl] = await Promise.all([
isUrlRewriteEnabled(),
getRewriteUrl()
])
return {
isEnableRewriteUrl,
rewriteUrl
}
}

View File

@@ -1,6 +1,6 @@
import { Storage } from "@plasmohq/storage"
import { cleanUrl } from "../libs/clean-url"
import { chromeRunTime } from "../libs/runtime"
import { urlRewriteRuntime } from "../libs/runtime"
const storage = new Storage()
@@ -22,10 +22,10 @@ Search results:
export const getOllamaURL = async () => {
const ollamaURL = await storage.get("ollamaURL")
if (!ollamaURL || ollamaURL.length === 0) {
await chromeRunTime(DEFAULT_OLLAMA_URL)
await urlRewriteRuntime(DEFAULT_OLLAMA_URL)
return DEFAULT_OLLAMA_URL
}
await chromeRunTime(cleanUrl(ollamaURL))
await urlRewriteRuntime(cleanUrl(ollamaURL))
return ollamaURL
}
@@ -163,7 +163,7 @@ export const setOllamaURL = async (ollamaURL: string) => {
"http://127.0.0.1:"
)
}
await chromeRunTime(cleanUrl(formattedUrl))
await urlRewriteRuntime(cleanUrl(formattedUrl))
await storage.set("ollamaURL", cleanUrl(formattedUrl))
}