Files
CC-Switch/src/hooks/useOpenClaw.ts
T
Jason 7e6f803035 feat: overhaul OpenClaw config panels with JSON5 round-trip write engine
- Add json-five crate for JSON5 serialization preserving comments and formatting
- Rewrite openclaw_config.rs with comment-preserving JSON5 read/write engine
- Add Tauri commands: get_openclaw_live_provider, write_openclaw_config_section
- Redesign EnvPanel as full JSON editor with structured error handling
- Add tools.profile selection (minimal/coding/messaging/full) to ToolsPanel
- Add legacy timeout migration support to AgentsDefaultsPanel
- Add OpenClawHealthBanner component for config validation warnings
- Add supporting hooks, mutations, utility functions, and unit tests
2026-03-08 19:42:18 +08:00

145 lines
4.1 KiB
TypeScript

import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { openclawApi } from "@/lib/api/openclaw";
import { providersApi } from "@/lib/api/providers";
import type {
OpenClawEnvConfig,
OpenClawToolsConfig,
OpenClawAgentsDefaults,
} from "@/types";
/**
* Centralized query keys for all OpenClaw-related queries.
* Import this from any file that needs to invalidate OpenClaw caches.
*/
export const openclawKeys = {
all: ["openclaw"] as const,
liveProviderIds: ["openclaw", "liveProviderIds"] as const,
defaultModel: ["openclaw", "defaultModel"] as const,
env: ["openclaw", "env"] as const,
tools: ["openclaw", "tools"] as const,
agentsDefaults: ["openclaw", "agentsDefaults"] as const,
health: ["openclaw", "health"] as const,
};
// ============================================================
// Query hooks
// ============================================================
/**
* Query live provider IDs from openclaw.json config.
* Used by ProviderList to show "In Config" badge.
*/
export function useOpenClawLiveProviderIds(enabled: boolean) {
return useQuery({
queryKey: openclawKeys.liveProviderIds,
queryFn: () => providersApi.getOpenClawLiveProviderIds(),
enabled,
});
}
/**
* Query the default model from agents.defaults.model.
* Used by ProviderList to show which provider is the default.
*/
export function useOpenClawDefaultModel(enabled: boolean) {
return useQuery({
queryKey: openclawKeys.defaultModel,
queryFn: () => openclawApi.getDefaultModel(),
enabled,
});
}
/**
* Query env section of openclaw.json.
*/
export function useOpenClawEnv() {
return useQuery({
queryKey: openclawKeys.env,
queryFn: () => openclawApi.getEnv(),
staleTime: 30_000,
});
}
/**
* Query tools section of openclaw.json.
*/
export function useOpenClawTools() {
return useQuery({
queryKey: openclawKeys.tools,
queryFn: () => openclawApi.getTools(),
staleTime: 30_000,
});
}
/**
* Query agents.defaults section of openclaw.json.
*/
export function useOpenClawAgentsDefaults() {
return useQuery({
queryKey: openclawKeys.agentsDefaults,
queryFn: () => openclawApi.getAgentsDefaults(),
staleTime: 30_000,
});
}
export function useOpenClawHealth(enabled: boolean) {
return useQuery({
queryKey: openclawKeys.health,
queryFn: () => openclawApi.scanHealth(),
staleTime: 30_000,
enabled,
});
}
// ============================================================
// Mutation hooks
// ============================================================
/**
* Save env config. Invalidates env query on success.
* Toast notifications are handled by the component.
*/
export function useSaveOpenClawEnv() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (env: OpenClawEnvConfig) => openclawApi.setEnv(env),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: openclawKeys.env });
queryClient.invalidateQueries({ queryKey: openclawKeys.health });
},
});
}
/**
* Save tools config. Invalidates tools query on success.
* Toast notifications are handled by the component.
*/
export function useSaveOpenClawTools() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (tools: OpenClawToolsConfig) => openclawApi.setTools(tools),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: openclawKeys.tools });
queryClient.invalidateQueries({ queryKey: openclawKeys.health });
},
});
}
/**
* Save agents.defaults config. Invalidates both agentsDefaults and defaultModel
* queries on success (since changing agents.defaults may affect the default model).
* Toast notifications are handled by the component.
*/
export function useSaveOpenClawAgentsDefaults() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (defaults: OpenClawAgentsDefaults) =>
openclawApi.setAgentsDefaults(defaults),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: openclawKeys.agentsDefaults });
queryClient.invalidateQueries({ queryKey: openclawKeys.defaultModel });
queryClient.invalidateQueries({ queryKey: openclawKeys.health });
},
});
}