diff --git a/src/components/Common/ShareBtn.tsx b/src/components/Common/ShareBtn.tsx index 98dddb1..ffce24c 100644 --- a/src/components/Common/ShareBtn.tsx +++ b/src/components/Common/ShareBtn.tsx @@ -9,6 +9,7 @@ import { getPageShareUrl } from "~/services/ollama" import { cleanUrl } from "~/libs/clean-url" import { getUserId, saveWebshare } from "@/db" import { useTranslation } from "react-i18next" +import fetcher from "@/libs/fetcher" type Props = { messages: Message[] @@ -94,7 +95,7 @@ export const ShareBtn: React.FC = ({ messages }) => { const chat = reformatMessages(messages, values.name) const title = values.title const url = await getPageShareUrl() - const res = await fetch(`${cleanUrl(url)}/api/v1/share/create`, { + const res = await fetcher(`${cleanUrl(url)}/api/v1/share/create`, { method: "POST", headers: { "Content-Type": "application/json" diff --git a/src/components/Option/Settings/about.tsx b/src/components/Option/Settings/about.tsx index 6cdc5a5..7b7614f 100644 --- a/src/components/Option/Settings/about.tsx +++ b/src/components/Option/Settings/about.tsx @@ -4,6 +4,7 @@ import { useQuery } from "@tanstack/react-query" import { Skeleton } from "antd" import { cleanUrl } from "@/libs/clean-url" import { Descriptions } from "antd" +import fetcher from "@/libs/fetcher" export const AboutApp = () => { const { t } = useTranslation("settings") @@ -14,7 +15,7 @@ export const AboutApp = () => { const chromeVersion = browser.runtime.getManifest().version try { const url = await getOllamaURL() - const req = await fetch(`${cleanUrl(url)}/api/version`) + const req = await fetcher(`${cleanUrl(url)}/api/version`) if (!req.ok) { return { diff --git a/src/components/Option/Share/index.tsx b/src/components/Option/Share/index.tsx index f0db8a6..ac52eb1 100644 --- a/src/components/Option/Share/index.tsx +++ b/src/components/Option/Share/index.tsx @@ -7,6 +7,7 @@ import { deleteWebshare, getAllWebshares, getUserId } from "@/db" import { getPageShareUrl, setPageShareUrl } from "~/services/ollama" import { verifyPageShareURL } from "~/utils/verify-page-share" import { useStorage } from "@plasmohq/storage/hook" +import fetcher from "@/libs/fetcher" export const OptionShareBody = () => { const queryClient = useQueryClient() @@ -48,7 +49,7 @@ export const OptionShareBody = () => { api_url: string }) => { const owner_id = await getUserId() - const res = await fetch(`${api_url}/api/v1/share/delete`, { + const res = await fetcher(`${api_url}/api/v1/share/delete`, { method: "POST", headers: { "Content-Type": "application/json" diff --git a/src/libs/fetcher.ts b/src/libs/fetcher.ts new file mode 100644 index 0000000..1d7327c --- /dev/null +++ b/src/libs/fetcher.ts @@ -0,0 +1,14 @@ +import { getCustomOllamaHeaders } from "@/services/app" + + +const fetcher = async (input: string | URL | globalThis.Request, init?: RequestInit) : Promise => { + const update = {...init} || {} + const customHeaders = await getCustomOllamaHeaders() + update.headers = { + ...customHeaders, + ...update?.headers + } + return fetch(input, update) +} + +export default fetcher \ No newline at end of file diff --git a/src/services/ollama.ts b/src/services/ollama.ts index 7b0a6a8..7ec4f69 100644 --- a/src/services/ollama.ts +++ b/src/services/ollama.ts @@ -3,6 +3,8 @@ import { cleanUrl } from "../libs/clean-url" import { urlRewriteRuntime } from "../libs/runtime" import { getChromeAIModel } from "./chrome" import { setNoOfRetrievedDocs, setTotalFilePerKB } from "./app" +import fetcher from "@/libs/fetcher" + const storage = new Storage() @@ -15,9 +17,9 @@ const DEFAULT_RAG_QUESTION_PROMPT = const DEFAUTL_RAG_SYSTEM_PROMPT = `You are a helpful AI assistant. Use the following pieces of context to answer the question at the end. If you don't know the answer, just say you don't know. DO NOT try to make up an answer. If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context. {context} Question: {question} Helpful answer:` -const DEFAULT_WEBSEARCH_PROMP = `You are an AI model who is expert at searching the web and answering user's queries. +const DEFAULT_WEBSEARCH_PROMP = `You are an AI model who is expert at searching the web and answering user's queries. -Generate a response that is informative and relevant to the user's query based on provided search results. the current date and time are {current_date_time}. +Generate a response that is informative and relevant to the user's query based on provided search results. the current date and time are {current_date_time}. \`search-results\` block provides knowledge from the web search results. You can use this information to generate a meaningful response. @@ -43,7 +45,7 @@ Follow-up question: Taylor Swift's latest album? Rephrased question: Name of Taylor Swift's latest album. -Previous Conversation: +Previous Conversation: {chat_history} @@ -82,7 +84,7 @@ export const defaultModel = async () => { export const isOllamaRunning = async () => { try { const baseUrl = await getOllamaURL() - const response = await fetch(`${cleanUrl(baseUrl)}`) + const response = await fetcher(`${cleanUrl(baseUrl)}`) if (!response.ok) { throw new Error(response.statusText) } @@ -100,7 +102,7 @@ export const getAllModels = async ({ }) => { try { const baseUrl = await getOllamaURL() - const response = await fetch(`${cleanUrl(baseUrl)}/api/tags`) + const response = await fetcher(`${cleanUrl(baseUrl)}/api/tags`) if (!response.ok) { if (returnEmpty) { return [] @@ -132,7 +134,7 @@ export const getAllModels = async ({ export const deleteModel = async (model: string) => { const baseUrl = await getOllamaURL() - const response = await fetch(`${cleanUrl(baseUrl)}/api/delete`, { + const response = await fetcher(`${cleanUrl(baseUrl)}/api/delete`, { method: "DELETE", headers: { "Content-Type": "application/json" @@ -154,7 +156,7 @@ export const fetchChatModels = async ({ }) => { try { const baseUrl = await getOllamaURL() - const response = await fetch(`${cleanUrl(baseUrl)}/api/tags`) + const response = await fetcher(`${cleanUrl(baseUrl)}/api/tags`) if (!response.ok) { if (returnEmpty) { return [] diff --git a/src/utils/pull-ollama.ts b/src/utils/pull-ollama.ts index d15ff2b..310c600 100644 --- a/src/utils/pull-ollama.ts +++ b/src/utils/pull-ollama.ts @@ -1,5 +1,5 @@ import { setBadgeBackgroundColor, setBadgeText, setTitle } from "@/utils/action" - +import fetcher from "@/libs/fetcher" export const progressHuman = (completed: number, total: number) => { return ((completed / total) * 100).toFixed(0) + "%" @@ -11,7 +11,7 @@ export const clearBadge = () => { } export const streamDownload = async (url: string, model: string) => { url += "/api/pull" - const response = await fetch(url, { + const response = await fetcher(url, { method: "POST", headers: { "Content-Type": "application/json" diff --git a/src/utils/verify-page-share.ts b/src/utils/verify-page-share.ts index 9a509ce..1586ecd 100644 --- a/src/utils/verify-page-share.ts +++ b/src/utils/verify-page-share.ts @@ -1,7 +1,8 @@ import { cleanUrl } from "~/libs/clean-url" +import fetcher from "@/libs/fetcher" export const verifyPageShareURL = async (url: string) => { - const res = await fetch(`${cleanUrl(url)}/api/v1/ping`) + const res = await fetcher(`${cleanUrl(url)}/api/v1/ping`) if (!res.ok) { throw new Error("Unable to verify page share") }