Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
e5cbb8cd
Unverified
Commit
e5cbb8cd
authored
Mar 11, 2023
by
boojack
Committed by
GitHub
Mar 11, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: openAI config system setting (#1333)
parent
7c92805a
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
108 additions
and
181 deletions
+108
-181
system.go
api/system.go
+0
-2
system_setting.go
api/system_setting.go
+15
-48
openai.go
server/openai.go
+29
-56
system.go
server/system.go
+13
-30
SystemSection.tsx
web/src/components/Settings/SystemSection.tsx
+32
-43
api.ts
web/src/helpers/api.ts
+4
-0
global.ts
web/src/store/module/global.ts
+10
-1
system.d.ts
web/src/types/modules/system.d.ts
+5
-1
No files found.
api/system.go
View file @
e5cbb8cd
...
...
@@ -19,6 +19,4 @@ type SystemStatus struct {
// Customized server profile, including server name and external url.
CustomizedProfile
CustomizedProfile
`json:"customizedProfile"`
StorageServiceID
int
`json:"storageServiceId"`
// OpenAI API Host
OpenAIAPIHost
string
`json:"openAIApiHost"`
}
api/system_setting.go
View file @
e5cbb8cd
...
...
@@ -27,10 +27,8 @@ const (
SystemSettingCustomizedProfileName
SystemSettingName
=
"customizedProfile"
// SystemSettingStorageServiceIDName is the key type of storage service ID.
SystemSettingStorageServiceIDName
SystemSettingName
=
"storageServiceId"
// SystemSettingOpenAIAPIKeyName is the key type of OpenAI API key.
SystemSettingOpenAIAPIKeyName
SystemSettingName
=
"openAIApiKey"
// SystemSettingOpenAIAPIHost is the key type of OpenAI API path.
SystemSettingOpenAIAPIHost
SystemSettingName
=
"openAIApiHost"
// SystemSettingOpenAIConfigName is the key type of OpenAI config.
SystemSettingOpenAIConfigName
SystemSettingName
=
"openAIConfig"
)
// CustomizedProfile is the struct definition for SystemSettingCustomizedProfileName system setting item.
...
...
@@ -49,6 +47,11 @@ type CustomizedProfile struct {
ExternalURL
string
`json:"externalUrl"`
}
type
OpenAIConfig
struct
{
Key
string
`json:"key"`
Host
string
`json:"host"`
}
func
(
key
SystemSettingName
)
String
()
string
{
switch
key
{
case
SystemSettingServerID
:
...
...
@@ -67,24 +70,17 @@ func (key SystemSettingName) String() string {
return
"customizedProfile"
case
SystemSettingStorageServiceIDName
:
return
"storageServiceId"
case
SystemSettingOpenAIAPIKeyName
:
return
"openAIApiKey"
case
SystemSettingOpenAIAPIHost
:
return
"openAIApiHost"
case
SystemSettingOpenAIConfigName
:
return
"openAIConfig"
}
return
""
}
var
(
SystemSettingAllowSignUpValue
=
[]
bool
{
true
,
false
}
SystemSettingDisablePublicMemosValue
=
[]
bool
{
true
,
false
}
)
type
SystemSetting
struct
{
Name
SystemSettingName
Name
SystemSettingName
`json:"name"`
// Value is a JSON string with basic value.
Value
string
Description
string
Value
string
`json:"value"`
Description
string
`json:"description"`
}
type
SystemSettingUpsert
struct
{
...
...
@@ -102,35 +98,12 @@ func (upsert SystemSettingUpsert) Validate() error {
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to unmarshal system setting allow signup value"
)
}
invalid
:=
true
for
_
,
v
:=
range
SystemSettingAllowSignUpValue
{
if
value
==
v
{
invalid
=
false
break
}
}
if
invalid
{
return
fmt
.
Errorf
(
"invalid system setting allow signup value"
)
}
}
else
if
upsert
.
Name
==
SystemSettingDisablePublicMemosName
{
value
:=
false
err
:=
json
.
Unmarshal
([]
byte
(
upsert
.
Value
),
&
value
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to unmarshal system setting disable public memos value"
)
}
invalid
:=
true
for
_
,
v
:=
range
SystemSettingDisablePublicMemosValue
{
if
value
==
v
{
invalid
=
false
break
}
}
if
invalid
{
return
fmt
.
Errorf
(
"invalid system setting disable public memos value"
)
}
}
else
if
upsert
.
Name
==
SystemSettingAdditionalStyleName
{
value
:=
""
err
:=
json
.
Unmarshal
([]
byte
(
upsert
.
Value
),
&
value
)
...
...
@@ -169,17 +142,11 @@ func (upsert SystemSettingUpsert) Validate() error {
return
fmt
.
Errorf
(
"failed to unmarshal system setting storage service id value"
)
}
return
nil
}
else
if
upsert
.
Name
==
SystemSettingOpenAIAPIKeyName
{
value
:=
""
err
:=
json
.
Unmarshal
([]
byte
(
upsert
.
Value
),
&
value
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to unmarshal system setting openai api key value"
)
}
}
else
if
upsert
.
Name
==
SystemSettingOpenAIAPIHost
{
value
:=
""
}
else
if
upsert
.
Name
==
SystemSettingOpenAIConfigName
{
value
:=
OpenAIConfig
{}
err
:=
json
.
Unmarshal
([]
byte
(
upsert
.
Value
),
&
value
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to unmarshal system setting openai api
host
value"
)
return
fmt
.
Errorf
(
"failed to unmarshal system setting openai api
config
value"
)
}
}
else
{
return
fmt
.
Errorf
(
"invalid system setting name"
)
...
...
server/openai.go
View file @
e5cbb8cd
...
...
@@ -13,39 +13,24 @@ import (
func
(
s
*
Server
)
registerOpenAIRoutes
(
g
*
echo
.
Group
)
{
g
.
POST
(
"/openai/chat-completion"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
openAI
ApiKey
Setting
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
api
.
SystemSettingOpenAI
APIKey
Name
,
openAI
Config
Setting
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
api
.
SystemSettingOpenAI
Config
Name
,
})
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find openai
api
key"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find openai key"
)
.
SetInternal
(
err
)
}
openAIApiHostSetting
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
api
.
SystemSettingOpenAIAPIHost
,
})
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find openai api host"
)
.
SetInternal
(
err
)
}
openAIApiKey
:=
""
if
openAIApiKeySetting
!=
nil
{
err
=
json
.
Unmarshal
([]
byte
(
openAIApiKeySetting
.
Value
),
&
openAIApiKey
)
openAIConfig
:=
api
.
OpenAIConfig
{}
if
openAIConfigSetting
!=
nil
{
err
=
json
.
Unmarshal
([]
byte
(
openAIConfigSetting
.
Value
),
&
openAIConfig
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal system setting value"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal
openai
system setting value"
)
.
SetInternal
(
err
)
}
}
if
openAI
Api
Key
==
""
{
if
openAI
Config
.
Key
==
""
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"OpenAI API key not set"
)
}
openAIApiHost
:=
""
if
openAIApiHostSetting
!=
nil
{
err
=
json
.
Unmarshal
([]
byte
(
openAIApiHostSetting
.
Value
),
&
openAIApiHost
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal system setting value"
)
.
SetInternal
(
err
)
}
}
completionRequest
:=
api
.
OpenAICompletionRequest
{}
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
&
completionRequest
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted post chat completion request"
)
.
SetInternal
(
err
)
...
...
@@ -54,7 +39,7 @@ func (s *Server) registerOpenAIRoutes(g *echo.Group) {
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Prompt is required"
)
}
result
,
err
:=
openai
.
PostChatCompletion
(
completionRequest
.
Prompt
,
openAI
ApiKey
,
openAIApi
Host
)
result
,
err
:=
openai
.
PostChatCompletion
(
completionRequest
.
Prompt
,
openAI
Config
.
Key
,
openAIConfig
.
Host
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to post chat completion"
)
.
SetInternal
(
err
)
}
...
...
@@ -64,39 +49,24 @@ func (s *Server) registerOpenAIRoutes(g *echo.Group) {
g
.
POST
(
"/openai/text-completion"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
openAIApiKeySetting
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
api
.
SystemSettingOpenAIAPIKeyName
,
})
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find openai api key"
)
.
SetInternal
(
err
)
}
openAIApiHostSetting
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
api
.
SystemSettingOpenAIAPIHost
,
openAIConfigSetting
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
api
.
SystemSettingOpenAIConfigName
,
})
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find openai
api host
"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find openai
key
"
)
.
SetInternal
(
err
)
}
openAI
ApiKey
:=
""
if
openAI
ApiKey
Setting
!=
nil
{
err
=
json
.
Unmarshal
([]
byte
(
openAI
ApiKeySetting
.
Value
),
&
openAIApiKey
)
openAI
Config
:=
api
.
OpenAIConfig
{}
if
openAI
Config
Setting
!=
nil
{
err
=
json
.
Unmarshal
([]
byte
(
openAI
ConfigSetting
.
Value
),
&
openAIConfig
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal system setting value"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal
openai
system setting value"
)
.
SetInternal
(
err
)
}
}
if
openAI
Api
Key
==
""
{
if
openAI
Config
.
Key
==
""
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"OpenAI API key not set"
)
}
openAIApiHost
:=
""
if
openAIApiHostSetting
!=
nil
{
err
=
json
.
Unmarshal
([]
byte
(
openAIApiHostSetting
.
Value
),
&
openAIApiHost
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal system setting value"
)
.
SetInternal
(
err
)
}
}
textCompletion
:=
api
.
OpenAICompletionRequest
{}
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
&
textCompletion
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted post text completion request"
)
.
SetInternal
(
err
)
...
...
@@ -105,7 +75,7 @@ func (s *Server) registerOpenAIRoutes(g *echo.Group) {
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Prompt is required"
)
}
result
,
err
:=
openai
.
PostTextCompletion
(
textCompletion
.
Prompt
,
openAI
ApiKey
,
openAIApi
Host
)
result
,
err
:=
openai
.
PostTextCompletion
(
textCompletion
.
Prompt
,
openAI
Config
.
Key
,
openAIConfig
.
Host
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to post text completion"
)
.
SetInternal
(
err
)
}
...
...
@@ -115,21 +85,24 @@ func (s *Server) registerOpenAIRoutes(g *echo.Group) {
g
.
GET
(
"/openai/enabled"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
openAI
ApiKey
Setting
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
api
.
SystemSettingOpenAI
APIKey
Name
,
openAI
Config
Setting
,
err
:=
s
.
Store
.
FindSystemSetting
(
ctx
,
&
api
.
SystemSettingFind
{
Name
:
api
.
SystemSettingOpenAI
Config
Name
,
})
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find openai
api
key"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find openai key"
)
.
SetInternal
(
err
)
}
openAI
ApiKey
:=
""
if
openAI
ApiKey
Setting
!=
nil
{
err
=
json
.
Unmarshal
([]
byte
(
openAI
ApiKeySetting
.
Value
),
&
openAIApiKey
)
openAI
Config
:=
api
.
OpenAIConfig
{}
if
openAI
Config
Setting
!=
nil
{
err
=
json
.
Unmarshal
([]
byte
(
openAI
ConfigSetting
.
Value
),
&
openAIConfig
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal system setting value"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal
openai
system setting value"
)
.
SetInternal
(
err
)
}
}
if
openAIConfig
.
Key
==
""
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"OpenAI API key not set"
)
}
return
c
.
JSON
(
http
.
StatusOK
,
composeResponse
(
openAI
Api
Key
!=
""
))
return
c
.
JSON
(
http
.
StatusOK
,
composeResponse
(
openAI
Config
.
Key
!=
""
))
})
}
server/system.go
View file @
e5cbb8cd
...
...
@@ -52,7 +52,6 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
ExternalURL
:
""
,
},
StorageServiceID
:
0
,
OpenAIAPIHost
:
""
,
}
systemSettingList
,
err
:=
s
.
Store
.
FindSystemSettingList
(
ctx
,
&
api
.
SystemSettingFind
{})
...
...
@@ -60,49 +59,33 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find system setting list"
)
.
SetInternal
(
err
)
}
for
_
,
systemSetting
:=
range
systemSettingList
{
if
systemSetting
.
Name
==
api
.
SystemSettingServerID
||
systemSetting
.
Name
==
api
.
SystemSettingSecretSessionName
||
systemSetting
.
Name
==
api
.
SystemSettingOpenAI
APIKey
Name
{
if
systemSetting
.
Name
==
api
.
SystemSettingServerID
||
systemSetting
.
Name
==
api
.
SystemSettingSecretSessionName
||
systemSetting
.
Name
==
api
.
SystemSettingOpenAI
Config
Name
{
continue
}
var
v
alue
interface
{}
err
:=
json
.
Unmarshal
([]
byte
(
systemSetting
.
Value
),
&
v
alue
)
var
baseV
alue
interface
{}
err
:=
json
.
Unmarshal
([]
byte
(
systemSetting
.
Value
),
&
baseV
alue
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal system setting value"
)
.
SetInternal
(
err
)
}
if
systemSetting
.
Name
==
api
.
SystemSettingAllowSignUpName
{
systemStatus
.
AllowSignUp
=
v
alue
.
(
bool
)
systemStatus
.
AllowSignUp
=
baseV
alue
.
(
bool
)
}
else
if
systemSetting
.
Name
==
api
.
SystemSettingDisablePublicMemosName
{
systemStatus
.
DisablePublicMemos
=
v
alue
.
(
bool
)
systemStatus
.
DisablePublicMemos
=
baseV
alue
.
(
bool
)
}
else
if
systemSetting
.
Name
==
api
.
SystemSettingAdditionalStyleName
{
systemStatus
.
AdditionalStyle
=
v
alue
.
(
string
)
systemStatus
.
AdditionalStyle
=
baseV
alue
.
(
string
)
}
else
if
systemSetting
.
Name
==
api
.
SystemSettingAdditionalScriptName
{
systemStatus
.
AdditionalScript
=
v
alue
.
(
string
)
systemStatus
.
AdditionalScript
=
baseV
alue
.
(
string
)
}
else
if
systemSetting
.
Name
==
api
.
SystemSettingCustomizedProfileName
{
valueMap
:=
value
.
(
map
[
string
]
interface
{})
systemStatus
.
CustomizedProfile
=
api
.
CustomizedProfile
{}
if
v
:=
valueMap
[
"name"
];
v
!=
nil
{
systemStatus
.
CustomizedProfile
.
Name
=
v
.
(
string
)
}
if
v
:=
valueMap
[
"logoUrl"
];
v
!=
nil
{
systemStatus
.
CustomizedProfile
.
LogoURL
=
v
.
(
string
)
}
if
v
:=
valueMap
[
"description"
];
v
!=
nil
{
systemStatus
.
CustomizedProfile
.
Description
=
v
.
(
string
)
}
if
v
:=
valueMap
[
"locale"
];
v
!=
nil
{
systemStatus
.
CustomizedProfile
.
Locale
=
v
.
(
string
)
}
if
v
:=
valueMap
[
"appearance"
];
v
!=
nil
{
systemStatus
.
CustomizedProfile
.
Appearance
=
v
.
(
string
)
}
if
v
:=
valueMap
[
"externalUrl"
];
v
!=
nil
{
systemStatus
.
CustomizedProfile
.
ExternalURL
=
v
.
(
string
)
customizedProfile
:=
api
.
CustomizedProfile
{}
err
:=
json
.
Unmarshal
([]
byte
(
systemSetting
.
Value
),
&
customizedProfile
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal system setting customized profile value"
)
.
SetInternal
(
err
)
}
systemStatus
.
CustomizedProfile
=
customizedProfile
}
else
if
systemSetting
.
Name
==
api
.
SystemSettingStorageServiceIDName
{
systemStatus
.
StorageServiceID
=
int
(
value
.
(
float64
))
}
else
if
systemSetting
.
Name
==
api
.
SystemSettingOpenAIAPIHost
{
systemStatus
.
OpenAIAPIHost
=
value
.
(
string
)
systemStatus
.
StorageServiceID
=
int
(
baseValue
.
(
float64
))
}
}
...
...
web/src/components/Settings/SystemSection.tsx
View file @
e5cbb8cd
...
...
@@ -5,16 +5,12 @@ import { Button, Divider, Input, Switch, Textarea } from "@mui/joy";
import
{
useGlobalStore
}
from
"../../store/module"
;
import
*
as
api
from
"../../helpers/api"
;
import
showUpdateCustomizedProfileDialog
from
"../UpdateCustomizedProfileDialog"
;
import
{
useAppDispatch
}
from
"../../store"
;
import
{
setGlobalState
}
from
"../../store/reducer/global"
;
import
"@/less/settings/system-section.less"
;
interface
State
{
dbSize
:
number
;
allowSignUp
:
boolean
;
disablePublicMemos
:
boolean
;
openAIApiKey
:
string
;
openAIApiHost
:
string
;
additionalStyle
:
string
;
additionalScript
:
string
;
}
...
...
@@ -36,13 +32,13 @@ const SystemSection = () => {
dbSize
:
systemStatus
.
dbSize
,
allowSignUp
:
systemStatus
.
allowSignUp
,
additionalStyle
:
systemStatus
.
additionalStyle
,
openAIApiKey
:
""
,
openAIApiHost
:
systemStatus
.
openAIApiHost
,
additionalScript
:
systemStatus
.
additionalScript
,
disablePublicMemos
:
systemStatus
.
disablePublicMemos
,
});
const
dispatch
=
useAppDispatch
();
const
[
openAIConfig
,
setOpenAIConfig
]
=
useState
<
OpenAIConfig
>
({
key
:
""
,
host
:
""
,
});
useEffect
(()
=>
{
globalStore
.
fetchSystemStatus
();
...
...
@@ -50,16 +46,24 @@ const SystemSection = () => {
useEffect
(()
=>
{
setState
({
...
state
,
dbSize
:
systemStatus
.
dbSize
,
allowSignUp
:
systemStatus
.
allowSignUp
,
additionalStyle
:
systemStatus
.
additionalStyle
,
openAIApiKey
:
""
,
openAIApiHost
:
systemStatus
.
openAIApiHost
,
additionalScript
:
systemStatus
.
additionalScript
,
disablePublicMemos
:
systemStatus
.
disablePublicMemos
,
});
},
[
systemStatus
]);
useEffect
(()
=>
{
api
.
getSystemSetting
().
then
(({
data
:
{
data
:
systemSettings
}
})
=>
{
const
openAIConfigSetting
=
systemSettings
.
find
((
setting
)
=>
setting
.
name
===
"openAIConfig"
);
if
(
openAIConfigSetting
)
{
setOpenAIConfig
(
JSON
.
parse
(
openAIConfigSetting
.
value
));
}
});
},
[]);
const
handleAllowSignUpChanged
=
async
(
value
:
boolean
)
=>
{
setState
({
...
state
,
...
...
@@ -86,44 +90,31 @@ const SystemSection = () => {
toast
.
success
(
t
(
"message.succeed-vacuum-database"
));
};
const
handleOpenAI
Api
KeyChanged
=
(
value
:
string
)
=>
{
set
State
({
...
state
,
openAIApiK
ey
:
value
,
const
handleOpenAI
Config
KeyChanged
=
(
value
:
string
)
=>
{
set
OpenAIConfig
({
...
openAIConfig
,
k
ey
:
value
,
});
};
const
handleSaveOpenAI
ApiKey
=
async
()
=>
{
const
handleSaveOpenAI
Config
=
async
()
=>
{
try
{
await
api
.
upsertSystemSetting
({
name
:
"openAI
ApiKey
"
,
value
:
JSON
.
stringify
(
state
.
openAIApiKey
),
name
:
"openAI
Config
"
,
value
:
JSON
.
stringify
(
openAIConfig
),
});
}
catch
(
error
)
{
console
.
error
(
error
);
return
;
}
toast
.
success
(
"OpenAI
Api Key
updated"
);
toast
.
success
(
"OpenAI
Config
updated"
);
};
const
handleOpenAIApiHostChanged
=
(
value
:
string
)
=>
{
setState
({
...
state
,
openAIApiHost
:
value
,
});
};
const
handleSaveOpenAIApiHost
=
async
()
=>
{
try
{
await
api
.
upsertSystemSetting
({
name
:
"openAIApiHost"
,
value
:
JSON
.
stringify
(
state
.
openAIApiHost
),
const
handleOpenAIConfigHostChanged
=
(
value
:
string
)
=>
{
setOpenAIConfig
({
...
openAIConfig
,
host
:
value
,
});
}
catch
(
error
)
{
console
.
error
(
error
);
return
;
}
toast
.
success
(
"OpenAI Api Host updated"
);
};
const
handleAdditionalStyleChanged
=
(
value
:
string
)
=>
{
...
...
@@ -171,8 +162,7 @@ const SystemSection = () => {
...
state
,
disablePublicMemos
:
value
,
});
// Update global store immediately as MemoEditor/Selector is dependent on this value.
dispatch
(
setGlobalState
({
systemStatus
:
{
...
systemStatus
,
disablePublicMemos
:
value
}
}));
globalStore
.
setSystemStatus
({
disablePublicMemos
:
value
});
await
api
.
upsertSystemSetting
({
name
:
"disablePublicMemos"
,
value
:
JSON
.
stringify
(
value
),
...
...
@@ -206,7 +196,7 @@ const SystemSection = () => {
<
Divider
className=
"!mt-3 !my-4"
/>
<
div
className=
"form-label"
>
<
span
className=
"normal-text"
>
OpenAI API Key
</
span
>
<
Button
onClick=
{
handleSaveOpenAI
ApiKey
}
>
{
t
(
"common.save"
)
}
</
Button
>
<
Button
onClick=
{
handleSaveOpenAI
Config
}
>
{
t
(
"common.save"
)
}
</
Button
>
</
div
>
<
Input
className=
"w-full"
...
...
@@ -215,12 +205,11 @@ const SystemSection = () => {
fontSize
:
"14px"
,
}
}
placeholder=
"Write only"
value=
{
state
.
openAIApiK
ey
}
onChange=
{
(
event
)
=>
handleOpenAI
Api
KeyChanged
(
event
.
target
.
value
)
}
value=
{
openAIConfig
.
k
ey
}
onChange=
{
(
event
)
=>
handleOpenAI
Config
KeyChanged
(
event
.
target
.
value
)
}
/>
<
div
className=
"form-label mt-2"
>
<
span
className=
"normal-text"
>
OpenAI API Host
</
span
>
<
Button
onClick=
{
handleSaveOpenAIApiHost
}
>
{
t
(
"common.save"
)
}
</
Button
>
</
div
>
<
Input
className=
"w-full"
...
...
@@ -229,8 +218,8 @@ const SystemSection = () => {
fontSize
:
"14px"
,
}
}
placeholder=
"OpenAI Host. Default: https://api.openai.com"
value=
{
state
.
openAIApiH
ost
}
onChange=
{
(
event
)
=>
handleOpenAI
Api
HostChanged
(
event
.
target
.
value
)
}
value=
{
openAIConfig
.
h
ost
}
onChange=
{
(
event
)
=>
handleOpenAI
Config
HostChanged
(
event
.
target
.
value
)
}
/>
<
Divider
className=
"!mt-3 !my-4"
/>
<
div
className=
"form-label"
>
...
...
web/src/helpers/api.ts
View file @
e5cbb8cd
...
...
@@ -10,6 +10,10 @@ export function getSystemStatus() {
return
axios
.
get
<
ResponseObject
<
SystemStatus
>>
(
"/api/status"
);
}
export
function
getSystemSetting
()
{
return
axios
.
get
<
ResponseObject
<
SystemSetting
[]
>>
(
"/api/system/setting"
);
}
export
function
upsertSystemSetting
(
systemSetting
:
SystemSetting
)
{
return
axios
.
post
<
ResponseObject
<
SystemSetting
>>
(
"/api/system/setting"
,
systemSetting
);
}
...
...
web/src/store/module/global.ts
View file @
e5cbb8cd
...
...
@@ -22,7 +22,6 @@ export const initialGlobalState = async () => {
appearance
:
"system"
,
externalUrl
:
""
,
},
openAIApiHost
:
""
,
}
as
SystemStatus
,
};
...
...
@@ -75,6 +74,16 @@ export const useGlobalStore = () => {
store
.
dispatch
(
setGlobalState
({
systemStatus
:
systemStatus
}));
return
systemStatus
;
},
setSystemStatus
:
(
systemStatus
:
Partial
<
SystemStatus
>
)
=>
{
store
.
dispatch
(
setGlobalState
({
systemStatus
:
{
...
state
.
systemStatus
,
...
systemStatus
,
},
})
);
},
setLocale
:
(
locale
:
Locale
)
=>
{
store
.
dispatch
(
setLocale
(
locale
));
},
...
...
web/src/types/modules/system.d.ts
View file @
e5cbb8cd
...
...
@@ -12,6 +12,11 @@ interface CustomizedProfile {
externalUrl
:
string
;
}
interface
OpenAIConfig
{
key
:
string
;
host
:
string
;
}
interface
SystemStatus
{
host
?:
User
;
profile
:
Profile
;
...
...
@@ -23,7 +28,6 @@ interface SystemStatus {
additionalScript
:
string
;
customizedProfile
:
CustomizedProfile
;
storageServiceId
:
number
;
openAIApiHost
:
string
;
}
interface
SystemSetting
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment