@@ -2,7 +2,7 @@ import { getOwner } from '@ember/owner';
2
2
import { service } from '@ember/service' ;
3
3
import { tracked } from '@glimmer/tracking' ;
4
4
5
- import { restartableTask } from 'ember-concurrency' ;
5
+ import { restartableTask , timeout } from 'ember-concurrency' ;
6
6
import { Resource } from 'ember-resources' ;
7
7
8
8
import { TrackedMap } from 'tracked-built-ins' ;
@@ -77,7 +77,7 @@ export class RoomResource extends Resource<Args> {
77
77
78
78
// To avoid delay, instead of using `roomResource.activeLLM`, we use a tracked property
79
79
// that updates immediately after the user selects the LLM.
80
- @tracked _activeLLM : string | undefined ;
80
+ @tracked private llmBeingActivated : string | undefined ;
81
81
@service declare private matrixService : MatrixService ;
82
82
@service declare private commandService : CommandService ;
83
83
@service declare private cardService : CardService ;
@@ -89,7 +89,6 @@ export class RoomResource extends Resource<Args> {
89
89
}
90
90
this . _previousRoomId = named . roomId ;
91
91
this . loading = this . load . perform ( named . roomId ) ;
92
- this . _activeLLM = undefined ;
93
92
}
94
93
}
95
94
@@ -223,15 +222,11 @@ export class RoomResource extends Resource<Args> {
223
222
return maybeLastActive ?? this . created . getTime ( ) ;
224
223
}
225
224
226
- get activeLLM ( ) {
227
- return this . _activeLLM ?? this . matrixRoom ?. activeLLM ?? DEFAULT_LLM ;
225
+ get activeLLM ( ) : string {
226
+ return this . llmBeingActivated ?? this . matrixRoom ?. activeLLM ?? DEFAULT_LLM ;
228
227
}
229
228
230
229
activateLLM ( model : string ) {
231
- if ( this . activeLLM === model ) {
232
- return ;
233
- }
234
- this . _activeLLM = model ;
235
230
this . activateLLMTask . perform ( model ) ;
236
231
}
237
232
@@ -240,10 +235,29 @@ export class RoomResource extends Resource<Args> {
240
235
}
241
236
242
237
private activateLLMTask = restartableTask ( async ( model : string ) => {
243
- if ( ! this . matrixRoom ) {
244
- throw new Error ( 'matrixRoom is required to activate LLM' ) ;
238
+ if ( this . activeLLM === model ) {
239
+ return ;
240
+ }
241
+ this . llmBeingActivated = model ;
242
+ try {
243
+ if ( ! this . matrixRoom ) {
244
+ throw new Error ( 'matrixRoom is required to activate LLM' ) ;
245
+ }
246
+ await this . matrixService . sendActiveLLMEvent (
247
+ this . matrixRoom . roomId ,
248
+ model ,
249
+ ) ;
250
+ let remainingRetries = 20 ;
251
+ while ( this . matrixRoom . activeLLM !== model && remainingRetries > 0 ) {
252
+ await timeout ( 50 ) ;
253
+ remainingRetries -- ;
254
+ }
255
+ if ( remainingRetries === 0 ) {
256
+ throw new Error ( 'Failed to activate LLM' ) ;
257
+ }
258
+ } finally {
259
+ this . llmBeingActivated = undefined ;
245
260
}
246
- await this . matrixService . sendActiveLLMEvent ( this . matrixRoom . roomId , model ) ;
247
261
} ) ;
248
262
249
263
private async loadFromEvents ( roomId : string ) {
0 commit comments