Files
wehub-resource-sync 555e282cc4
pi-agent-plugin checks / lint (push) Has been cancelled
pi-agent-plugin checks / test (20) (push) Has been cancelled
pi-agent-plugin checks / test (22) (push) Has been cancelled
pi-agent-plugin checks / build (push) Has been cancelled
TypeScript SDK CI / check_changes (push) Has been cancelled
TypeScript SDK CI / changelog_check (push) Has been cancelled
ci / changelog_check (push) Has been cancelled
ci / check_changes (push) Has been cancelled
ci / build_mem0 (3.10) (push) Has been cancelled
ci / build_mem0 (3.11) (push) Has been cancelled
ci / build_mem0 (3.12) (push) Has been cancelled
CLI Node CI / lint (push) Has been cancelled
CLI Node CI / test (20) (push) Has been cancelled
CLI Node CI / test (22) (push) Has been cancelled
CLI Node CI / build (push) Has been cancelled
CLI Python CI / lint (push) Has been cancelled
CLI Python CI / test (3.10) (push) Has been cancelled
CLI Python CI / test (3.11) (push) Has been cancelled
CLI Python CI / test (3.12) (push) Has been cancelled
CLI Python CI / build (push) Has been cancelled
openclaw checks / lint (push) Has been cancelled
openclaw checks / test (20) (push) Has been cancelled
openclaw checks / test (22) (push) Has been cancelled
openclaw checks / build (push) Has been cancelled
opencode-plugin checks / build (push) Has been cancelled
TypeScript SDK CI / build_ts_sdk (20) (push) Has been cancelled
TypeScript SDK CI / build_ts_sdk (22) (push) Has been cancelled
TypeScript SDK CI / integration_ts_sdk (20) (push) Has been cancelled
TypeScript SDK CI / integration_ts_sdk (22) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:03:45 +08:00

131 lines
3.9 KiB
TypeScript

import { useState } from 'react';
import axios from 'axios';
import { useDispatch, useSelector } from 'react-redux';
import { AppDispatch, RootState } from '@/store/store';
import {
setConfigLoading,
setConfigSuccess,
setConfigError,
updateLLM,
updateEmbedder,
updateMem0Config,
updateOpenMemory,
LLMProvider,
EmbedderProvider,
Mem0Config,
OpenMemoryConfig
} from '@/store/configSlice';
interface UseConfigApiReturn {
fetchConfig: () => Promise<void>;
saveConfig: (config: { openmemory?: OpenMemoryConfig; mem0: Mem0Config }) => Promise<void>;
saveLLMConfig: (llmConfig: LLMProvider) => Promise<void>;
saveEmbedderConfig: (embedderConfig: EmbedderProvider) => Promise<void>;
resetConfig: () => Promise<void>;
isLoading: boolean;
error: string | null;
}
export const useConfig = (): UseConfigApiReturn => {
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
const dispatch = useDispatch<AppDispatch>();
const URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8765";
const fetchConfig = async () => {
setIsLoading(true);
dispatch(setConfigLoading());
try {
const response = await axios.get(`${URL}/api/v1/config`);
dispatch(setConfigSuccess(response.data));
setIsLoading(false);
} catch (err: any) {
const errorMessage = err.response?.data?.detail || err.message || 'Failed to fetch configuration';
dispatch(setConfigError(errorMessage));
setError(errorMessage);
setIsLoading(false);
throw new Error(errorMessage);
}
};
const saveConfig = async (config: { openmemory?: OpenMemoryConfig; mem0: Mem0Config }) => {
setIsLoading(true);
setError(null);
try {
const response = await axios.put(`${URL}/api/v1/config`, config);
dispatch(setConfigSuccess(response.data));
setIsLoading(false);
return response.data;
} catch (err: any) {
const errorMessage = err.response?.data?.detail || err.message || 'Failed to save configuration';
dispatch(setConfigError(errorMessage));
setError(errorMessage);
setIsLoading(false);
throw new Error(errorMessage);
}
};
const resetConfig = async () => {
setIsLoading(true);
setError(null);
try {
const response = await axios.post(`${URL}/api/v1/config/reset`);
dispatch(setConfigSuccess(response.data));
setIsLoading(false);
return response.data;
} catch (err: any) {
const errorMessage = err.response?.data?.detail || err.message || 'Failed to reset configuration';
dispatch(setConfigError(errorMessage));
setError(errorMessage);
setIsLoading(false);
throw new Error(errorMessage);
}
};
const saveLLMConfig = async (llmConfig: LLMProvider) => {
setIsLoading(true);
setError(null);
try {
const response = await axios.put(`${URL}/api/v1/config/mem0/llm`, llmConfig);
dispatch(updateLLM(response.data));
setIsLoading(false);
return response.data;
} catch (err: any) {
const errorMessage = err.response?.data?.detail || err.message || 'Failed to save LLM configuration';
setError(errorMessage);
setIsLoading(false);
throw new Error(errorMessage);
}
};
const saveEmbedderConfig = async (embedderConfig: EmbedderProvider) => {
setIsLoading(true);
setError(null);
try {
const response = await axios.put(`${URL}/api/v1/config/mem0/embedder`, embedderConfig);
dispatch(updateEmbedder(response.data));
setIsLoading(false);
return response.data;
} catch (err: any) {
const errorMessage = err.response?.data?.detail || err.message || 'Failed to save Embedder configuration';
setError(errorMessage);
setIsLoading(false);
throw new Error(errorMessage);
}
};
return {
fetchConfig,
saveConfig,
saveLLMConfig,
saveEmbedderConfig,
resetConfig,
isLoading,
error
};
};