File size: 2,193 Bytes
5fc68b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { ConfigManager } from "@/lib/config/manager";
import { toast } from "sonner";

export const configKeys = {
  all: ['config'] as const,
  details: () => [...configKeys.all, 'details'] as const,
} as const;

export function useConfig() {
  const configManager = ConfigManager.getInstance();
  
  return useQuery({
    queryKey: configKeys.details(),
    queryFn: async () => await configManager.getConfig(),
  });
}

export function useUpdateConfig() {
  const queryClient = useQueryClient();
  const configManager = ConfigManager.getInstance();

  return useMutation({
    mutationFn: async (updates: Record<string, string>) => {
      return await configManager.updateConfig(updates);
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: configKeys.all });
      toast.success('Configuration updated successfully');
    },
    onError: (error) => {
      toast.error('Failed to update configuration');
      console.error('Config update error:', error);
    }
  });
}

export function useUpdateEnabledModels(type: 'chat' | 'embedding') {
  const queryClient = useQueryClient();
  const configManager = ConfigManager.getInstance();
  const configKey = type === 'chat' ? 'enabled_chat_models' : 'enabled_embedding_models';

  return useMutation({
    mutationFn: async ({ modelId, enabled }: { modelId: string, enabled: boolean }) => {
      const currentConfig = await configManager.getConfig();
      const enabledModels = new Set(currentConfig[configKey] || []);
      
      if (enabled) {
        enabledModels.add(modelId);
      } else {
        enabledModels.delete(modelId);
      }

      return await configManager.updateConfig({
        [configKey]: Array.from(enabledModels)
      });
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: configKeys.all });
      toast.success(`${type === 'chat' ? 'Chat' : 'Embedding'} models updated successfully`);
    },
    onError: (error) => {
      toast.error(`Failed to update ${type === 'chat' ? 'chat' : 'embedding'} models`);
      console.error(`${type} models update error:`, error);
    }
  });
}