|
1 | 1 | import { createContext, useContext, useState, useEffect } from 'react';
|
2 | 2 | import type { ReactNode, Dispatch, SetStateAction } from 'react';
|
3 | 3 | import { api } from '@/lib/api';
|
4 |
| -import type { Config, StatusLineConfig } from '@/types'; |
| 4 | +import type { Config } from '@/types'; |
5 | 5 |
|
6 | 6 | interface ConfigContextType {
|
7 | 7 | config: Config | null;
|
@@ -66,44 +66,52 @@ export function ConfigProvider({ children }: ConfigProviderProps) {
|
66 | 66 | // Try to fetch config regardless of API key presence
|
67 | 67 | const data = await api.getConfig();
|
68 | 68 |
|
69 |
| - // Validate the received data to ensure it has the expected structure |
70 |
| - const validConfig = { |
71 |
| - LOG: typeof data.LOG === 'boolean' ? data.LOG : false, |
72 |
| - LOG_LEVEL: typeof data.LOG_LEVEL === 'string' ? data.LOG_LEVEL : 'debug', |
73 |
| - CLAUDE_PATH: typeof data.CLAUDE_PATH === 'string' ? data.CLAUDE_PATH : '', |
74 |
| - HOST: typeof data.HOST === 'string' ? data.HOST : '127.0.0.1', |
75 |
| - PORT: typeof data.PORT === 'number' ? data.PORT : 3456, |
76 |
| - APIKEY: typeof data.APIKEY === 'string' ? data.APIKEY : '', |
77 |
| - API_TIMEOUT_MS: typeof data.API_TIMEOUT_MS === 'string' ? data.API_TIMEOUT_MS : '600000', |
78 |
| - PROXY_URL: typeof data.PROXY_URL === 'string' ? data.PROXY_URL : '', |
79 |
| - transformers: Array.isArray(data.transformers) ? data.transformers : [], |
80 |
| - Providers: Array.isArray(data.Providers) ? data.Providers : [], |
81 |
| - StatusLine: data.StatusLine && typeof data.StatusLine === 'object' ? { |
82 |
| - enabled: typeof data.StatusLine.enabled === 'boolean' ? data.StatusLine.enabled : false, |
83 |
| - currentStyle: typeof data.StatusLine.currentStyle === 'string' ? data.StatusLine.currentStyle : 'default', |
84 |
| - default: data.StatusLine.default && typeof data.StatusLine.default === 'object' && Array.isArray(data.StatusLine.default.modules) ? data.StatusLine.default : { modules: [] }, |
85 |
| - powerline: data.StatusLine.powerline && typeof data.StatusLine.powerline === 'object' && Array.isArray(data.StatusLine.powerline.modules) ? data.StatusLine.powerline : { modules: [] } |
86 |
| - } : { |
87 |
| - enabled: false, |
88 |
| - currentStyle: 'default', |
89 |
| - default: { modules: [] }, |
90 |
| - powerline: { modules: [] } |
91 |
| - }, |
92 |
| - Router: data.Router && typeof data.Router === 'object' ? { |
93 |
| - default: typeof data.Router.default === 'string' ? data.Router.default : '', |
94 |
| - background: typeof data.Router.background === 'string' ? data.Router.background : '', |
95 |
| - think: typeof data.Router.think === 'string' ? data.Router.think : '', |
96 |
| - longContext: typeof data.Router.longContext === 'string' ? data.Router.longContext : '', |
97 |
| - longContextThreshold: typeof data.Router.longContextThreshold === 'number' ? data.Router.longContextThreshold : 60000, |
98 |
| - webSearch: typeof data.Router.webSearch === 'string' ? data.Router.webSearch : '' |
99 |
| - } : { |
100 |
| - default: '', |
101 |
| - background: '', |
102 |
| - think: '', |
103 |
| - longContext: '', |
104 |
| - longContextThreshold: 60000, |
105 |
| - webSearch: '' |
106 |
| - } |
| 69 | + // Start with the original data to preserve all fields |
| 70 | + const validConfig = { ...data }; |
| 71 | + |
| 72 | + // Validate and set defaults for known fields |
| 73 | + validConfig.LOG = typeof data.LOG === 'boolean' ? data.LOG : false; |
| 74 | + validConfig.LOG_LEVEL = typeof data.LOG_LEVEL === 'string' ? data.LOG_LEVEL : 'debug'; |
| 75 | + validConfig.CLAUDE_PATH = typeof data.CLAUDE_PATH === 'string' ? data.CLAUDE_PATH : ''; |
| 76 | + validConfig.HOST = typeof data.HOST === 'string' ? data.HOST : '127.0.0.1'; |
| 77 | + validConfig.PORT = typeof data.PORT === 'number' ? data.PORT : 3456; |
| 78 | + validConfig.APIKEY = typeof data.APIKEY === 'string' ? data.APIKEY : ''; |
| 79 | + validConfig.API_TIMEOUT_MS = typeof data.API_TIMEOUT_MS === 'string' ? data.API_TIMEOUT_MS : '600000'; |
| 80 | + validConfig.PROXY_URL = typeof data.PROXY_URL === 'string' ? data.PROXY_URL : ''; |
| 81 | + validConfig.transformers = Array.isArray(data.transformers) ? data.transformers : []; |
| 82 | + validConfig.Providers = Array.isArray(data.Providers) ? data.Providers : []; |
| 83 | + |
| 84 | + // Preserve CUSTOM_ROUTER_PATH if it exists |
| 85 | + if (typeof data.CUSTOM_ROUTER_PATH === 'string') { |
| 86 | + validConfig.CUSTOM_ROUTER_PATH = data.CUSTOM_ROUTER_PATH; |
| 87 | + } |
| 88 | + |
| 89 | + // Preserve NON_INTERACTIVE_MODE if it exists |
| 90 | + if (typeof data.NON_INTERACTIVE_MODE === 'boolean') { |
| 91 | + validConfig.NON_INTERACTIVE_MODE = data.NON_INTERACTIVE_MODE; |
| 92 | + } |
| 93 | + |
| 94 | + validConfig.StatusLine = data.StatusLine && typeof data.StatusLine === 'object' ? { |
| 95 | + enabled: typeof data.StatusLine.enabled === 'boolean' ? data.StatusLine.enabled : false, |
| 96 | + currentStyle: typeof data.StatusLine.currentStyle === 'string' ? data.StatusLine.currentStyle : 'default', |
| 97 | + default: data.StatusLine.default && typeof data.StatusLine.default === 'object' && Array.isArray(data.StatusLine.default.modules) ? data.StatusLine.default : { modules: [] }, |
| 98 | + powerline: data.StatusLine.powerline && typeof data.StatusLine.powerline === 'object' && Array.isArray(data.StatusLine.powerline.modules) ? data.StatusLine.powerline : { modules: [] } |
| 99 | + } : undefined; |
| 100 | + |
| 101 | + validConfig.Router = data.Router && typeof data.Router === 'object' ? { |
| 102 | + default: typeof data.Router.default === 'string' ? data.Router.default : '', |
| 103 | + background: typeof data.Router.background === 'string' ? data.Router.background : '', |
| 104 | + think: typeof data.Router.think === 'string' ? data.Router.think : '', |
| 105 | + longContext: typeof data.Router.longContext === 'string' ? data.Router.longContext : '', |
| 106 | + longContextThreshold: typeof data.Router.longContextThreshold === 'number' ? data.Router.longContextThreshold : 60000, |
| 107 | + webSearch: typeof data.Router.webSearch === 'string' ? data.Router.webSearch : '' |
| 108 | + } : { |
| 109 | + default: '', |
| 110 | + background: '', |
| 111 | + think: '', |
| 112 | + longContext: '', |
| 113 | + longContextThreshold: 60000, |
| 114 | + webSearch: '' |
107 | 115 | };
|
108 | 116 |
|
109 | 117 | setConfig(validConfig);
|
|
0 commit comments