Skip to content

Commit 84f346d

Browse files
committed
working code
1 parent 1a197eb commit 84f346d

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

lib/Pages/Playground/playground.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ class PlaygroundApp {
183183
console.log('🔄 Playground received shared connection event:', event, data);
184184

185185
if (event === 'connected') {
186+
// Check if we already have the same connection to avoid duplicates
187+
if (this.aiAssistant === data && this.isConnected) {
188+
console.log('⏸️ Same connection already active, skipping duplicate setup');
189+
return;
190+
}
191+
186192
this.aiAssistant = data;
187193
this.aiAssistant.onStatusChange = (status) => this.handleAIStatusChange(status);
188194

@@ -344,6 +350,12 @@ class PlaygroundApp {
344350
console.log('🔄 AI Status Change:', status);
345351

346352
if (status.connected) {
353+
// Check if we're already connected to avoid duplicate processing
354+
if (this.isConnected && status.provider === this.getCurrentProvider()) {
355+
console.log('⏸️ Already connected to same provider, skipping duplicate status change');
356+
return;
357+
}
358+
347359
this.isConnected = true;
348360
this.updateStatus('aiStatus', `Connected (${status.provider})`, 'success');
349361
this.updateConnectButtonText();

lib/Pages/shared/ai-connection-manager.js

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ class SharedAIConnectionManager {
311311
async restoreAIAssistant() {
312312
const now = Date.now();
313313

314+
// Early exit if we already have a working connection
315+
if (this.isConnected && this.aiAssistant && this.aiAssistant.isConnected) {
316+
console.log('✅ AI connection already active, skipping restoration');
317+
return;
318+
}
319+
314320
// Check if restoration is already in progress
315321
if (this.isRestoring) {
316322
console.log('⏸️ Restoration already in progress, skipping...');
@@ -345,25 +351,25 @@ class SharedAIConnectionManager {
345351
// Attempt to restore connection based on provider
346352
let restored = false;
347353

348-
if (this.currentProvider === 'ollama') {
349-
const baseURL = this.connectionSettings.baseURL || 'http://localhost:11434';
350-
await this.connectOllama({ baseURL });
351-
restored = this.aiAssistant && this.aiAssistant.isConnected;
352-
} else if (this.currentProvider === 'lmstudio') {
353-
const baseURL = this.connectionSettings.baseURL || 'http://localhost:1234';
354-
await this.connectLMStudio({ baseURL });
355-
restored = this.aiAssistant && this.aiAssistant.isConnected;
356-
} else if (this.currentProvider === 'openai') {
357-
if (this.connectionSettings.apiKey) {
358-
await this.connectOpenAI({ apiKey: this.connectionSettings.apiKey });
359-
restored = this.aiAssistant && this.aiAssistant.isConnected;
360-
} else {
361-
console.log('❌ Cannot restore OpenAI - no API key saved');
362-
}
363-
} else if (this.currentProvider === 'local') {
364-
await this.connectLocal();
365-
restored = this.aiAssistant && this.aiAssistant.isConnected;
366-
}
354+
if (this.currentProvider === 'ollama') {
355+
const baseURL = this.connectionSettings.baseURL || 'http://localhost:11434';
356+
await this.connectOllama({ baseURL });
357+
restored = this.aiAssistant && this.aiAssistant.isConnected;
358+
} else if (this.currentProvider === 'lmstudio') {
359+
const baseURL = this.connectionSettings.baseURL || 'http://localhost:1234';
360+
await this.connectLMStudio({ baseURL });
361+
restored = this.aiAssistant && this.aiAssistant.isConnected;
362+
} else if (this.currentProvider === 'openai') {
363+
if (this.connectionSettings.apiKey) {
364+
await this.connectOpenAI({ apiKey: this.connectionSettings.apiKey });
365+
restored = this.aiAssistant && this.aiAssistant.isConnected;
366+
} else {
367+
console.log('❌ Cannot restore OpenAI - no API key saved');
368+
}
369+
} else if (this.currentProvider === 'local') {
370+
await this.connectLocal();
371+
restored = this.aiAssistant && this.aiAssistant.isConnected;
372+
}
367373

368374
if (restored) {
369375
console.log('✅ AI connection fully restored from saved state');
@@ -414,6 +420,13 @@ class SharedAIConnectionManager {
414420

415421
this.lastRefreshTime = now;
416422

423+
// If we already have a working connection, just notify listeners
424+
if (this.isConnected && this.aiAssistant && this.aiAssistant.isConnected) {
425+
console.log('✅ Active connection found, notifying listeners only');
426+
this.notifyListeners('connected', this.aiAssistant);
427+
return;
428+
}
429+
417430
// Reload connection state from localStorage
418431
this.loadConnectionState();
419432

@@ -434,11 +447,17 @@ class SharedAIConnectionManager {
434447
async checkAndRestoreConnection() {
435448
console.log('🔍 Checking for saved AI connection...');
436449

450+
// If we already have a working connection, don't restore
451+
if (this.isConnected && this.aiAssistant && this.aiAssistant.isConnected) {
452+
console.log('✅ AI connection already active, skipping restoration');
453+
return;
454+
}
455+
437456
if (this.isConnected && this.currentProvider) {
438457
console.log(`📂 Found saved connection: ${this.currentProvider}`);
439458

440-
if (!this.aiAssistant) {
441-
console.log('🔄 No AI assistant instance, attempting to restore...');
459+
if (!this.aiAssistant || !this.aiAssistant.isConnected) {
460+
console.log('🔄 No active AI assistant instance, attempting to restore...');
442461
await this.restoreAIAssistant();
443462
} else {
444463
console.log('✅ AI assistant already available, notifying listeners');

0 commit comments

Comments
 (0)