From 0d684d4b8dcc9dbbb0dd3ea52baf8d3b5b64fbea Mon Sep 17 00:00:00 2001 From: Florian Richter Date: Tue, 25 Feb 2025 16:15:43 +0100 Subject: [PATCH 1/4] Initialize AIActivationService from preferences In our Theia-derived IDE, the AI feature sometimes is not enabled properly, even though the setting is enabled. We traced this down to the AIActivationServer depending on a signal from the PreferenceService. Fixes #15043 Signed-off-by: Florian Richter --- packages/ai-core/src/browser/ai-activation-service.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/ai-core/src/browser/ai-activation-service.ts b/packages/ai-core/src/browser/ai-activation-service.ts index a914604306995..f28b1dceeac04 100644 --- a/packages/ai-core/src/browser/ai-activation-service.ts +++ b/packages/ai-core/src/browser/ai-activation-service.ts @@ -45,7 +45,8 @@ export class AIActivationService implements FrontendApplicationContribution { } initialize(): MaybePromise { - this.isAiEnabledKey = this.contextKeyService.createKey(ENABLE_AI_CONTEXT_KEY, false); + const value = this.preferenceService.get(ENABLE_AI_CONTEXT_KEY); + this.isAiEnabledKey = this.contextKeyService.createKey(ENABLE_AI_CONTEXT_KEY, value); this.preferenceService.onPreferenceChanged(e => { if (e.preferenceName === PREFERENCE_NAME_ENABLE_AI) { this.isAiEnabledKey.set(e.newValue); From a3c8d4df6e31d85b415d51404b222ccb65d61ebe Mon Sep 17 00:00:00 2001 From: Florian Richter Date: Tue, 25 Feb 2025 17:08:27 +0100 Subject: [PATCH 2/4] Await PreferenceService to be ready Signed-off-by: Florian Richter --- packages/ai-core/src/browser/ai-activation-service.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/ai-core/src/browser/ai-activation-service.ts b/packages/ai-core/src/browser/ai-activation-service.ts index f28b1dceeac04..b149172e2cfb6 100644 --- a/packages/ai-core/src/browser/ai-activation-service.ts +++ b/packages/ai-core/src/browser/ai-activation-service.ts @@ -15,7 +15,7 @@ // ***************************************************************************** import { inject, injectable } from '@theia/core/shared/inversify'; import { FrontendApplicationContribution, PreferenceService } from '@theia/core/lib/browser'; -import { Emitter, MaybePromise, Event, } from '@theia/core'; +import { Emitter, Event } from '@theia/core'; import { ContextKeyService, ContextKey } from '@theia/core/lib/browser/context-key-service'; import { PREFERENCE_NAME_ENABLE_AI } from './ai-core-preferences'; @@ -44,7 +44,8 @@ export class AIActivationService implements FrontendApplicationContribution { return this.isAiEnabledKey.get() ?? false; } - initialize(): MaybePromise { + async initialize(): Promise { + await this.preferenceService.ready; const value = this.preferenceService.get(ENABLE_AI_CONTEXT_KEY); this.isAiEnabledKey = this.contextKeyService.createKey(ENABLE_AI_CONTEXT_KEY, value); this.preferenceService.onPreferenceChanged(e => { From 7f275cb925995b3314f78a7a55ef2121f657566f Mon Sep 17 00:00:00 2001 From: Florian Richter Date: Thu, 27 Feb 2025 08:37:21 +0100 Subject: [PATCH 3/4] Revert "Await PreferenceService to be ready" This reverts commit a3c8d4df6e31d85b415d51404b222ccb65d61ebe. --- packages/ai-core/src/browser/ai-activation-service.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/ai-core/src/browser/ai-activation-service.ts b/packages/ai-core/src/browser/ai-activation-service.ts index b149172e2cfb6..f28b1dceeac04 100644 --- a/packages/ai-core/src/browser/ai-activation-service.ts +++ b/packages/ai-core/src/browser/ai-activation-service.ts @@ -15,7 +15,7 @@ // ***************************************************************************** import { inject, injectable } from '@theia/core/shared/inversify'; import { FrontendApplicationContribution, PreferenceService } from '@theia/core/lib/browser'; -import { Emitter, Event } from '@theia/core'; +import { Emitter, MaybePromise, Event, } from '@theia/core'; import { ContextKeyService, ContextKey } from '@theia/core/lib/browser/context-key-service'; import { PREFERENCE_NAME_ENABLE_AI } from './ai-core-preferences'; @@ -44,8 +44,7 @@ export class AIActivationService implements FrontendApplicationContribution { return this.isAiEnabledKey.get() ?? false; } - async initialize(): Promise { - await this.preferenceService.ready; + initialize(): MaybePromise { const value = this.preferenceService.get(ENABLE_AI_CONTEXT_KEY); this.isAiEnabledKey = this.contextKeyService.createKey(ENABLE_AI_CONTEXT_KEY, value); this.preferenceService.onPreferenceChanged(e => { From 046d2c0f49d9fdda892b0f01af193b9d4bfc59a3 Mon Sep 17 00:00:00 2001 From: Florian Richter Date: Thu, 27 Feb 2025 12:23:56 +0000 Subject: [PATCH 4/4] Update AI enabled setting when preferences become ready Signed-off-by: Florian Richter --- .../src/browser/ai-activation-service.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/ai-core/src/browser/ai-activation-service.ts b/packages/ai-core/src/browser/ai-activation-service.ts index f28b1dceeac04..d1ff622f1a98d 100644 --- a/packages/ai-core/src/browser/ai-activation-service.ts +++ b/packages/ai-core/src/browser/ai-activation-service.ts @@ -44,13 +44,23 @@ export class AIActivationService implements FrontendApplicationContribution { return this.isAiEnabledKey.get() ?? false; } + protected updateEnableValue(value: boolean): void { + if (value !== this.isAiEnabledKey.get()) { + this.isAiEnabledKey.set(value); + this.onDidChangeAIEnabled.fire(value); + } + } + initialize(): MaybePromise { - const value = this.preferenceService.get(ENABLE_AI_CONTEXT_KEY); - this.isAiEnabledKey = this.contextKeyService.createKey(ENABLE_AI_CONTEXT_KEY, value); + this.isAiEnabledKey = this.contextKeyService.createKey(ENABLE_AI_CONTEXT_KEY, false); + // make sure we don't miss once preferences are ready + this.preferenceService.ready.then(() => { + const enableValue = this.preferenceService.get(PREFERENCE_NAME_ENABLE_AI) ?? false; + this.updateEnableValue(enableValue); + }); this.preferenceService.onPreferenceChanged(e => { if (e.preferenceName === PREFERENCE_NAME_ENABLE_AI) { - this.isAiEnabledKey.set(e.newValue); - this.onDidChangeAIEnabled.fire(e.newValue); + this.updateEnableValue(e.newValue); } }); }