paper-ai-release-24-07-21/app/store/slices/authSlice.ts

110 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-01-21 23:08:25 +08:00
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { Reference } from "@/utils/global";
2024-01-21 23:08:25 +08:00
export interface APIState {
apiKey: string;
referencesRedux: Reference[];
2024-01-23 12:10:03 +08:00
editorContent: string;
upsreamUrl: string;
2024-01-29 13:35:24 +08:00
systemPrompt: string;
2024-02-08 10:31:29 +08:00
showPaperManagement: boolean;
2024-01-21 23:08:25 +08:00
}
const initialState: APIState = {
apiKey: "sk-aiHrrRLYUUelHstX69E9484509254dBf92061d6744FfFaD1", //sk-ffe19ebe9fa44d00884330ff1c18cf82
referencesRedux: [],
2024-01-23 12:10:03 +08:00
editorContent: "",
upsreamUrl: "https://one.caifree.com", //https://api.openai.com https://one.caifree.com https://chatserver.3211000.xyz https://api.deepseek.com
2024-02-08 23:28:33 +08:00
systemPrompt: `作为论文写作助手,您的主要任务是根据用户提供的研究主题和上下文,以及相关的研究论文,来撰写和完善学术论文。在撰写过程中,请注意以下要点:
1.使
2.使 [1]***[1]*
3.
4.
5.使,
2024-02-08 23:28:33 +08:00
6.
...[1],...[2]`,
2024-02-08 10:31:29 +08:00
showPaperManagement: false,
2024-01-21 23:08:25 +08:00
};
export const authSlice = createSlice({
name: "auth",
initialState,
reducers: {
2024-02-08 10:31:29 +08:00
setShowPaperManagement: (state, action: PayloadAction<boolean>) => {
state.showPaperManagement = action.payload;
},
2024-01-21 23:08:25 +08:00
setApiKey: (state, action: PayloadAction<string>) => {
state.apiKey = action.payload;
},
setUpsreamUrl: (state, action: PayloadAction<string>) => {
state.upsreamUrl = action.payload;
},
2024-01-29 13:35:24 +08:00
setSystemPrompt: (state, action: PayloadAction<string>) => {
state.systemPrompt = action.payload;
},
2024-01-29 15:01:43 +08:00
setEditorContent: (state, action: PayloadAction<string>) => {
state.editorContent = action.payload;
},
addReferenceRedux: (state, action: PayloadAction<Reference>) => {
state.referencesRedux.push(action.payload);
},
addReferencesRedux: (
state,
action: PayloadAction<{ references: Reference[]; position?: number }>
) => {
const { references, position } = action.payload;
const insertPosition =
position !== undefined ? position : state.referencesRedux.length;
state.referencesRedux.splice(insertPosition, 0, ...references);
},
removeReferenceRedux: (state, action: PayloadAction<number>) => {
state.referencesRedux = state.referencesRedux.filter(
(_, i) => i !== action.payload
);
},
clearReferencesRedux: (state) => {
state.referencesRedux = [];
},
2024-02-08 10:31:29 +08:00
setReferencesRedux: (state, action: PayloadAction<Reference[]>) => {
state.referencesRedux = action.payload;
},
2024-01-29 15:01:43 +08:00
swapReferencesRedux: (
state,
action: PayloadAction<{ indexA: number; indexB: number }>
) => {
console.log("moveReference", state.referencesRedux); // 调试输出
const { indexA, indexB } = action.payload;
if (
indexA >= 0 &&
indexA < state.referencesRedux.length &&
indexB >= 0 &&
indexB < state.referencesRedux.length
) {
const newReferences = [...state.referencesRedux];
const temp = newReferences[indexA];
newReferences[indexA] = newReferences[indexB];
newReferences[indexB] = temp;
state.referencesRedux = newReferences;
}
2024-01-23 12:10:03 +08:00
},
2024-01-21 23:08:25 +08:00
},
});
// Action creators are generated for each case reducer function
export const {
2024-02-08 10:31:29 +08:00
setShowPaperManagement,
setApiKey,
setUpsreamUrl,
addReferenceRedux,
addReferencesRedux,
removeReferenceRedux,
clearReferencesRedux,
2024-01-23 12:10:03 +08:00
setEditorContent,
2024-02-08 10:31:29 +08:00
setReferencesRedux,
2024-01-29 13:35:24 +08:00
setSystemPrompt,
2024-01-29 15:01:43 +08:00
swapReferencesRedux,
} = authSlice.actions;
2024-01-21 23:08:25 +08:00
export const authReducer = authSlice.reducer;