Skip to content

Commit 8bce43f

Browse files
committed
Update based on feedbacks
1 parent b42d1d4 commit 8bce43f

15 files changed

+103
-85
lines changed

packages/host/app/components/ai-assistant/past-session-item.gts

+1-9
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,7 @@ export default class PastSessionItem extends Component<Signature> {
206206
}
207207

208208
get isStreaming() {
209-
if (!this.args.session.lastMessage) {
210-
return false;
211-
}
212-
213-
return (
214-
this.args.session.lastMessage.author.userId !==
215-
this.matrixService.userId &&
216-
!this.args.session.lastMessage.isStreamingFinished
217-
);
209+
return this.args.session.lastMessage?.isStreamingFinished === false;
218210
}
219211

220212
get hasUnseenMessage() {

packages/host/app/components/ai-assistant/past-sessions.gts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default class AiAssistantPastSessionsList extends Component<Signature> {
6868
{{#each @sessions key='roomId' as |session|}}
6969
<PastSessionItem @session={{session}} @actions={{@roomActions}} />
7070
{{/each}}
71-
{{#if this.matrixService.loadingMoreAIRooms}}
71+
{{#if this.matrixService.isLoadingMoreAIRooms}}
7272
<li
7373
class='loading-indicator-container'
7474
data-test-loading-more-rooms

packages/host/app/services/matrix-sdk-loader.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ export type ExtendedClient = Pick<
7676
| 'fetchRoomEvent'
7777
| 'forget'
7878
| 'getAccessToken'
79-
| 'getAccountData'
8079
| 'getJoinedRooms'
8180
| 'getProfileInfo'
8281
| 'getRoom'
@@ -99,6 +98,7 @@ export type ExtendedClient = Pick<
9998
| 'sendEvent'
10099
| 'sendReadReceipt'
101100
| 'sendStateEvent'
101+
| 'setAccountData'
102102
| 'setDisplayName'
103103
| 'setPassword'
104104
| 'setPowerLevel'

packages/host/app/services/matrix-service.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { debounce } from '@ember/runloop';
44
import Service, { service } from '@ember/service';
55
import { cached, tracked } from '@glimmer/tracking';
66

7-
import { restartableTask, task } from 'ember-concurrency';
7+
import { task, dropTask } from 'ember-concurrency';
88
import window from 'ember-window-mock';
99
import { cloneDeep } from 'lodash';
1010
import {
@@ -119,7 +119,7 @@ import type ResetService from './reset';
119119

120120
import type * as MatrixSDK from 'matrix-js-sdk';
121121

122-
const { matrixURL, environment } = ENV;
122+
const { matrixURL } = ENV;
123123
const MAX_CARD_SIZE_KB = 60;
124124
const STATE_EVENTS_OF_INTEREST = ['m.room.create', 'm.room.name'];
125125
const SLIDING_SYNC_AI_ROOM_LIST_NAME = 'ai-room';
@@ -181,7 +181,7 @@ export default class MatrixService extends Service {
181181

182182
private slidingSync: SlidingSync | undefined;
183183
private aiRoomIds: Set<string> = new Set();
184-
@tracked private isLoadingMoreAIRooms = false;
184+
@tracked private _isLoadingMoreAIRooms = false;
185185

186186
constructor(owner: Owner) {
187187
super(owner);
@@ -555,7 +555,7 @@ export default class MatrixService extends Service {
555555
filters: {
556556
is_dm: true,
557557
},
558-
timeline_limit: 1,
558+
timeline_limit: SLIDING_SYNC_LIST_TIMELINE_LIMIT,
559559
required_state: [['*', '*']],
560560
});
561561
this.slidingSync = new this.matrixSdkLoader.SlidingSync(
@@ -565,7 +565,7 @@ export default class MatrixService extends Service {
565565
timeline_limit: SLIDING_SYNC_LIST_TIMELINE_LIMIT,
566566
},
567567
this.client as any,
568-
environment === 'test' ? 200 : 2000,
568+
500,
569569
);
570570
this.slidingSync.on(
571571
SlidingSyncEvent.Lifecycle,
@@ -1224,7 +1224,7 @@ export default class MatrixService extends Service {
12241224
return await this.client.isUsernameAvailable(username);
12251225
}
12261226

1227-
private loadAllTimelineEvents = restartableTask(async (roomId: string) => {
1227+
private loadAllTimelineEvents = dropTask(async (roomId: string) => {
12281228
let roomData = this.ensureRoomData(roomId);
12291229
let room = this.client.getRoom(roomId);
12301230
let roomResource = this.roomResources.get(roomId);
@@ -1669,8 +1669,12 @@ export default class MatrixService extends Service {
16691669
this.loadMoreAIRoomsTask.perform();
16701670
}
16711671

1672-
private loadMoreAIRoomsTask = restartableTask(async () => {
1673-
if (!this.slidingSync) return;
1672+
private loadMoreAIRoomsTask = dropTask(async () => {
1673+
if (!this.slidingSync) {
1674+
throw new Error(
1675+
'To load more AI rooms, sliding sync must be initialized',
1676+
);
1677+
}
16741678

16751679
let currentList = this.slidingSync.getListParams(
16761680
SLIDING_SYNC_AI_ROOM_LIST_NAME,
@@ -1686,22 +1690,26 @@ export default class MatrixService extends Service {
16861690

16871691
let newEndRange = currentRange[1] + 10;
16881692

1689-
this.isLoadingMoreAIRooms = true;
1693+
this._isLoadingMoreAIRooms = true;
16901694
try {
16911695
await this.slidingSync.setListRanges(SLIDING_SYNC_AI_ROOM_LIST_NAME, [
16921696
[0, newEndRange],
16931697
]);
16941698
} finally {
1695-
this.isLoadingMoreAIRooms = false;
1699+
this._isLoadingMoreAIRooms = false;
16961700
}
16971701
});
16981702

1699-
get loadingMoreAIRooms() {
1700-
return this.isLoadingMoreAIRooms;
1703+
get isLoadingMoreAIRooms() {
1704+
return this._isLoadingMoreAIRooms;
17011705
}
17021706

17031707
async loadMoreAuthRooms(realms: string[]) {
1704-
if (!this.slidingSync) return;
1708+
if (!this.slidingSync) {
1709+
throw new Error(
1710+
'To load more auth rooms, sliding sync must be initialized',
1711+
);
1712+
}
17051713

17061714
let currentList = this.slidingSync.getListParams(
17071715
SLIDING_SYNC_AUTH_ROOM_LIST_NAME,

packages/host/tests/acceptance/ai-assistant-test.gts

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
} from '../helpers/base-realm';
3636

3737
import { setupMockMatrix } from '../helpers/mock-matrix';
38+
import { getRoomIdForRealmAndUser } from '../helpers/mock-matrix/_utils';
3839
import { setupApplicationTest } from '../helpers/setup';
3940

4041
async function selectCardFromCatalog(cardId: string) {
@@ -52,6 +53,10 @@ module('Acceptance | AI Assistant tests', function (hooks) {
5253
let mockMatrixUtils = setupMockMatrix(hooks, {
5354
loggedInAs: '@testuser:localhost',
5455
activeRealms: [baseRealm.url, testRealmURL],
56+
directRooms: [
57+
getRoomIdForRealmAndUser(testRealmURL, '@testuser:localhost'),
58+
getRoomIdForRealmAndUser(baseRealm.url, '@testuser:localhost'),
59+
],
5560
});
5661

5762
let { createAndJoinRoom, getRoomState } = mockMatrixUtils;

packages/host/tests/helpers/mock-matrix.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface Config {
1717
expiresInSec?: number;
1818
autostart?: boolean;
1919
now?: () => number;
20-
directRooms?: Record<string, string[]>;
20+
directRooms?: string[];
2121
}
2222

2323
export function setupMockMatrix(

packages/host/tests/helpers/mock-matrix/_client.ts

+26-19
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,16 @@ export class MockClient implements ExtendedClient {
5252
async getAccountDataFromServer<T extends { [k: string]: any }>(
5353
_eventType: string,
5454
): Promise<T | null> {
55-
return {
56-
realms: this.sdkOpts.activeRealms ?? [],
57-
} as unknown as T;
55+
if (_eventType === 'm.direct') {
56+
return {
57+
[this.loggedInAs!]: this.sdkOpts.directRooms ?? [],
58+
} as unknown as T;
59+
} else if (_eventType === APP_BOXEL_REALMS_EVENT_TYPE) {
60+
return {
61+
realms: this.sdkOpts.activeRealms ?? [],
62+
} as unknown as T;
63+
}
64+
return null;
5865
}
5966

6067
get loggedInAs() {
@@ -105,6 +112,7 @@ export class MockClient implements ExtendedClient {
105112
nonce: nonce++,
106113
permissions,
107114
};
115+
108116
let stringifiedHeader = JSON.stringify(header);
109117
let stringifiedPayload = JSON.stringify(payload);
110118
let headerAndPayload = `${btoa(stringifiedHeader)}.${btoa(
@@ -131,7 +139,7 @@ export class MockClient implements ExtendedClient {
131139
if (type === APP_BOXEL_REALMS_EVENT_TYPE) {
132140
this.sdkOpts.activeRealms = (data as any).realms;
133141
} else if (type === 'm.direct') {
134-
this.sdkOpts.directRooms = data as any;
142+
this.sdkOpts.directRooms = (data as any)[this.loggedInAs!];
135143
} else {
136144
throw new Error(
137145
'Support for updating this event type in account data is not yet implemented in this mock.',
@@ -636,22 +644,21 @@ export class MockClient implements ExtendedClient {
636644
): Promise<MSC3575SlidingSyncResponse> {
637645
let lists: MSC3575SlidingSyncResponse['lists'] = {};
638646
let rooms: MSC3575SlidingSyncResponse['rooms'] = {};
639-
Object.entries(req.lists || {}).forEach(([listKey, list]) => {
640-
list.ranges.forEach((range) => {
641-
let [start, end] = range;
647+
for (const [listKey, list] of Object.entries(req.lists || {})) {
648+
for (let i = 0; i < list.ranges.length; i++) {
649+
let [start, end] = list.ranges[i];
642650
//currently we only filter rooms using is_dm
643-
let dmRooms =
644-
this.getAccountData('m.direct')?.getContent()?.rooms ?? [];
651+
let dmRooms = (await this.getAccountDataFromServer('m.direct')) ?? {};
645652
let roomsInRange = this.serverState.rooms
646-
.filter((r) => r.id.includes('mock'))
647653
.filter((r) =>
648654
list.filters?.is_dm
649-
? dmRooms.includes(r.id)
650-
: !dmRooms.includes(r.id),
655+
? dmRooms[this.loggedInAs!]?.includes(r.id)
656+
: !dmRooms[this.loggedInAs!]?.includes(r.id),
651657
)
652658
.slice(start, end + 1);
653659

654-
roomsInRange.forEach((room) => {
660+
for (let j = 0; j < roomsInRange.length; j++) {
661+
let room = roomsInRange[j];
655662
let timeline = this.serverState.getRoomEvents(room.id);
656663
rooms[room.id] = {
657664
name:
@@ -665,11 +672,11 @@ export class MockClient implements ExtendedClient {
665672
invited_count: 0,
666673
initial: true,
667674
};
668-
669-
timeline.forEach((event: MatrixSDK.IRoomEvent) => {
675+
for (let k = 0; k < timeline.length; k++) {
676+
let event = timeline[k];
670677
this.emitEvent(new MatrixEvent(event));
671-
});
672-
});
678+
}
679+
}
673680

674681
lists[listKey] = {
675682
count: roomsInRange.length,
@@ -681,8 +688,8 @@ export class MockClient implements ExtendedClient {
681688
},
682689
],
683690
};
684-
});
685-
});
691+
}
692+
}
686693

687694
let response: MSC3575SlidingSyncResponse = {
688695
pos: String(Date.now()),

packages/host/tests/helpers/mock-matrix/_server-state.ts

+32-30
Original file line numberDiff line numberDiff line change
@@ -98,39 +98,41 @@ export class ServerState {
9898
{ origin_server_ts: timestamp, state_key: sender },
9999
);
100100

101-
this.addRoomEvent(
102-
sender,
103-
{
104-
room_id: roomId,
105-
type: 'm.room.member',
106-
content: {
107-
displayname: 'aibot',
108-
membership: 'invite',
101+
if (!roomId.includes('test-session-room-realm')) {
102+
this.addRoomEvent(
103+
sender,
104+
{
105+
room_id: roomId,
106+
type: 'm.room.member',
107+
content: {
108+
displayname: 'aibot',
109+
membership: 'invite',
110+
},
109111
},
110-
},
111-
{
112-
origin_server_ts: timestamp,
113-
// host application expects this for the bot to join the room
114-
state_key: '@aibot:localhost',
115-
},
116-
);
112+
{
113+
origin_server_ts: timestamp,
114+
// host application expects this for the bot to join the room
115+
state_key: '@aibot:localhost',
116+
},
117+
);
117118

118-
this.addRoomEvent(
119-
'@aibot:localhost',
120-
{
121-
room_id: roomId,
122-
type: 'm.room.member',
123-
content: {
124-
displayname: 'aibot',
125-
membership: 'join',
119+
this.addRoomEvent(
120+
'@aibot:localhost',
121+
{
122+
room_id: roomId,
123+
type: 'm.room.member',
124+
content: {
125+
displayname: 'aibot',
126+
membership: 'join',
127+
},
126128
},
127-
},
128-
{
129-
origin_server_ts: timestamp,
130-
// host application expects this for the bot to join the room
131-
state_key: '@aibot:localhost',
132-
},
133-
);
129+
{
130+
origin_server_ts: timestamp,
131+
// host application expects this for the bot to join the room
132+
state_key: '@aibot:localhost',
133+
},
134+
);
135+
}
134136

135137
return roomId;
136138
}

packages/host/tests/helpers/mock-matrix/_utils.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class MockUtils {
2525
};
2626

2727
getRoomIdForRealmAndUser = (realmURL: string, userId: string) => {
28-
return `test-session-room-realm-${realmURL}-user-${userId}`;
28+
return getRoomIdForRealmAndUser(realmURL, userId);
2929
};
3030

3131
getRoomState = (roomId: string, eventType: string, stateKey?: string) => {
@@ -109,3 +109,7 @@ export class MockUtils {
109109
function isRealmEvent(e: IEvent): e is RealmEvent {
110110
return e.type === APP_BOXEL_REALM_EVENT_TYPE;
111111
}
112+
113+
export function getRoomIdForRealmAndUser(realmURL: string, userId: string) {
114+
return `test-session-room-realm-${realmURL}-user-${userId}`;
115+
}

packages/realm-server/node-realm.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export class NodeAdapter implements RealmAdapter {
224224

225225
try {
226226
dmRooms =
227-
(await matrixClient.getAccountData<Record<string, string>>(
227+
(await matrixClient.getAccountDataFromServer<Record<string, string>>(
228228
'boxel.session-rooms',
229229
)) ?? {};
230230
} catch (e) {

packages/realm-server/server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ export class RealmServer {
461461

462462
private sendEvent = async (user: string, eventType: string) => {
463463
let dmRooms =
464-
(await this.matrixClient.getAccountData<Record<string, string>>(
464+
(await this.matrixClient.getAccountDataFromServer<Record<string, string>>(
465465
'boxel.session-rooms',
466466
)) ?? {};
467467
let roomId = dmRooms[user];

packages/realm-server/tests/auth-client-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module(basename(__filename), function () {
3737
async hashMessageWithSecret(_message: string): Promise<string> {
3838
throw new Error('Method not implemented.');
3939
},
40-
async getAccountData() {
40+
async getAccountDataFromServer() {
4141
return {};
4242
},
4343
async setAccountData() {

packages/runtime-common/matrix-backend-authentication.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class MatrixBackendAuthentication {
7171
}
7272

7373
let dmRooms =
74-
(await this.matrixClient.getAccountData<Record<string, string>>(
74+
(await this.matrixClient.getAccountDataFromServer<Record<string, string>>(
7575
'boxel.session-rooms',
7676
)) ?? {};
7777
let roomId = dmRooms[user];
@@ -136,7 +136,7 @@ export class MatrixBackendAuthentication {
136136
}
137137

138138
let dmRooms =
139-
(await this.matrixClient.getAccountData<Record<string, string>>(
139+
(await this.matrixClient.getAccountDataFromServer<Record<string, string>>(
140140
'boxel.session-rooms',
141141
)) ?? {};
142142
let roomId = dmRooms[user];

0 commit comments

Comments
 (0)