-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathroom.gts
856 lines (759 loc) · 26.2 KB
/
room.gts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
import { registerDestructor } from '@ember/destroyable';
import { on } from '@ember/modifier';
import { action } from '@ember/object';
import type Owner from '@ember/owner';
import { schedule } from '@ember/runloop';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { cached } from '@glimmer/tracking';
import {
enqueueTask,
restartableTask,
timeout,
all,
task,
} from 'ember-concurrency';
import perform from 'ember-concurrency/helpers/perform';
import { consume } from 'ember-provide-consume-context';
import { resource, use } from 'ember-resources';
import max from 'lodash/max';
import { MatrixEvent } from 'matrix-js-sdk';
import pluralize from 'pluralize';
import { TrackedObject, TrackedSet } from 'tracked-built-ins';
import { v4 as uuidv4 } from 'uuid';
import { BoxelButton, LoadingIndicator } from '@cardstack/boxel-ui/components';
import { and, eq, not } from '@cardstack/boxel-ui/helpers';
import {
type getCard,
GetCardContextName,
ResolvedCodeRef,
internalKeyFor,
} from '@cardstack/runtime-common';
import { DEFAULT_LLM_LIST } from '@cardstack/runtime-common/matrix-constants';
import AddSkillsToRoomCommand from '@cardstack/host/commands/add-skills-to-room';
import UpdateSkillActivationCommand from '@cardstack/host/commands/update-skill-activation';
import { Message } from '@cardstack/host/lib/matrix-classes/message';
import type { StackItem } from '@cardstack/host/lib/stack-item';
import { getAutoAttachment } from '@cardstack/host/resources/auto-attached-card';
import { RoomResource } from '@cardstack/host/resources/room';
import type CardService from '@cardstack/host/services/card-service';
import type CommandService from '@cardstack/host/services/command-service';
import LoaderService from '@cardstack/host/services/loader-service';
import type MatrixService from '@cardstack/host/services/matrix-service';
import { type MonacoSDK } from '@cardstack/host/services/monaco-service';
import type OperatorModeStateService from '@cardstack/host/services/operator-mode-state-service';
import PlaygroundPanelService from '@cardstack/host/services/playground-panel-service';
import { type CardDef } from 'https://cardstack.com/base/card-api';
import { type FileDef } from 'https://cardstack.com/base/file-api';
import type { SkillCard } from 'https://cardstack.com/base/skill-card';
import AiAssistantAttachmentPicker from '../ai-assistant/attachment-picker';
import AiAssistantChatInput from '../ai-assistant/chat-input';
import LLMSelect from '../ai-assistant/llm-select';
import { AiAssistantConversation } from '../ai-assistant/message';
import NewSession from '../ai-assistant/new-session';
import AiAssistantSkillMenu from '../ai-assistant/skill-menu';
import { Submodes } from '../submode-switcher';
import RoomMessage from './room-message';
import type RoomData from '../../lib/matrix-classes/room';
import type { Skill } from '../ai-assistant/skill-menu';
interface Signature {
Args: {
roomId: string;
roomResource: RoomResource;
monacoSDK: MonacoSDK;
selectedCardRef?: ResolvedCodeRef;
};
}
export default class Room extends Component<Signature> {
<template>
{{#if (not this.doMatrixEventFlush.isRunning)}}
<section
class='room'
data-room-settled={{(and
this.doWhenRoomChanges.isIdle
(not this.matrixService.isLoadingTimeline)
)}}
data-test-room-settled={{(and
this.doWhenRoomChanges.isIdle
(not this.matrixService.isLoadingTimeline)
)}}
data-test-room-name={{@roomResource.name}}
data-test-room={{@roomId}}
>
<AiAssistantConversation
@registerConversationScroller={{this.registerConversationScroller}}
@setScrollPosition={{this.setScrollPosition}}
>
{{#each this.messages key='eventId' as |message i|}}
<RoomMessage
@roomId={{@roomId}}
@roomResource={{@roomResource}}
@index={{i}}
@registerScroller={{this.registerMessageScroller}}
@isPending={{this.isPendingMessage message}}
@monacoSDK={{@monacoSDK}}
@isStreaming={{this.isMessageStreaming message}}
@retryAction={{this.maybeRetryAction i message}}
data-test-message-idx={{i}}
/>
{{else}}
{{#if this.matrixService.isLoadingTimeline}}
<LoadingIndicator
@color='var(--boxel-light)'
class='loading-indicator'
/>
{{else}}
<NewSession @sendPrompt={{this.sendMessage}} />
{{/if}}
{{/each}}
{{#if this.room}}
{{#if this.showUnreadIndicator}}
<div class='unread-indicator'>
<BoxelButton
@size='tall'
@kind='primary'
class='unread-button'
data-test-unread-messages-button
{{on 'click' this.scrollToFirstUnread}}
>{{this.unreadMessageText}}</BoxelButton>
</div>
{{else}}
<AiAssistantSkillMenu
class='skills'
@skills={{this.sortedSkills}}
@onChooseCard={{perform this.attachSkillTask}}
@onUpdateSkillIsActive={{perform this.updateSkillIsActiveTask}}
data-test-skill-menu
/>
{{/if}}
{{/if}}
</AiAssistantConversation>
<footer class='room-actions'>
<div class='chat-input-area' data-test-chat-input-area>
<AiAssistantChatInput
@value={{this.messageToSend}}
@onInput={{this.setMessage}}
@onSend={{this.sendMessage}}
@canSend={{this.canSend}}
data-test-message-field={{@roomId}}
/>
<div class='chat-input-area__bottom-section'>
<AiAssistantAttachmentPicker
@autoAttachedCards={{this.autoAttachedCards}}
@cardsToAttach={{this.cardsToAttach}}
@chooseCard={{this.chooseCard}}
@removeCard={{this.removeCard}}
@chooseFile={{this.chooseFile}}
@removeFile={{this.removeFile}}
@submode={{this.operatorModeStateService.state.submode}}
@autoAttachedFile={{this.autoAttachedFile}}
@filesToAttach={{this.filesToAttach}}
@autoAttachedCardTooltipMessage={{if
(eq this.operatorModeStateService.state.submode Submodes.Code)
'Current card is shared automatically'
'Topmost card is shared automatically'
}}
/>
<LLMSelect
@selected={{@roomResource.activeLLM}}
@onChange={{perform @roomResource.activateLLMTask}}
@options={{this.supportedLLMs}}
@disabled={{@roomResource.isActivatingLLM}}
/>
</div>
</div>
</footer>
</section>
{{/if}}
<style scoped>
.room {
display: grid;
grid-template-rows: 1fr auto;
height: 100%;
overflow: hidden;
}
.skills {
position: sticky;
bottom: 0;
margin-left: auto;
}
.unread-indicator {
position: sticky;
bottom: 0;
margin-left: auto;
width: 100%;
}
.unread-button {
width: 100%;
}
.room-actions {
padding: var(--boxel-sp-xxs) var(--boxel-sp) var(--boxel-sp);
box-shadow: var(--boxel-box-shadow);
}
.chat-input-area {
background-color: var(--boxel-light);
border-radius: var(--boxel-border-radius);
overflow: hidden;
}
.chat-input-area__bottom-section {
display: flex;
justify-content: space-between;
padding-right: var(--boxel-sp-xxs);
gap: var(--boxel-sp-xxl);
}
.chat-input-area__bottom-section
:deep(.ember-basic-dropdown-content-wormhole-origin) {
position: absolute; /* This prevents layout shift when menu opens */
}
:deep(.ai-assistant-conversation > *:first-child) {
margin-top: auto;
}
:deep(.ai-assistant-conversation > *:nth-last-of-type(2)) {
padding-bottom: var(--boxel-sp-xl);
}
.loading-indicator {
margin-top: auto;
margin-bottom: auto;
margin-left: auto;
margin-right: auto;
}
</style>
</template>
@consume(GetCardContextName) private declare getCard: getCard;
@service private declare cardService: CardService;
@service private declare commandService: CommandService;
@service private declare matrixService: MatrixService;
@service private declare operatorModeStateService: OperatorModeStateService;
@service private declare loaderService: LoaderService;
@service private declare playgroundPanelService: PlaygroundPanelService;
private autoAttachmentResource = getAutoAttachment(
this,
() => this.topMostStackItems,
() => this.cardsToAttach,
);
private getConversationScrollability: (() => boolean) | undefined;
private scrollConversationToBottom: (() => void) | undefined;
private roomScrollState: WeakMap<
RoomData,
{
messageElemements: WeakMap<HTMLElement, number>;
messageScrollers: Map<number, Element['scrollIntoView']>;
messageVisibilityObserver: IntersectionObserver;
isScrolledToBottom: boolean;
userHasScrolled: boolean;
isConversationScrollable: boolean;
}
> = new WeakMap();
constructor(owner: Owner, args: Signature['Args']) {
super(owner, args);
this.doMatrixEventFlush.perform();
registerDestructor(this, () => {
this.scrollState().messageVisibilityObserver.disconnect();
});
}
private scrollState() {
if (!this.room) {
throw new Error(`Cannot get room scroll state before room is loaded`);
}
let state = this.roomScrollState.get(this.room);
if (!state) {
state = new TrackedObject({
isScrolledToBottom: false,
userHasScrolled: false,
isConversationScrollable: false,
messageElemements: new WeakMap(),
messageScrollers: new Map(),
messageVisibilityObserver: new IntersectionObserver((entries) => {
entries.forEach((entry) => {
let index = this.messageElements.get(entry.target as HTMLElement);
if (index != null) {
if (
(!this.isConversationScrollable || entry.isIntersecting) &&
index > this.lastReadMessageIndex
) {
this.sendReadReceipt(this.messages[index]);
}
}
});
}),
});
this.roomScrollState.set(this.room, state);
}
return state;
}
// Using a resource for automatically attached files,
// so the file can be reattached after being removed
// when the user opens a different file and then returns to this one.
@use private autoAttachedFileResource = resource(() => {
let state = new TrackedObject<{
value: FileDef | undefined;
remove: () => void;
}>({
value: undefined,
remove: () => {
state.value = undefined;
},
});
if (!this.autoAttachedFileUrl) {
state.value = undefined;
} else {
state.value = this.matrixService.fileAPI.createFileDef({
sourceUrl: this.autoAttachedFileUrl,
name: this.autoAttachedFileUrl.split('/').pop(),
});
}
return state;
});
private get autoAttachedFileUrl() {
return this.operatorModeStateService.state.codePath?.href;
}
private get autoAttachedFile() {
return this.operatorModeStateService.state.submode === Submodes.Code
? this.autoAttachedFileResource.value
: undefined;
}
private get removeAutoAttachedFile() {
return this.autoAttachedFileResource.remove;
}
private get filesToAttach() {
return this.matrixService.filesToSend.get(this.args.roomId) ?? [];
}
@use private playgroundPanelCardResource = resource(() => {
let state = new TrackedObject<{
value: CardDef | undefined;
remove: () => void;
}>({
value: undefined,
remove: () => {
state.value = undefined;
},
});
(async () => {
let cardResource = this.getCard(this, () => {
if (!this.args.selectedCardRef) {
return;
}
let moduleId = internalKeyFor(this.args.selectedCardRef, undefined);
return this.playgroundPanelService.getSelection(moduleId)?.cardId;
});
await cardResource.loaded;
state.value = cardResource.card;
})();
return state;
});
private get playgroundPanelCard() {
return this.playgroundPanelCardResource.value;
}
private get removePlaygroundPanelCard() {
return this.playgroundPanelCardResource.remove;
}
private get isScrolledToBottom() {
return this.scrollState().isScrolledToBottom;
}
private get userHasScrolled() {
return this.scrollState().userHasScrolled;
}
private get isConversationScrollable() {
return this.scrollState().isConversationScrollable;
}
private get messageElements() {
return this.scrollState().messageElemements;
}
private get messageScrollers() {
return this.scrollState().messageScrollers;
}
private get messageVisibilityObserver() {
return this.scrollState().messageVisibilityObserver;
}
private registerMessageScroller = ({
index,
element,
scrollTo,
}: {
index: number;
element: HTMLElement;
scrollTo: (arg?: any) => void;
}) => {
this.messageElements.set(element, index);
this.messageScrollers.set(index, scrollTo);
this.messageVisibilityObserver.observe(element);
this.scrollState().isConversationScrollable = Boolean(
this.getConversationScrollability?.(),
);
if (!this.isConversationScrollable || !this.isAllowedToAutoScroll) {
return;
}
// TODO udpate this so that we preserve the scroll position as the user
// changes rooms and we restore the scroll position when a user enters a room
if (
// If we are permitted to auto-scroll and if there are no unread messages in the
// room, then scroll to the last message in the room.
!this.hasUnreadMessages &&
index === this.messages.length - 1 &&
!this.isScrolledToBottom
) {
scrollTo();
} else if (
// otherwise if we are permitted to auto-scroll and if there are unread
// messages in the room, then scroll to the first unread message in the room.
this.hasUnreadMessages &&
index === this.lastReadMessageIndex + 1
) {
scrollTo();
} else if (this.isScrolledToBottom) {
this.scrollConversationToBottom?.();
}
};
private registerConversationScroller = (
isConversationScrollable: () => boolean,
scrollToBottom: () => void,
) => {
this.getConversationScrollability = isConversationScrollable;
this.scrollConversationToBottom = scrollToBottom;
};
private setScrollPosition = ({ isBottom }: { isBottom: boolean }) => {
this.scrollState().isScrolledToBottom = isBottom;
if (!isBottom) {
this.scrollState().userHasScrolled = true;
}
};
private scrollToFirstUnread = () => {
if (!this.hasUnreadMessages) {
return;
}
let firstUnreadIndex = this.lastReadMessageIndex + 1;
let scrollTo = this.messageScrollers.get(firstUnreadIndex);
if (!scrollTo) {
console.warn(`No scroller for message index ${firstUnreadIndex}`);
} else {
scrollTo({ behavior: 'smooth' });
}
};
private get isAllowedToAutoScroll() {
return !this.userHasScrolled || this.isScrolledToBottom;
}
// For efficiency, read receipts are implemented using “up to” markers. This
// marker indicates that the acknowledgement applies to all events “up to and
// including” the event specified. For example, marking an event as “read” would
// indicate that the user had read all events up to the referenced event.
@cached private get lastReadMessageIndex() {
let readReceiptIndicies: number[] = [];
for (let receipt of this.matrixService.currentUserEventReadReceipts.keys()) {
let maybeIndex = this.messages.findIndex((m) => m.eventId === receipt);
if (maybeIndex != null) {
readReceiptIndicies.push(maybeIndex);
}
}
return max(readReceiptIndicies) ?? -1;
}
@cached private get numberOfUnreadMessages() {
let unreadMessagesCount = 0;
let firstUnreadIndex = this.lastReadMessageIndex + 1;
for (let i = firstUnreadIndex; i < this.messages.length; i++) {
if (
this.matrixService.profile.userId === this.messages[i].author.userId
) {
continue;
}
unreadMessagesCount++;
}
return unreadMessagesCount;
}
private get hasUnreadMessages() {
return this.numberOfUnreadMessages > 0;
}
private get showUnreadIndicator() {
// if user is already scrolled to bottom we don't show the indicator to
// prevent the flash of the indicator appearing and then disappearing during
// the read receipt acknowledgement
return (
this.isConversationScrollable &&
this.hasUnreadMessages &&
!this.isScrolledToBottom
);
}
private get unreadMessageText() {
return `${this.numberOfUnreadMessages} unread ${pluralize(
'message',
this.numberOfUnreadMessages,
)}`;
}
private sendReadReceipt(message: Message) {
if (this.matrixService.profile.userId === message.author.userId) {
return;
}
if (this.matrixService.currentUserEventReadReceipts.has(message.eventId)) {
return;
}
// sendReadReceipt expects an actual MatrixEvent (as defined in the matrix
// SDK), but it' not available to us here - however, we can fake it by adding
// the necessary methods
let matrixEvent = {
getId: () => message.eventId,
getRoomId: () => this.args.roomId,
getTs: () => message.created.getTime(),
};
// Without scheduling this after render, this produces the "attempted to
// update value, but it had already been used previously in the same
// computation" error
schedule('afterRender', () => {
this.matrixService.sendReadReceipt(matrixEvent as MatrixEvent);
});
}
private maybeRetryAction = (messageIndex: number, message: Message) => {
if (this.isLastMessage(messageIndex) && message.isRetryable) {
return this.resendLastMessage;
}
return undefined;
};
private isMessageStreaming = (message: Message) => {
return !message.isStreamingFinished;
};
private doMatrixEventFlush = restartableTask(async () => {
await this.matrixService.flushMembership;
await this.matrixService.flushTimeline;
await this.args.roomResource.processing;
});
private get messages() {
return this.args.roomResource.messages;
}
private get skills(): Skill[] {
return this.args.roomResource.skills;
}
private get supportedLLMs(): string[] {
return DEFAULT_LLM_LIST.sort();
}
private get sortedSkills(): Skill[] {
return [...this.skills].sort((a, b) => {
// Not all of the skills have a title, so we use the skillEventId as a fallback
// which should be consistent.
let aTitle = a.card.title || a.skillEventId;
let bTitle = b.card.title || b.skillEventId;
return aTitle.localeCompare(bTitle);
});
}
private get room() {
let room = this.args.roomResource.matrixRoom;
return room;
}
private doWhenRoomChanges = restartableTask(async () => {
await all([this.cardService.cardsSettled(), timeout(500)]);
});
private get messageToSend() {
return this.matrixService.messagesToSend.get(this.args.roomId) ?? '';
}
private get cardsToAttach() {
return this.matrixService.cardsToSend.get(this.args.roomId);
}
@action private resendLastMessage() {
if (!this.room) {
throw new Error(
'Bug: should not be able to resend a message without a room.',
);
}
let myMessages = this.messages.filter(
(message) => message.author.userId === this.matrixService.userId,
);
if (myMessages.length === 0) {
throw new Error(
'Bug: should not be able to resend a message that does not exist.',
);
}
let myLastMessage = myMessages[myMessages.length - 1];
let attachedCards = (myLastMessage!.attachedResources(this) || [])
.map((resource) => resource.card)
.filter((card) => card !== undefined) as CardDef[];
this.doSendMessage.perform(
myLastMessage.message,
attachedCards,
myLastMessage.attachedFiles,
true,
myLastMessage.clientGeneratedId,
);
}
@action
private setMessage(message: string) {
this.matrixService.messagesToSend.set(this.args.roomId, message);
}
@action
private sendMessage(prompt?: string) {
let cards = [];
if (this.cardsToAttach) {
cards.push(...this.cardsToAttach);
}
if (this.autoAttachedCards.size > 0) {
this.autoAttachedCards.forEach((card) => {
cards.push(card);
});
}
let files = [];
if (this.autoAttachedFile) {
files.push(this.autoAttachedFile);
}
files.push(...this.filesToAttach);
this.doSendMessage.perform(
prompt ?? this.messageToSend,
cards.length ? cards : undefined,
files.length ? files : undefined,
Boolean(prompt),
);
}
@action
private chooseCard(card: CardDef) {
let cards = this.cardsToAttach ?? [];
if (!cards?.find((c) => c.id === card.id)) {
this.matrixService.cardsToSend.set(this.args.roomId, [...cards, card]);
}
}
@action
private isAutoAttachedCard(card: CardDef) {
return this.autoAttachedCards.has(card);
}
@action
private removeCard(card: CardDef) {
if (this.playgroundPanelCard?.id === card.id) {
this.removePlaygroundPanelCard();
} else if (this.isAutoAttachedCard(card)) {
this.autoAttachmentResource.onCardRemoval(card);
} else {
const cardIndex = this.cardsToAttach?.findIndex((c) => c.id === card.id);
if (cardIndex != undefined && cardIndex !== -1) {
if (this.cardsToAttach !== undefined) {
this.autoAttachmentResource.onCardRemoval(
this.cardsToAttach[cardIndex],
);
this.cardsToAttach.splice(cardIndex, 1);
}
}
}
this.matrixService.cardsToSend.set(
this.args.roomId,
this.cardsToAttach?.length ? this.cardsToAttach : undefined,
);
}
@action
private chooseFile(file: FileDef) {
let files = this.filesToAttach;
if (!files?.find((f) => f.sourceUrl === file.sourceUrl)) {
this.matrixService.filesToSend.set(this.args.roomId, [...files, file]);
}
}
@action
private isAutoAttachedFile(file: FileDef) {
return this.autoAttachedFile?.sourceUrl === file.sourceUrl;
}
@action
private removeFile(file: FileDef) {
if (this.isAutoAttachedFile(file)) {
this.removeAutoAttachedFile();
return;
}
const fileIndex = this.filesToAttach?.findIndex(
(f) => f.sourceUrl === file.sourceUrl,
);
if (fileIndex != undefined && fileIndex !== -1) {
if (this.filesToAttach !== undefined) {
this.filesToAttach.splice(fileIndex, 1);
}
}
this.matrixService.cardsToSend.set(
this.args.roomId,
this.cardsToAttach?.length ? this.cardsToAttach : undefined,
);
}
private doSendMessage = enqueueTask(
async (
message: string | undefined,
cards?: CardDef[],
files?: FileDef[],
keepInputAndAttachments: boolean = false,
clientGeneratedId: string = uuidv4(),
) => {
if (!keepInputAndAttachments) {
// this is for situations when a message is sent via some other way than the text box
// (example: ai prompt from new-session screen)
// if there were cards attached or a typed — but not sent — message, do not erase or remove them
this.matrixService.messagesToSend.set(this.args.roomId, undefined);
this.matrixService.cardsToSend.set(this.args.roomId, undefined);
}
let context = {
submode: this.operatorModeStateService.state.submode,
openCardIds: this.operatorModeStateService
.topMostStackItems()
.filter((stackItem) => stackItem)
.map((stackItem) => stackItem.card.id),
};
try {
if (files) {
files = await this.matrixService.uploadFiles(files);
}
await this.matrixService.sendMessage(
this.args.roomId,
message,
cards,
files,
clientGeneratedId,
context,
);
} catch (e) {
console.error('Error sending message', e);
}
},
);
private get topMostStackItems(): StackItem[] {
return this.operatorModeStateService.topMostStackItems();
}
private get autoAttachedCards() {
if (this.operatorModeStateService.state.submode === Submodes.Code) {
return this.playgroundPanelCard
? new TrackedSet([this.playgroundPanelCard])
: new TrackedSet<CardDef>();
}
return this.autoAttachmentResource.cards;
}
updateSkillIsActiveTask = task(
async (skillEventId: string, isActive: boolean) => {
await new UpdateSkillActivationCommand(
this.commandService.commandContext,
).execute({
roomId: this.args.roomId,
skillEventId,
isActive,
});
},
);
private get canSend() {
return (
!this.doSendMessage.isRunning &&
Boolean(
this.messageToSend?.trim() ||
this.cardsToAttach?.length ||
this.autoAttachedCards.size !== 0,
) &&
!!this.room &&
!this.messages.some((m) => this.isPendingMessage(m)) &&
!this.matrixService.isLoadingTimeline
);
}
@action
private isLastMessage(messageIndex: number) {
return (this.room && messageIndex === this.messages.length - 1) ?? false;
}
private isPendingMessage(message: Message) {
return message.status === 'sending' || message.status === 'queued';
}
private attachSkillTask = task(async (card: SkillCard) => {
let addSkillsToRoomCommand = new AddSkillsToRoomCommand(
this.commandService.commandContext,
);
await addSkillsToRoomCommand.execute({
roomId: this.args.roomId,
skills: [card],
});
});
}
declare module '@glint/environment-ember-loose/registry' {
export default interface Room {
'Matrix::Room': typeof Room;
}
}