|
4 | 4 |
|
5 | 5 | namespace FlowVision.lib.Classes |
6 | 6 | { |
7 | | - /// <summary> |
8 | | - /// Configuration for LM Studio local AI integration |
9 | | - /// </summary> |
10 | 7 | public class LMStudioConfig |
11 | 8 | { |
12 | | - /// <summary> |
13 | | - /// LM Studio server endpoint (default: http://localhost:1234/v1) |
14 | | - /// </summary> |
15 | 9 | public string EndpointURL { get; set; } = "http://localhost:1234/v1"; |
16 | | - |
17 | | - /// <summary> |
18 | | - /// Model name (e.g., "gpt-3.5-turbo", "local-model", or whatever LM Studio shows) |
19 | | - /// Can be left as default - LM Studio typically uses the loaded model automatically |
20 | | - /// </summary> |
21 | 10 | public string ModelName { get; set; } = "local-model"; |
22 | | - |
23 | | - /// <summary> |
24 | | - /// API key (LM Studio doesn't require one, but field kept for compatibility) |
25 | | - /// Use "lm-studio" or "not-needed" as placeholder |
26 | | - /// </summary> |
27 | | - public string APIKey { get; set; } = "lm-studio"; |
28 | | - |
29 | | - /// <summary> |
30 | | - /// Whether to use LM Studio or fall back to Azure |
31 | | - /// </summary> |
32 | | - public bool Enabled { get; set; } = false; |
33 | | - |
34 | | - /// <summary> |
35 | | - /// Temperature setting for local model |
36 | | - /// </summary> |
37 | 11 | public double Temperature { get; set; } = 0.7; |
38 | | - |
39 | | - /// <summary> |
40 | | - /// Max tokens for completion |
41 | | - /// </summary> |
42 | 12 | public int MaxTokens { get; set; } = 2048; |
43 | | - |
44 | | - /// <summary> |
45 | | - /// Timeout in seconds for LM Studio requests |
46 | | - /// </summary> |
47 | | - public int TimeoutSeconds { get; set; } = 300; |
| 13 | + public bool Enabled { get; set; } = false; |
| 14 | + public string APIKey { get; set; } = "lm-studio"; // OpenAI client requires a key even if local |
| 15 | + public int TimeoutSeconds { get; set; } = 120; |
48 | 16 |
|
49 | | - private static string ConfigFilePath() |
50 | | - { |
51 | | - return Path.Combine( |
52 | | - Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), |
53 | | - "FlowVision", |
54 | | - "lmstudioconfig.json"); |
55 | | - } |
| 17 | + // Track if the config was successfully loaded from disk |
| 18 | + [System.Text.Json.Serialization.JsonIgnore] |
| 19 | + public bool IsValid { get; set; } = true; |
| 20 | + |
| 21 | + private static string ConfigFilePath => Path.Combine( |
| 22 | + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), |
| 23 | + "FlowVision", |
| 24 | + "lmstudioconfig.json"); |
56 | 25 |
|
57 | 26 | public static LMStudioConfig LoadConfig() |
58 | 27 | { |
59 | 28 | try |
60 | 29 | { |
61 | | - Directory.CreateDirectory(Path.GetDirectoryName(ConfigFilePath())); |
| 30 | + Directory.CreateDirectory(Path.GetDirectoryName(ConfigFilePath)); |
62 | 31 |
|
63 | | - if (File.Exists(ConfigFilePath())) |
| 32 | + if (File.Exists(ConfigFilePath)) |
64 | 33 | { |
65 | | - string jsonContent = File.ReadAllText(ConfigFilePath()); |
66 | | - if (!string.IsNullOrWhiteSpace(jsonContent)) |
| 34 | + string jsonContent = File.ReadAllText(ConfigFilePath); |
| 35 | + // Basic validation for empty file |
| 36 | + if (string.IsNullOrWhiteSpace(jsonContent)) |
67 | 37 | { |
68 | | - var config = JsonSerializer.Deserialize<LMStudioConfig>(jsonContent); |
69 | | - return config ?? new LMStudioConfig(); |
| 38 | + return new LMStudioConfig { IsValid = false }; |
| 39 | + } |
| 40 | + |
| 41 | + var config = JsonSerializer.Deserialize<LMStudioConfig>(jsonContent); |
| 42 | + if (config != null) |
| 43 | + { |
| 44 | + config.IsValid = true; |
| 45 | + return config; |
70 | 46 | } |
71 | 47 | } |
72 | 48 | } |
73 | 49 | catch (Exception ex) |
74 | 50 | { |
75 | | - Console.WriteLine($"Error loading LM Studio config: {ex.Message}"); |
| 51 | + // Return a config marked as invalid so the UI can warn the user |
| 52 | + return new LMStudioConfig |
| 53 | + { |
| 54 | + Enabled = false, |
| 55 | + IsValid = false |
| 56 | + }; |
76 | 57 | } |
77 | 58 |
|
| 59 | + // Return default if file doesn't exist |
78 | 60 | return new LMStudioConfig(); |
79 | 61 | } |
80 | 62 |
|
81 | 63 | public void SaveConfig() |
82 | 64 | { |
83 | 65 | try |
84 | 66 | { |
85 | | - Directory.CreateDirectory(Path.GetDirectoryName(ConfigFilePath())); |
| 67 | + Directory.CreateDirectory(Path.GetDirectoryName(ConfigFilePath)); |
86 | 68 |
|
87 | 69 | var options = new JsonSerializerOptions { WriteIndented = true }; |
88 | 70 | string jsonContent = JsonSerializer.Serialize(this, options); |
89 | | - File.WriteAllText(ConfigFilePath(), jsonContent); |
| 71 | + File.WriteAllText(ConfigFilePath, jsonContent); |
90 | 72 | } |
91 | 73 | catch (Exception ex) |
92 | 74 | { |
93 | 75 | Console.WriteLine($"Error saving LM Studio config: {ex.Message}"); |
| 76 | + throw; // Re-throw so the UI can show the error |
94 | 77 | } |
95 | 78 | } |
96 | 79 | } |
|
0 commit comments