mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-03 06:12:14 +08:00
chore: add settings store
This commit is contained in:
@@ -2,3 +2,4 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
export * from "./store";
|
||||
export * from "./settings-store";
|
||||
|
||||
58
web/src/core/store/settings-store.ts
Normal file
58
web/src/core/store/settings-store.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { create } from "zustand";
|
||||
|
||||
const SETTINGS_KEY = "deerflow.settings";
|
||||
|
||||
const DEFAULT_SETTINGS: SettingsState = {
|
||||
general: {
|
||||
maxPlanIterations: 1,
|
||||
maxStepNum: 3,
|
||||
},
|
||||
};
|
||||
|
||||
export type SettingsState = {
|
||||
general: {
|
||||
maxPlanIterations: number;
|
||||
maxStepNum: number;
|
||||
};
|
||||
};
|
||||
|
||||
export const useSettingsStore = create<SettingsState>(() => ({
|
||||
...DEFAULT_SETTINGS,
|
||||
}));
|
||||
|
||||
export const useSettings = (key: keyof SettingsState) => {
|
||||
return useSettingsStore((state) => state[key]);
|
||||
};
|
||||
|
||||
export const changeSettings = (settings: SettingsState) => {
|
||||
useSettingsStore.setState(settings);
|
||||
};
|
||||
|
||||
export const loadSettings = () => {
|
||||
if (typeof window === "undefined") {
|
||||
return;
|
||||
}
|
||||
const json = localStorage.getItem(SETTINGS_KEY);
|
||||
if (json) {
|
||||
const settings = JSON.parse(json);
|
||||
for (const key in DEFAULT_SETTINGS) {
|
||||
if (!(key in settings)) {
|
||||
settings[key] = DEFAULT_SETTINGS[key as keyof SettingsState];
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
useSettingsStore.setState(settings);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const saveSettings = () => {
|
||||
const latestSettings = useSettingsStore.getState();
|
||||
const json = JSON.stringify(latestSettings);
|
||||
localStorage.setItem(SETTINGS_KEY, json);
|
||||
};
|
||||
|
||||
loadSettings();
|
||||
@@ -10,6 +10,8 @@ import type { Message } from "../messages";
|
||||
import { mergeMessage } from "../messages";
|
||||
import { parseJSON } from "../utils";
|
||||
|
||||
import { useSettingsStore } from "./settings-store";
|
||||
|
||||
const THREAD_ID = nanoid();
|
||||
|
||||
export const useStore = create<{
|
||||
@@ -39,12 +41,8 @@ export const useStore = create<{
|
||||
export async function sendMessage(
|
||||
content: string,
|
||||
{
|
||||
maxPlanIterations = 1,
|
||||
maxStepNum = 3,
|
||||
interruptFeedback,
|
||||
}: {
|
||||
maxPlanIterations?: number;
|
||||
maxStepNum?: number;
|
||||
interruptFeedback?: string;
|
||||
} = {},
|
||||
options: { abortSignal?: AbortSignal } = {},
|
||||
@@ -59,12 +57,13 @@ export async function sendMessage(
|
||||
|
||||
setResponding(true);
|
||||
try {
|
||||
const generalSettings = useSettingsStore.getState().general;
|
||||
const stream = chatStream(
|
||||
content,
|
||||
{
|
||||
thread_id: THREAD_ID,
|
||||
max_plan_iterations: maxPlanIterations,
|
||||
max_step_num: maxStepNum,
|
||||
max_plan_iterations: generalSettings.maxPlanIterations,
|
||||
max_step_num: generalSettings.maxStepNum,
|
||||
interrupt_feedback: interruptFeedback,
|
||||
},
|
||||
options,
|
||||
|
||||
Reference in New Issue
Block a user