Skip to content

Commit ed59655

Browse files
committedFeb 19, 2025
Improve state management of switching LLM models
1 parent dc10876 commit ed59655

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed
 

‎packages/host/app/resources/room.ts

+26-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getOwner } from '@ember/owner';
22
import { service } from '@ember/service';
33
import { tracked } from '@glimmer/tracking';
44

5-
import { restartableTask } from 'ember-concurrency';
5+
import { restartableTask, timeout } from 'ember-concurrency';
66
import { Resource } from 'ember-resources';
77

88
import { TrackedMap } from 'tracked-built-ins';
@@ -77,7 +77,7 @@ export class RoomResource extends Resource<Args> {
7777

7878
// To avoid delay, instead of using `roomResource.activeLLM`, we use a tracked property
7979
// that updates immediately after the user selects the LLM.
80-
@tracked _activeLLM: string | undefined;
80+
@tracked private llmBeingActivated: string | undefined;
8181
@service declare private matrixService: MatrixService;
8282
@service declare private commandService: CommandService;
8383
@service declare private cardService: CardService;
@@ -89,7 +89,6 @@ export class RoomResource extends Resource<Args> {
8989
}
9090
this._previousRoomId = named.roomId;
9191
this.loading = this.load.perform(named.roomId);
92-
this._activeLLM = undefined;
9392
}
9493
}
9594

@@ -223,15 +222,11 @@ export class RoomResource extends Resource<Args> {
223222
return maybeLastActive ?? this.created.getTime();
224223
}
225224

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;
228227
}
229228

230229
activateLLM(model: string) {
231-
if (this.activeLLM === model) {
232-
return;
233-
}
234-
this._activeLLM = model;
235230
this.activateLLMTask.perform(model);
236231
}
237232

@@ -240,10 +235,29 @@ export class RoomResource extends Resource<Args> {
240235
}
241236

242237
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;
245260
}
246-
await this.matrixService.sendActiveLLMEvent(this.matrixRoom.roomId, model);
247261
});
248262

249263
private async loadFromEvents(roomId: string) {

0 commit comments

Comments
 (0)