forked from chatboxai/chatbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.ts
More file actions
195 lines (176 loc) · 5.58 KB
/
store.ts
File metadata and controls
195 lines (176 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { useState, useEffect } from 'react'
import { Settings, createSession, Session, Message } from './types'
import * as defaults from './defaults'
import * as openai from './utils/openai-node'
import { v4 as uuidv4 } from 'uuid';
import { ThemeMode } from './theme';
import * as api from './api'
// setting store
export function getDefaultSettings(): Settings {
return {
openaiKey: '',
apiHost: 'https://api.openai.com',
model: "gpt-3.5-turbo",
maxContextSize: "4000",
maxTokens: "2048",
showWordCount: false,
showTokenCount: false,
showModelName: false,
theme: ThemeMode.System,
}
}
export async function readSettings(): Promise<Settings> {
const setting: Settings|undefined = await api.readStore('settings')
if (!setting) {
return getDefaultSettings()
}
// 兼容早期版本
if (!setting.apiHost) {
setting.apiHost = getDefaultSettings().apiHost
}
if (!setting.model) {
setting.model = getDefaultSettings().model
}
if (!setting.maxTokens) {
setting.maxTokens = getDefaultSettings().maxTokens
}
if (!setting.maxContextSize) {
setting.maxContextSize = getDefaultSettings().maxContextSize
}
if (setting.showWordCount === undefined) {
setting.showWordCount = getDefaultSettings().showWordCount
}
if (setting.showTokenCount === undefined) {
setting.showTokenCount = getDefaultSettings().showTokenCount
}
if (setting.showModelName === undefined) {
setting.showModelName = getDefaultSettings().showModelName
}
if (setting.theme === undefined) {
setting.theme = getDefaultSettings().theme;
}
return setting
}
export async function writeSettings(settings: Settings) {
if (!settings.apiHost) {
settings.apiHost = getDefaultSettings().apiHost
}
console.log('writeSettings.apiHost', settings.apiHost)
openai.setHost(settings.apiHost)
return api.writeStore('settings', settings)
}
// session store
export async function readSessions(settings: Settings): Promise<Session[]> {
let sessions: Session[] | undefined = await api.readStore('chat-sessions')
if (!sessions) {
return defaults.sessions
}
if (sessions.length === 0) {
return [createSession(settings.model)]
}
return sessions.map((s: any) => {
// 兼容旧版本的数据
if (!s.model) {
s.model = getDefaultSettings().model
}
return s
})
}
export async function writeSessions(sessions: Session[]) {
return api.writeStore('chat-sessions', sessions)
}
// react hook
export default function useStore() {
const [version, _setVersion] = useState('unknown')
useEffect(() => {
api.getVersion().then((version: any) => {
_setVersion(version)
})
}, [])
const [settings, _setSettings] = useState<Settings>(getDefaultSettings())
const [needSetting, setNeedSetting] = useState(false)
useEffect(() => {
readSettings().then((settings) => {
_setSettings(settings)
if (settings.openaiKey === '') {
setNeedSetting(true)
}
})
}, [])
const setSettings = (settings: Settings) => {
_setSettings(settings)
writeSettings(settings)
}
const [chatSessions, _setChatSessions] = useState<Session[]>([createSession(settings.model)])
const [currentSession, switchCurrentSession] = useState<Session>(chatSessions[0])
useEffect(() => {
readSessions(settings).then((sessions: Session[]) => {
_setChatSessions(sessions)
switchCurrentSession(sessions[0])
})
}, [])
const setSessions = (sessions: Session[]) => {
_setChatSessions(sessions)
writeSessions(sessions)
}
const deleteChatSession = (target: Session) => {
const sessions = chatSessions.filter((s) => s.id !== target.id)
if (sessions.length === 0) {
sessions.push(createSession(settings.model))
}
if (target.id === currentSession.id) {
switchCurrentSession(sessions[0])
}
setSessions(sessions)
}
const updateChatSession = (session: Session) => {
const sessions = chatSessions.map((s) => {
if (s.id === session.id) {
return session
}
return s
})
setSessions(sessions)
if (session.id === currentSession.id) {
switchCurrentSession(session)
}
}
const createChatSession = (session: Session, ix?: number) => {
const sessions = [...chatSessions, session]
setSessions(sessions)
switchCurrentSession(session)
}
const createEmptyChatSession = () => {
createChatSession(createSession(settings.model))
}
const setMessages = (session: Session, messages: Message[]) => {
updateChatSession({
...session,
messages,
})
}
const [toasts, _setToasts] = useState<{id: string, content: string}[]>([])
const addToast = (content: string) => {
const id = uuidv4()
_setToasts([...toasts, {id, content}])
}
const removeToast = (id: string) => {
_setToasts(toasts.filter((t) => t.id !== id))
}
return {
version,
settings,
setSettings,
needSetting,
chatSessions,
createChatSession,
updateChatSession,
deleteChatSession,
createEmptyChatSession,
currentSession,
switchCurrentSession,
toasts,
addToast,
removeToast,
}
}