Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add relative path support #1576

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { REQUEST_TIMEOUT_MS } from "@/app/constant";
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";

import { ChatOptions, getHeaders, LLMApi, LLMUsage } from "../api";
import Locale from "../../locales";
import { prettyObject } from "@/app/utils/format";
import {
EventStreamContentType,
fetchEventSource,
} from "@microsoft/fetch-event-source";
import { prettyObject } from "@/app/utils/format";
import Locale from "../../locales";
import { ChatOptions, getHeaders, LLMApi, LLMUsage } from "../api";

export class ChatGPTApi implements LLMApi {
public ChatPath = "v1/chat/completions";
public UsagePath = "dashboard/billing/usage";
public SubsPath = "dashboard/billing/subscription";

path(path: string): string {
let openaiUrl = useAccessStore.getState().openaiUrl;
let openaiUrl = useAccessStore.getState().openaiUrl();
if (openaiUrl.endsWith("/")) {
openaiUrl = openaiUrl.slice(0, openaiUrl.length - 1);
}
Expand Down
8 changes: 4 additions & 4 deletions app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require("../polyfill");

import { useState, useEffect } from "react";
import { useEffect, useState } from "react";

import styles from "./home.module.scss";

Expand All @@ -16,13 +16,13 @@ import { Path, SlotID } from "../constant";
import { ErrorBoundary } from "./error";

import {
Route,
HashRouter as Router,
Routes,
Route,
useLocation,
} from "react-router-dom";
import { SideBar } from "./sidebar";
import { useAppConfig } from "../store/config";
import { SideBar } from "./sidebar";

export function Loading(props: { noLogo?: boolean }) {
return (
Expand Down Expand Up @@ -94,7 +94,7 @@ const loadAsyncGoogleFont = () => {
const linkEl = document.createElement("link");
linkEl.rel = "stylesheet";
linkEl.href =
"/google-fonts/css2?family=Noto+Sans+SC:wght@300;400;700;900&display=swap";
"./google-fonts/css2?family=Noto+Sans+SC:wght@300;400;700;900&display=swap";
document.head.appendChild(linkEl);
};

Expand Down
9 changes: 5 additions & 4 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @next/next/no-page-custom-font */
import { getBuildConfig } from "./config/build";
import "./styles/globals.scss";
import "./styles/markdown.scss";
import "./styles/highlight.scss";
import { getBuildConfig } from "./config/build";
import "./styles/markdown.scss";

const buildConfig = getBuildConfig();

Expand Down Expand Up @@ -33,8 +33,9 @@ export default function RootLayout({
<html lang="en">
<head>
<meta name="version" content={buildConfig.commitId} />
<link rel="manifest" href="/site.webmanifest"></link>
<script src="/serviceWorkerRegister.js" defer></script>
<link rel="manifest" href="./site.webmanifest"></link>
<script src="./serviceWorkerRegister.js" defer></script>
<link rel="icon" href="./favicon.ico" />
</head>
<body>{children}</body>
</html>
Expand Down
2 changes: 1 addition & 1 deletion app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const cn = {
WIP: "该功能仍在开发中……",
Error: {
Unauthorized:
"访问密码不正确或为空,请前往[设置](/#/settings)页输入正确的访问密码,或者填入你自己的 OpenAI API Key。",
"访问密码不正确或为空,请前往设置页输入正确的访问密码,或在设置页填入可用的 OpenAI API Key。",
},
ChatItem: {
ChatItemCount: (count: number) => `${count} 条对话`,
Expand Down
14 changes: 10 additions & 4 deletions app/store/access.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { StoreKey } from "../constant";
import { getHeaders } from "../client/api";
import { StoreKey } from "../constant";
import { BOT_HELLO } from "./chat";
import { ALL_MODELS } from "./config";

Expand All @@ -11,8 +11,9 @@ export interface AccessControlStore {

needCode: boolean;
hideUserApiKey: boolean;
openaiUrl: string;

openaiUrl: () => string;
configUrl: () => string;
updateToken: (_: string) => void;
updateCode: (_: string) => void;
enabledAccessControl: () => boolean;
Expand All @@ -29,8 +30,13 @@ export const useAccessStore = create<AccessControlStore>()(
accessCode: "",
needCode: true,
hideUserApiKey: false,
openaiUrl: "/api/openai/",

openaiUrl() {
return window.location.pathname + "api/openai/";
},
configUrl() {
return window.location.pathname + "api/config"; // DO NOT add additional slash
},
enabledAccessControl() {
get().fetch();

Expand All @@ -53,7 +59,7 @@ export const useAccessStore = create<AccessControlStore>()(
fetch() {
if (fetchState > 0) return;
fetchState = 1;
fetch("/api/config", {
fetch(this.configUrl(), {
method: "post",
body: null,
headers: {
Expand Down
1 change: 1 addition & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @type {import('next').NextConfig} */

const nextConfig = {
assetPrefix: './',
experimental: {
appDir: true,
},
Expand Down
2 changes: 1 addition & 1 deletion public/serviceWorkerRegister.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('/serviceWorker.js').then(function (registration) {
navigator.serviceWorker.register('./serviceWorker.js').then(function (registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function (err) {
console.error('ServiceWorker registration failed: ', err);
Expand Down
6 changes: 3 additions & 3 deletions public/site.webmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
"short_name": "ChatGPT",
"icons": [
{
"src": "/android-chrome-192x192.png",
"src": "./android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/android-chrome-512x512.png",
"src": "./android-chrome-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"start_url": "/",
"start_url": "./",
"theme_color": "#ffffff",
"background_color": "#ffffff",
"display": "standalone"
Expand Down