chore: add settings store

This commit is contained in:
Li Xin
2025-04-23 21:18:33 +08:00
parent d0ed5772d7
commit 1e7036ed90
3 changed files with 64 additions and 6 deletions

View File

@@ -2,3 +2,4 @@
// SPDX-License-Identifier: MIT
export * from "./store";
export * from "./settings-store";

View 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();

View File

@@ -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,