ChatGPT-Next-Web/app/store/mask.ts

127 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-04-26 17:16:21 +00:00
import { BUILTIN_MASKS } from "../masks";
2023-04-23 17:15:44 +00:00
import { getLang, Lang } from "../locales";
import { DEFAULT_TOPIC, ChatMessage } from "./chat";
2023-06-23 16:18:27 +00:00
import { ModelConfig, useAppConfig } from "./config";
2023-04-26 18:00:22 +00:00
import { StoreKey } from "../constant";
2023-07-09 11:37:42 +00:00
import { nanoid } from "nanoid";
import { createPersistStore } from "../utils/store";
2023-04-23 17:15:44 +00:00
export type Mask = {
2023-07-09 11:37:42 +00:00
id: string;
createdAt: number;
2023-04-23 17:15:44 +00:00
avatar: string;
name: string;
hideContext?: boolean;
context: ChatMessage[];
syncGlobalConfig?: boolean;
2023-04-24 16:49:27 +00:00
modelConfig: ModelConfig;
2023-04-23 17:15:44 +00:00
lang: Lang;
2023-04-26 17:16:21 +00:00
builtin: boolean;
2023-04-23 17:15:44 +00:00
};
export const DEFAULT_MASK_STATE = {
2023-07-09 11:37:42 +00:00
masks: {} as Record<string, Mask>,
2023-04-23 17:15:44 +00:00
};
export type MaskState = typeof DEFAULT_MASK_STATE;
2023-04-24 16:49:27 +00:00
export const DEFAULT_MASK_AVATAR = "gpt-bot";
export const createEmptyMask = () =>
({
2023-07-09 11:37:42 +00:00
id: nanoid(),
2023-04-24 16:49:27 +00:00
avatar: DEFAULT_MASK_AVATAR,
name: DEFAULT_TOPIC,
context: [],
syncGlobalConfig: true, // use global config as default
2023-04-25 18:02:46 +00:00
modelConfig: { ...useAppConfig.getState().modelConfig },
2023-04-24 16:49:27 +00:00
lang: getLang(),
2023-04-26 17:16:21 +00:00
builtin: false,
2023-07-09 11:37:42 +00:00
createdAt: Date.now(),
}) as Mask;
export const useMaskStore = createPersistStore(
{ ...DEFAULT_MASK_STATE },
2023-04-24 16:49:27 +00:00
(set, get) => ({
create(mask?: Partial<Mask>) {
const masks = get().masks;
const id = nanoid();
masks[id] = {
...createEmptyMask(),
...mask,
id,
builtin: false,
};
2023-04-23 17:15:44 +00:00
set(() => ({ masks }));
get().markUpdate();
2023-04-23 17:15:44 +00:00
return masks[id];
},
updateMask(id: string, updater: (mask: Mask) => void) {
const masks = get().masks;
const mask = masks[id];
if (!mask) return;
const updateMask = { ...mask };
updater(updateMask);
masks[id] = updateMask;
set(() => ({ masks }));
get().markUpdate();
},
delete(id: string) {
const masks = get().masks;
delete masks[id];
set(() => ({ masks }));
get().markUpdate();
},
2023-04-25 18:02:46 +00:00
get(id?: string) {
return get().masks[id ?? 1145141919810];
},
getAll() {
const userMasks = Object.values(get().masks).sort(
(a, b) => b.createdAt - a.createdAt,
);
const config = useAppConfig.getState();
if (config.hideBuiltinMasks) return userMasks;
const buildinMasks = BUILTIN_MASKS.map(
(m) =>
({
...m,
modelConfig: {
...config.modelConfig,
...m.modelConfig,
},
}) as Mask,
);
return userMasks.concat(buildinMasks);
},
search(text: string) {
return Object.values(get().masks);
},
}),
{
name: StoreKey.Mask,
version: 3.1,
2023-07-09 11:37:42 +00:00
migrate(state, version) {
const newState = JSON.parse(JSON.stringify(state)) as MaskState;
2023-07-09 11:37:42 +00:00
// migrate mask id to nanoid
if (version < 3) {
Object.values(newState.masks).forEach((m) => (m.id = nanoid()));
}
2023-07-09 11:37:42 +00:00
if (version < 3.1) {
const updatedMasks: Record<string, Mask> = {};
Object.values(newState.masks).forEach((m) => {
updatedMasks[m.id] = m;
});
newState.masks = updatedMasks;
}
2023-07-11 13:02:09 +00:00
return newState as any;
2023-04-23 17:15:44 +00:00
},
},
2023-04-23 17:15:44 +00:00
);