Skip to content

Commit

Permalink
feat: system config settings
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Dec 17, 2023
1 parent e3970c9 commit 06d2d75
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 87 deletions.
7 changes: 5 additions & 2 deletions adapter/midjourney/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ func (c *ChatInstance) CreateImagineRequest(prompt string) (*ImagineResponse, er
"mj-api-secret": c.GetApiSecret(),
},
ImagineRequest{
NotifyHook: fmt.Sprintf("%s/mj/notify", viper.GetString("system.domain")),
Prompt: prompt,
NotifyHook: fmt.Sprintf(
"%s/mj/notify",
viper.GetString("system.general.backend"),
),
Prompt: prompt,
},
)

Expand Down
11 changes: 8 additions & 3 deletions addition/web/duckduckgo.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package web

import (
"chat/channel"
"chat/utils"
"fmt"
"github.com/spf13/viper"
"net/url"
"strings"
)
Expand All @@ -30,8 +30,13 @@ func formatResponse(data *DDGResponse) string {
}

func CallDuckDuckGoAPI(query string) *DDGResponse {
query = url.QueryEscape(query)
data, err := utils.Get(fmt.Sprintf("%s/search?q=%s&max_results=%d", viper.GetString("system.ddg"), query, viper.GetInt("system.ddg_max_results")), nil)
data, err := utils.Get(fmt.Sprintf(
"%s/search?q=%s&max_results=%d",
channel.SystemInstance.GetSearchEndpoint(),
url.QueryEscape(query),
channel.SystemInstance.GetSearchQuery(),
), nil)

if err != nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion admin/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GetModelData(cache *redis.Client) ModelChartForm {

return ModelChartForm{
Date: getDates(dates),
Value: utils.EachNotNil[string, ModelData](channel.ManagerInstance.GetModels(), func(model string) *ModelData {
Value: utils.EachNotNil[string, ModelData](channel.ConduitInstance.GetModels(), func(model string) *ModelData {
data := ModelData{
Model: model,
Data: utils.Each[time.Time, int64](dates, func(date time.Time) int64 {
Expand Down
65 changes: 65 additions & 0 deletions app/src/admin/api/system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { CommonResponse } from "@/admin/utils.ts";
import { getErrorMessage } from "@/utils/base.ts";
import axios from "axios";

export type GeneralState = {
backend: string;
};

export type MailState = {
host: string;
port: number;
username: string;
password: string;
from: string;
};

export type SearchState = {
endpoint: string;
query: number;
};

export type SystemProps = {
general: GeneralState;
mail: MailState;
search: SearchState;
};

export type SystemResponse = CommonResponse & {
data?: SystemProps;
};

export async function getConfig(): Promise<SystemResponse> {
try {
const response = await axios.get("/admin/config/view");
return response.data as SystemResponse;
} catch (e) {
return { status: false, error: getErrorMessage(e) };
}
}

export async function setConfig(config: SystemProps): Promise<CommonResponse> {
try {
const response = await axios.post(`/admin/config/update`, config);
return response.data as CommonResponse;
} catch (e) {
return { status: false, error: getErrorMessage(e) };
}
}

export const initialSystemState: SystemProps = {
general: {
backend: "",
},
mail: {
host: "",
port: 465,
username: "",
password: "",
from: "",
},
search: {
endpoint: "https://duckduckgo-api.vercel.app",
query: 5,
},
};
9 changes: 0 additions & 9 deletions app/src/conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ export const supportModels: Model[] = [
auth: true,
tag: ["free", "official"],
},
{
id: "gpt-4-free",
name: "GPT-4 Free",
free: true,
auth: true,
tag: ["free", "unstable", "high-quality"],
},
{
id: "gpt-4-0613",
name: "GPT-4",
Expand Down Expand Up @@ -321,7 +314,6 @@ export const supportModels: Model[] = [
export const defaultModels = [
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-16k-0613",
"gpt-4-free",
"gpt-4-0613",
"gpt-4-1106-preview",

Expand Down Expand Up @@ -380,7 +372,6 @@ export const modelAvatars: Record<string, string> = {
"gpt-3.5-turbo-16k-0613": "gpt35turbo16k.webp",
"gpt-3.5-turbo-1106": "gpt35turbo16k.webp",
"gpt-4-0613": "gpt4.png",
"gpt-4-free": "gpt4.png",
"gpt-4-1106-preview": "gpt432k.webp",
"gpt-4-vision-preview": "gpt4v.png",
"gpt-4-all": "gpt4.png",
Expand Down
117 changes: 62 additions & 55 deletions app/src/routes/admin/System.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,27 @@ import { Input } from "@/components/ui/input.tsx";
import { useReducer } from "react";
import { formReducer } from "@/utils/form.ts";
import { NumberInput } from "@/components/ui/number-input.tsx";
import {
GeneralState,
getConfig,
initialSystemState,
MailState,
SearchState,
setConfig,
SystemProps,
} from "@/admin/api/system.ts";
import { useEffectAsync } from "@/utils/hook.ts";
import { toastState } from "@/admin/utils.ts";
import { useToast } from "@/components/ui/use-toast.ts";

type GeneralState = {
backend: string;
type CompProps<T> = {
data: T;
dispatch: (action: any) => void;
onChange: () => void;
};

function General() {
function General({ data, dispatch, onChange }: CompProps<GeneralState>) {
const { t } = useTranslation();
const [general, generalDispatch] = useReducer(formReducer<GeneralState>(), {
backend: "",
} as GeneralState);

return (
<Paragraph
Expand All @@ -36,10 +47,10 @@ function General() {
<ParagraphItem>
<Label>{t("admin.system.backend")}</Label>
<Input
value={general.backend}
value={data.backend}
onChange={(e) =>
generalDispatch({
type: "update:backend",
dispatch({
type: "update:general.backend",
value: e.target.value,
})
}
Expand All @@ -51,29 +62,16 @@ function General() {
</ParagraphDescription>
<ParagraphFooter>
<div className={`grow`} />
<Button size={`sm`} loading={true}>
<Button size={`sm`} loading={true} onClick={onChange}>
{t("admin.system.save")}
</Button>
</ParagraphFooter>
</Paragraph>
);
}

type MailState = {
host: string;
port: number;
username: string;
password: string;
};

function Mail() {
function Mail({ data, dispatch, onChange }: CompProps<MailState>) {
const { t } = useTranslation();
const [mail, mailDispatch] = useReducer(formReducer<MailState>(), {
host: "",
port: 465,
username: "",
password: "",
} as MailState);

return (
<Paragraph
Expand All @@ -84,10 +82,10 @@ function Mail() {
<ParagraphItem>
<Label>{t("admin.system.mailHost")}</Label>
<Input
value={mail.host}
value={data.host}
onChange={(e) =>
mailDispatch({
type: "update:host",
dispatch({
type: "update:mail.host",
value: e.target.value,
})
}
Expand All @@ -97,9 +95,9 @@ function Mail() {
<ParagraphItem>
<Label>{t("admin.system.mailPort")}</Label>
<NumberInput
value={mail.port}
value={data.port}
onValueChange={(value) =>
mailDispatch({ type: "update:port", value })
dispatch({ type: "update:mail.port", value })
}
placeholder={`465`}
min={0}
Expand All @@ -109,10 +107,10 @@ function Mail() {
<ParagraphItem>
<Label>{t("admin.system.mailUser")}</Label>
<Input
value={mail.username}
value={data.username}
onChange={(e) =>
mailDispatch({
type: "update:username",
dispatch({
type: "update:mail.username",
value: e.target.value,
})
}
Expand All @@ -122,10 +120,10 @@ function Mail() {
<ParagraphItem>
<Label>{t("admin.system.mailPass")}</Label>
<Input
value={mail.password}
value={data.password}
onChange={(e) =>
mailDispatch({
type: "update:password",
dispatch({
type: "update:mail.password",
value: e.target.value,
})
}
Expand All @@ -134,25 +132,16 @@ function Mail() {
</ParagraphItem>
<ParagraphFooter>
<div className={`grow`} />
<Button size={`sm`} loading={true}>
<Button size={`sm`} loading={true} onClick={onChange}>
{t("admin.system.save")}
</Button>
</ParagraphFooter>
</Paragraph>
);
}

type SearchState = {
endpoint: string;
query: number;
};

function Search() {
function Search({ data, dispatch, onChange }: CompProps<SearchState>) {
const { t } = useTranslation();
const [search, searchDispatch] = useReducer(formReducer<SearchState>(), {
endpoint: "https://duckduckgo-api.vercel.app",
query: 5,
} as SearchState);

return (
<Paragraph
Expand All @@ -163,10 +152,10 @@ function Search() {
<ParagraphItem>
<Label>{t("admin.system.searchEndpoint")}</Label>
<Input
value={search.endpoint}
value={data.endpoint}
onChange={(e) =>
searchDispatch({
type: "update:endpoint",
dispatch({
type: "update:search.endpoint",
value: e.target.value,
})
}
Expand All @@ -176,9 +165,9 @@ function Search() {
<ParagraphItem>
<Label>{t("admin.system.searchQuery")}</Label>
<NumberInput
value={search.query}
value={data.query}
onValueChange={(value) =>
searchDispatch({ type: "update:query", value })
dispatch({ type: "update:search.query", value })
}
placeholder={`5`}
min={0}
Expand All @@ -188,7 +177,7 @@ function Search() {
<ParagraphDescription>{t("admin.system.searchTip")}</ParagraphDescription>
<ParagraphFooter>
<div className={`grow`} />
<Button size={`sm`} loading={true}>
<Button size={`sm`} loading={true} onClick={onChange}>
{t("admin.system.save")}
</Button>
</ParagraphFooter>
Expand All @@ -198,6 +187,24 @@ function Search() {

function System() {
const { t } = useTranslation();
const { toast } = useToast();
const [data, setData] = useReducer(
formReducer<SystemProps>(),
initialSystemState,
);

const save = async () => {
const res = await setConfig(data);
toastState(toast, t, res, true);
};

useEffectAsync(async () => {
const res = await getConfig();
toastState(toast, t, res);
if (res.status) {
setData({ type: "set", value: res.data });
}
}, []);

return (
<div className={`system`}>
Expand All @@ -206,9 +213,9 @@ function System() {
<CardTitle>{t("admin.settings")}</CardTitle>
</CardHeader>
<CardContent className={`flex flex-col gap-1`}>
<General />
<Mail />
<Search />
<General data={data.general} dispatch={setData} onChange={save} />
<Mail data={data.mail} dispatch={setData} onChange={save} />
<Search data={data.search} dispatch={setData} onChange={save} />
</CardContent>
</Card>
</div>
Expand Down
7 changes: 7 additions & 0 deletions app/src/utils/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function inWaiting(duration: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, duration);
});
}
Loading

0 comments on commit 06d2d75

Please sign in to comment.