@@ -3,6 +3,7 @@ import { Settings, X } from 'lucide-react';
33import {
44 chat ,
55 loadConfig ,
6+ loadConfigSync ,
67 saveConfig ,
78 getDefaultConfig ,
89 type LLMConfig ,
@@ -11,6 +12,7 @@ import {
1112} from '@/lib/llmClient' ;
1213import {
1314 loadImageGenConfig ,
15+ loadImageGenConfigSync ,
1416 saveImageGenConfig ,
1517 getDefaultImageGenConfig ,
1618 type ImageGenConfig ,
@@ -27,6 +29,7 @@ import {
2729import { seedMetaFiles } from '@/lib/seedMeta' ;
2830import { dispatchAgentAction , onUserAction } from '@/lib/vibeContainerMock' ;
2931import { getFileToolDefinitions , isFileTool , executeFileTool } from '@/lib/fileTools' ;
32+ import { logger } from '@/lib/logger' ;
3033import {
3134 getImageGenToolDefinitions ,
3235 isImageGenTool ,
@@ -69,10 +72,22 @@ const ChatPanel: React.FC<{ onClose: () => void; visible?: boolean }> = ({
6972 const [ input , setInput ] = useState ( '' ) ;
7073 const [ loading , setLoading ] = useState ( false ) ;
7174 const [ showSettings , setShowSettings ] = useState ( false ) ;
72- const [ config , setConfig ] = useState < LLMConfig | null > ( loadConfig ) ;
73- const [ imageGenConfig , setImageGenConfig ] = useState < ImageGenConfig | null > ( loadImageGenConfig ) ;
75+ // Init from localStorage immediately (sync), then override from local file if available
76+ const [ config , setConfig ] = useState < LLMConfig | null > ( loadConfigSync ) ;
77+ const [ imageGenConfig , setImageGenConfig ] = useState < ImageGenConfig | null > (
78+ loadImageGenConfigSync ,
79+ ) ;
7480 const messagesEndRef = useRef < HTMLDivElement > ( null ) ;
7581
82+ useEffect ( ( ) => {
83+ loadConfig ( ) . then ( ( fileConfig ) => {
84+ if ( fileConfig ) setConfig ( fileConfig ) ;
85+ } ) ;
86+ loadImageGenConfig ( ) . then ( ( fileConfig ) => {
87+ if ( fileConfig ) setImageGenConfig ( fileConfig ) ;
88+ } ) ;
89+ } , [ ] ) ;
90+
7691 useEffect ( ( ) => {
7792 messagesEndRef . current ?. scrollIntoView ( { behavior : 'smooth' } ) ;
7893 } , [ messages , loading ] ) ;
@@ -111,7 +126,7 @@ const ChatPanel: React.FC<{ onClose: () => void; visible?: boolean }> = ({
111126 try {
112127 await runConversation ( newHistory , cfg ) ;
113128 } catch ( err ) {
114- console . error ( '[ ChatPanel] User action error:' , err ) ;
129+ logger . error ( 'ChatPanel' , ' User action error:', err ) ;
115130 } finally {
116131 setLoading ( false ) ;
117132 }
@@ -134,20 +149,20 @@ const ChatPanel: React.FC<{ onClose: () => void; visible?: boolean }> = ({
134149 } ;
135150 action_result ?: string ;
136151 } ;
137- console . info ( '[ ChatPanel] onUserAction received:' , evt ) ;
152+ logger . info ( 'ChatPanel' , ' onUserAction received:', evt ) ;
138153 // Ignore action_result callbacks (result callbacks triggered by Agent)
139154 if ( evt . action_result !== undefined ) {
140- console . info ( '[ ChatPanel] Ignored: action_result event' ) ;
155+ logger . info ( 'ChatPanel' , ' Ignored: action_result event') ;
141156 return ;
142157 }
143158 const action = evt . app_action ;
144159 if ( ! action ) {
145- console . info ( '[ ChatPanel] Ignored: no app_action' ) ;
160+ logger . info ( 'ChatPanel' , ' Ignored: no app_action') ;
146161 return ;
147162 }
148163 // Ignore actions triggered by Agent (trigger_by=2)
149164 if ( action . trigger_by === 2 ) {
150- console . info ( '[ ChatPanel] Ignored: Agent triggered' ) ;
165+ logger . info ( 'ChatPanel' , ' Ignored: Agent triggered') ;
151166 return ;
152167 }
153168
@@ -185,7 +200,7 @@ const ChatPanel: React.FC<{ onClose: () => void; visible?: boolean }> = ({
185200 try {
186201 await runConversation ( newHistory , config ) ;
187202 } catch ( err ) {
188- console . error ( '[ ChatPanel] Error:' , err ) ;
203+ logger . error ( 'ChatPanel' , ' Error:', err ) ;
189204 addMessage ( {
190205 id : String ( Date . now ( ) ) ,
191206 role : 'assistant' ,
@@ -197,8 +212,9 @@ const ChatPanel: React.FC<{ onClose: () => void; visible?: boolean }> = ({
197212 } , [ input , loading , config , chatHistory , addMessage ] ) ;
198213
199214 const runConversation = async ( history : ChatMessage [ ] , cfg : LLMConfig ) => {
200- console . info (
201- '[ChatPanel] runConversation called, history length:' ,
215+ logger . info (
216+ 'ChatPanel' ,
217+ 'runConversation called, history length:' ,
202218 history . length ,
203219 'provider:' ,
204220 cfg . provider ,
@@ -212,7 +228,7 @@ const ChatPanel: React.FC<{ onClose: () => void; visible?: boolean }> = ({
212228 ...getFileToolDefinitions ( ) ,
213229 ...( hasImageGen ? getImageGenToolDefinitions ( ) : [ ] ) ,
214230 ] ;
215- console . info ( '[ ToolLog] ChatPanel: tools passed to chat(), count=' , tools . length ) ;
231+ logger . info ( 'ToolLog' , ' ChatPanel: tools passed to chat(), count=', tools . length ) ;
216232 const fullMessages : ChatMessage [ ] = [
217233 { role : 'system' , content : buildSystemPrompt ( hasImageGen ) } ,
218234 ...history ,
@@ -222,21 +238,22 @@ const ChatPanel: React.FC<{ onClose: () => void; visible?: boolean }> = ({
222238 let iterations = 0 ;
223239 const maxIterations = 10 ;
224240
225- console . info ( '[ChatDebug] === START conversation ===' ) ;
226- console . info ( '[ChatDebug] messages sent to LLM:' , JSON . stringify ( currentMessages , null , 2 ) ) ;
227- console . info (
228- '[ChatDebug] tools:' ,
241+ logger . info ( 'ChatDebug' , '=== START conversation ===' ) ;
242+ logger . info ( 'ChatDebug' , 'messages sent to LLM:' , JSON . stringify ( currentMessages , null , 2 ) ) ;
243+ logger . info (
244+ 'ChatDebug' ,
245+ 'tools:' ,
229246 tools . map ( ( t ) => ( t as { function : { name : string } } ) . function . name ) ,
230247 ) ;
231248
232249 while ( iterations < maxIterations ) {
233250 iterations ++ ;
234- console . info ( `[ ChatDebug] --- iteration ${ iterations } ---`) ;
235- console . info ( '[ ChatDebug] messages count:' , currentMessages . length ) ;
251+ logger . info ( ' ChatDebug' , ` --- iteration ${ iterations } ---`) ;
252+ logger . info ( 'ChatDebug' , ' messages count:', currentMessages . length ) ;
236253 const response = await chat ( currentMessages , tools , cfg ) ;
237254
238- console . info ( '[ ChatDebug] LLM response content:' , response . content ) ;
239- console . info ( '[ ChatDebug] LLM toolCalls:' , JSON . stringify ( response . toolCalls , null , 2 ) ) ;
255+ logger . info ( 'ChatDebug' , ' LLM response content:', response . content ) ;
256+ logger . info ( 'ChatDebug' , ' LLM toolCalls:', JSON . stringify ( response . toolCalls , null , 2 ) ) ;
240257
241258 if ( response . toolCalls . length === 0 ) {
242259 // No tool calls, just text response
@@ -268,14 +285,15 @@ const ChatPanel: React.FC<{ onClose: () => void; visible?: boolean }> = ({
268285 }
269286
270287 // Execute each tool call
271- console . info (
272- '[ToolLog] ChatPanel: executing toolCalls count=' ,
288+ logger . info (
289+ 'ToolLog' ,
290+ 'ChatPanel: executing toolCalls count=' ,
273291 response . toolCalls . length ,
274292 'names=' ,
275293 response . toolCalls . map ( ( tc ) => tc . function . name ) ,
276294 ) ;
277295 for ( const tc of response . toolCalls ) {
278- console . info ( '[ ToolLog] ChatPanel: processing tool name=' , tc . function . name ) ;
296+ logger . info ( 'ToolLog' , ' ChatPanel: processing tool name=', tc . function . name ) ;
279297 let params : Record < string , string > = { } ;
280298 try {
281299 params = JSON . parse ( tc . function . arguments ) ;
@@ -286,7 +304,7 @@ const ChatPanel: React.FC<{ onClose: () => void; visible?: boolean }> = ({
286304 // list_apps tool
287305 if ( tc . function . name === 'list_apps' ) {
288306 const result = executeListApps ( ) ;
289- console . info ( '[ ChatDebug] list_apps result:' , result ) ;
307+ logger . info ( 'ChatDebug' , ' list_apps result:', result ) ;
290308 currentMessages = [
291309 ...currentMessages ,
292310 { role : 'tool' , content : result , tool_call_id : tc . id } ,
@@ -303,8 +321,9 @@ const ChatPanel: React.FC<{ onClose: () => void; visible?: boolean }> = ({
303321 } ) ;
304322 try {
305323 const result = await executeFileTool ( tc . function . name , params ) ;
306- console . info (
307- `[ChatDebug] ${ tc . function . name } (${ JSON . stringify ( params ) } ) result:` ,
324+ logger . info (
325+ 'ChatDebug' ,
326+ `${ tc . function . name } (${ JSON . stringify ( params ) } ) result:` ,
308327 result . slice ( 0 , 500 ) ,
309328 ) ;
310329 currentMessages = [
@@ -503,8 +522,9 @@ const ChatPanel: React.FC<{ onClose: () => void; visible?: boolean }> = ({
503522 imageGenConfig = { imageGenConfig }
504523 onSave = { ( c , igc ) => {
505524 setConfig ( c ) ;
506- saveConfig ( c ) ;
507525 setImageGenConfig ( igc ) ;
526+ // Persist both configs atomically to ~/.openroom/config.json
527+ saveConfig ( c , igc ) ;
508528 if ( igc ) {
509529 saveImageGenConfig ( igc ) ;
510530 }
0 commit comments