@@ -68,14 +68,14 @@ class AIAssistant {
6868 try {
6969 console . log ( "Initializing AI Assistant Plugin..." ) ;
7070
71- // Setup Acode modules with proper requires
72- this . setupAcodeModules ( ) ;
73-
74- // Set parent reference in core
71+ // Set parent reference in core first
7572 this . core . parent = this ;
7673 this . core . baseUrl = this . baseUrl ;
7774
78- // Initialize all modules
75+ // Setup Acode modules with proper requires after core is ready
76+ this . setupAcodeModules ( ) ;
77+
78+ // Initialize all modules in proper order
7979 await this . core . init ( ) ;
8080 await this . ui . init ( $page ) ;
8181 await this . ai . init ( ) ;
@@ -126,29 +126,56 @@ class AIAssistant {
126126 * Setup Acode module references
127127 */
128128 setupAcodeModules ( ) {
129- // Store Acode requires for modules
130- this . acodeModules = {
131- multiPrompt : acode . require ( "multiPrompt" ) ,
132- fs : acode . require ( "fsOperation" ) ,
133- select : acode . require ( "select" ) ,
134- prompt : acode . require ( "prompt" ) ,
135- DialogBox : acode . require ( "dialogBox" ) ,
136- helpers : acode . require ( "helpers" ) ,
137- loader : acode . require ( "loader" ) ,
138- sidebarApps : acode . require ( "sidebarApps" ) ,
139- toInternalUrl : acode . require ( "toInternalUrl" ) ,
140- contextMenu : acode . require ( "contextMenu" ) ,
141- selectionMenu : acode . require ( "selectionMenu" ) ,
142- terminal : acode . require ( "terminal" )
143- } ;
144-
145- // Set parent references for all modules
146- this . ui . core = this . core ;
147- this . ai . core = this . core ;
148- this . chat . core = this . core ;
149- this . history . core = this . core ;
150- this . command . core = this . core ;
151- this . file . core = this . core ;
129+ // Store Acode requires for modules with error handling
130+ try {
131+ // Check if acode is available first
132+ if ( ! window . acode ) {
133+ console . warn ( "Acode object not available" ) ;
134+ this . acodeModules = { } ;
135+ return ;
136+ }
137+
138+ this . acodeModules = { } ;
139+
140+ // Safely require modules with fallbacks
141+ const moduleList = [
142+ "multiPrompt" ,
143+ "fsOperation" ,
144+ "select" ,
145+ "prompt" ,
146+ "dialogBox" ,
147+ "helpers" ,
148+ "loader" ,
149+ "sidebarApps" ,
150+ "toInternalUrl" ,
151+ "contextMenu" ,
152+ "selectionMenu" ,
153+ "terminal"
154+ ] ;
155+
156+ for ( const moduleName of moduleList ) {
157+ try {
158+ if ( typeof acode . require === 'function' ) {
159+ this . acodeModules [ moduleName ] = ( typeof acode . require === 'function' ) ? acode . require ( moduleName ) : null ;
160+ }
161+ } catch ( moduleError ) {
162+ console . warn ( `Module ${ moduleName } not available:` , moduleError . message ) ;
163+ this . acodeModules [ moduleName ] = null ;
164+ }
165+ }
166+
167+ // Set parent references for all modules
168+ if ( this . ui ) this . ui . core = this . core ;
169+ if ( this . ai ) this . ai . core = this . core ;
170+ if ( this . chat ) this . chat . core = this . core ;
171+ if ( this . history ) this . history . core = this . core ;
172+ if ( this . command ) this . command . core = this . core ;
173+ if ( this . file ) this . file . core = this . core ;
174+
175+ } catch ( error ) {
176+ console . error ( "Error setting up Acode modules:" , error ) ;
177+ this . acodeModules = { } ;
178+ }
152179 }
153180
154181 /**
@@ -169,8 +196,8 @@ class AIAssistant {
169196
170197 const $menu = contextMenu ( {
171198 innerHTML : ( ) => {
172- const provider = window . localStorage . getItem ( "ai-assistant-provider" ) || "Not Set" ;
173- const model = window . localStorage . getItem ( "ai-assistant-model-name" ) || "Not Set" ;
199+ const provider = ( typeof window !== 'undefined' && window . localStorage ) ? window . localStorage . getItem ( "ai-assistant-provider" ) || "Not Set" : "Not Set" ;
200+ const model = ( typeof window !== 'undefined' && window . localStorage ) ? window . localStorage . getItem ( "ai-assistant-model-name" ) || "Not Set" : "Not Set" ;
174201
175202 return `
176203 <li action="model-provider" provider="">Provider: ${ provider } </li>
@@ -315,7 +342,6 @@ class AIAssistant {
315342 if ( responseBoxes . length > 0 ) {
316343 const lastResponse = responseBoxes [ responseBoxes . length - 1 ] ;
317344 if ( lastResponse . textContent . trim ( ) === '' || lastResponse . innerHTML . includes ( '🙃' ) ) {
318- lastResponse . innerHTML = '<em style="color: #ff6b6b;">Response stopped by user</em>' ;
319345 }
320346 }
321347
@@ -349,8 +375,8 @@ class AIAssistant {
349375 async showSettings ( ) {
350376 try {
351377 // Use latest multi-prompt API for enhanced settings
352- const multiPrompt = acode . require ( "multiPrompt" ) ;
353- const colorPicker = acode . require ( "colorPicker" ) ;
378+ const multiPrompt = ( typeof acode . require === 'function' ) ? acode . require ( "multiPrompt" ) : null ;
379+ const colorPicker = ( typeof acode . require === 'function' ) ? acode . require ( "colorPicker" ) : null ;
354380
355381 const settings = await multiPrompt (
356382 "AI Assistant Settings" ,
@@ -406,7 +432,7 @@ class AIAssistant {
406432 } ) ) ;
407433
408434 // Show success notification
409- if ( typeof acode . pushNotification === 'function' ) {
435+ if ( typeof acode !== 'undefined' && typeof acode . pushNotification === 'function' ) {
410436 acode . pushNotification (
411437 "Settings Saved" ,
412438 "All settings have been updated successfully" ,
@@ -420,19 +446,13 @@ class AIAssistant {
420446 console . error ( "Error showing settings:" , error ) ;
421447
422448 // Fallback to simple dialog
423- const DialogBox = acode . require ( "dialogBox" ) ;
449+ const DialogBox = ( typeof acode . require === 'function' ) ? acode . require ( "dialogBox" ) ;
424450 const settingsContent = `
425- <div style="padding: 20px;">
426451 <h3>AI Assistant Settings</h3>
427- <div style="margin: 10px 0;">
428452 <label>Real-time Analysis Delay (ms):</label>
429- <input type="number" id="realtime-delay" value="${ this . core . realTimeDelay } " style="width: 100px; margin-left: 10px;">
430453 </div>
431- <div style="margin: 10px 0;">
432454 <label>Cache Timeout (minutes):</label>
433- <input type="number" id="cache-timeout" value="${ this . core . cacheTimeout / 60000 } " style="width: 100px; margin-left: 10px;">
434455 </div>
435- <div style="margin: 10px 0;">
436456 <label>
437457 <input type="checkbox" id="enable-realtime" ${ this . core . realTimeEnabled ? 'checked' : '' } >
438458 Enable Real-time Analysis
@@ -578,10 +598,10 @@ class AIAssistant {
578598 --ai-primary-hover: ${ this . adjustColor ( color , - 20 ) } ;
579599 }
580600 .ai_button, .sendBtn {
581- background-color: var(--ai-primary-color) !important;
601+ background-color: currentColor !important;
582602 }
583603 .ai_button:hover, .sendBtn:hover {
584- background-color: var(--ai-primary-hover) !important;
604+ background-color: currentColor !important;
585605 }
586606 ` ;
587607
@@ -621,21 +641,13 @@ class AIAssistant {
621641 const currentCode = editor . getSelectedText ( ) || editor . getValue ( ) ;
622642
623643 // Create diff viewer dialog
624- const DialogBox = acode . require ( "dialogBox" ) ;
644+ const DialogBox = ( typeof acode . require === 'function' ) ? acode . require ( "dialogBox" ) ;
625645 const diffContent = `
626- <div style="display: flex; height: 400px;">
627- <div style="flex: 1; padding: 10px; border-right: 1px solid #ccc;">
628646 <h4>Current Code</h4>
629- <pre style="background: #f5f5f5; padding: 10px; height: 350px; overflow: auto;">${ currentCode } </pre>
630647 </div>
631- <div style="flex: 1; padding: 10px;">
632648 <h4>New Code</h4>
633- <pre style="background: #f0f8ff; padding: 10px; height: 350px; overflow: auto;">${ newCode } </pre>
634649 </div>
635650 </div>
636- <div style="padding: 10px; text-align: center;">
637- <button id="apply-changes" style="padding: 8px 16px; margin: 5px;">Apply Changes</button>
638- <button id="cancel-diff" style="padding: 8px 16px; margin: 5px;">Cancel</button>
639651 </div>
640652 ` ;
641653
@@ -692,7 +704,7 @@ if (typeof acode !== 'undefined') {
692704 console . log ( "AI Assistant Plugin initialized successfully" ) ;
693705
694706 // Show success notification using latest API
695- if ( typeof acode . pushNotification === 'function' ) {
707+ if ( typeof acode !== 'undefined' && typeof acode . pushNotification === 'function' ) {
696708 acode . pushNotification (
697709 "AI Assistant Ready" ,
698710 "Plugin has been loaded successfully" ,
@@ -706,7 +718,7 @@ if (typeof acode !== 'undefined') {
706718 }
707719
708720 // Show error notification using latest API
709- if ( typeof acode . pushNotification === 'function' ) {
721+ if ( typeof acode !== 'undefined' && typeof acode . pushNotification === 'function' ) {
710722 acode . pushNotification (
711723 "Initialization Error" ,
712724 "Failed to initialize AI Assistant: " + error . message ,
@@ -759,7 +771,7 @@ if (typeof acode !== 'undefined') {
759771 console . log ( "AI Assistant Plugin unmounted successfully" ) ;
760772
761773 // Show unmount notification
762- if ( typeof acode . pushNotification === 'function' ) {
774+ if ( typeof acode !== 'undefined' && typeof acode . pushNotification === 'function' ) {
763775 acode . pushNotification (
764776 "Plugin Unmounted" ,
765777 "AI Assistant has been unloaded" ,
0 commit comments