File size: 1,734 Bytes
6e38ce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useEffect } from "react";
import { ModelConfig } from "../tabs/agentSettings/modelSelector/modelConfigForms/types";
import { initializeDefaultModel } from "../utils/modelUtils";
import { useSettingsStore } from "../../store";
import { settingsAPI } from "../../views/api";

/**
 * Hook to get the default model, prioritizing config file over UI settings
 *
 * When --config is provided, it uses the default model from the config file.
 * Otherwise, it falls back to UI settings.
 */
export const useDefaultModel = () => {
  const { config: uiSettings } = useSettingsStore();
  const [defaultModel, setDefaultModel] = useState<ModelConfig | undefined>(
    initializeDefaultModel(uiSettings)
  );

  useEffect(() => {
    const fetchConfigInfo = async () => {
      try {
        const configFileInfo = await settingsAPI.getConfigInfo();

        // Check if config file exists and has content
        if (configFileInfo?.has_config_file && configFileInfo?.config_content) {
          const configFileData = configFileInfo.config_content;
          const modelFromConfigFile = initializeDefaultModel(configFileData);
          setDefaultModel(modelFromConfigFile);
          return;
        }

        // Fall back to UI settings if no config file
        const modelFromUISettings = initializeDefaultModel(uiSettings);
        setDefaultModel(modelFromUISettings);
      } catch (error) {
        console.warn("Failed to fetch config file info:", error);
        // Fall back to UI settings on error
        const modelFromUISettings = initializeDefaultModel(uiSettings);
        setDefaultModel(modelFromUISettings);
      }
    };

    fetchConfigInfo();
  }, []);

  return { defaultModel, setDefaultModel };
};