rio-assist-widget 0.1.28 → 0.1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -62
- package/dist/rio-assist.js +164 -104
- package/index.html +38 -38
- package/package.json +40 -40
- package/src/components/conversations-panel/conversations-panel.template.ts +27 -27
- package/src/components/fullscreen/fullscreen.styles.ts +75 -65
- package/src/components/fullscreen/fullscreen.template.ts +34 -0
- package/src/components/mini-panel/mini-panel.styles.ts +52 -51
- package/src/components/mini-panel/mini-panel.template.ts +167 -189
- package/src/components/rio-assist/rio-assist.styles.ts +101 -92
- package/src/components/rio-assist/rio-assist.template.ts +106 -75
- package/src/components/rio-assist/rio-assist.ts +373 -347
- package/src/main.ts +15 -15
- package/src/playground.ts +31 -31
- package/src/services/rioWebsocket.ts +142 -142
|
@@ -31,35 +31,35 @@ type ConversationDeleteTarget = {
|
|
|
31
31
|
index: number;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
type ConversationRenameTarget = {
|
|
35
|
-
id: string;
|
|
36
|
-
title: string;
|
|
37
|
-
index: number;
|
|
38
|
-
draft: string;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
type ConversationActionKind = 'rename' | 'delete';
|
|
42
|
-
|
|
43
|
-
type ConversationActionAttempt = {
|
|
44
|
-
action: ConversationActionKind;
|
|
45
|
-
conversationId: string;
|
|
46
|
-
originalTitle: string;
|
|
47
|
-
index: number;
|
|
48
|
-
newTitle?: string;
|
|
49
|
-
snapshot?: ConversationItem;
|
|
50
|
-
messagesSnapshot?: ChatMessage[];
|
|
51
|
-
wasActive?: boolean;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
type ConversationActionErrorState = ConversationActionAttempt & {
|
|
55
|
-
message: string;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export type HeaderActionConfig = {
|
|
59
|
-
id?: string;
|
|
60
|
-
iconUrl: string;
|
|
61
|
-
ariaLabel?: string;
|
|
62
|
-
onClick?: () => void;
|
|
34
|
+
type ConversationRenameTarget = {
|
|
35
|
+
id: string;
|
|
36
|
+
title: string;
|
|
37
|
+
index: number;
|
|
38
|
+
draft: string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type ConversationActionKind = 'rename' | 'delete';
|
|
42
|
+
|
|
43
|
+
type ConversationActionAttempt = {
|
|
44
|
+
action: ConversationActionKind;
|
|
45
|
+
conversationId: string;
|
|
46
|
+
originalTitle: string;
|
|
47
|
+
index: number;
|
|
48
|
+
newTitle?: string;
|
|
49
|
+
snapshot?: ConversationItem;
|
|
50
|
+
messagesSnapshot?: ChatMessage[];
|
|
51
|
+
wasActive?: boolean;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
type ConversationActionErrorState = ConversationActionAttempt & {
|
|
55
|
+
message: string;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type HeaderActionConfig = {
|
|
59
|
+
id?: string;
|
|
60
|
+
iconUrl: string;
|
|
61
|
+
ariaLabel?: string;
|
|
62
|
+
onClick?: () => void;
|
|
63
63
|
};
|
|
64
64
|
|
|
65
65
|
export class RioAssistWidget extends LitElement {
|
|
@@ -92,6 +92,7 @@ export class RioAssistWidget extends LitElement {
|
|
|
92
92
|
deleteConversationTarget: { attribute: false },
|
|
93
93
|
renameConversationTarget: { attribute: false },
|
|
94
94
|
shortAnswerEnabled: { type: Boolean, state: true },
|
|
95
|
+
newConversationConfirmOpen: { type: Boolean, state: true },
|
|
95
96
|
conversationActionError: { attribute: false },
|
|
96
97
|
headerActions: { attribute: false },
|
|
97
98
|
homeUrl: { type: String, attribute: 'data-home-url' },
|
|
@@ -148,30 +149,32 @@ export class RioAssistWidget extends LitElement {
|
|
|
148
149
|
conversationHistoryLoading = false;
|
|
149
150
|
|
|
150
151
|
conversationHistoryError = '';
|
|
152
|
+
|
|
153
|
+
deleteConversationTarget: ConversationDeleteTarget | null = null;
|
|
154
|
+
|
|
155
|
+
renameConversationTarget: ConversationRenameTarget | null = null;
|
|
151
156
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
renameConversationTarget: ConversationRenameTarget | null = null;
|
|
157
|
+
shortAnswerEnabled = true;
|
|
155
158
|
|
|
156
|
-
|
|
159
|
+
newConversationConfirmOpen = false;
|
|
157
160
|
|
|
158
161
|
conversationActionError: ConversationActionErrorState | null = null;
|
|
159
|
-
|
|
160
|
-
private loadingLabelInternal = 'Rio Insight está respondendo...';
|
|
162
|
+
|
|
163
|
+
private loadingLabelInternal = 'Rio Insight está respondendo...';
|
|
161
164
|
private loadingTimerSlow: number | null = null;
|
|
162
165
|
private loadingTimerTimeout: number | null = null;
|
|
163
166
|
|
|
164
167
|
private refreshConversationsAfterResponse = false;
|
|
165
168
|
|
|
166
169
|
activeConversationTitle: string | null = null;
|
|
167
|
-
|
|
168
|
-
headerActions: HeaderActionConfig[] = [];
|
|
169
|
-
|
|
170
|
-
homeUrl = '';
|
|
171
|
-
|
|
172
|
-
private pendingConversationAction: ConversationActionAttempt | null = null;
|
|
173
|
-
|
|
174
|
-
private generateConversationId() {
|
|
170
|
+
|
|
171
|
+
headerActions: HeaderActionConfig[] = [];
|
|
172
|
+
|
|
173
|
+
homeUrl = '';
|
|
174
|
+
|
|
175
|
+
private pendingConversationAction: ConversationActionAttempt | null = null;
|
|
176
|
+
|
|
177
|
+
private generateConversationId() {
|
|
175
178
|
if (!this.conversationUserId) {
|
|
176
179
|
this.conversationUserId = this.inferUserIdFromToken();
|
|
177
180
|
}
|
|
@@ -384,18 +387,18 @@ export class RioAssistWidget extends LitElement {
|
|
|
384
387
|
this.requestConversationHistory();
|
|
385
388
|
}
|
|
386
389
|
|
|
387
|
-
toggleNewConversationShortcut() {
|
|
388
|
-
this.showNewConversationShortcut = !this.showNewConversationShortcut;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
toggleShortAnswers() {
|
|
392
|
-
this.shortAnswerEnabled = !this.shortAnswerEnabled;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
handleConversationSelect(conversationId: string) {
|
|
396
|
-
if (!conversationId) {
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
390
|
+
toggleNewConversationShortcut() {
|
|
391
|
+
this.showNewConversationShortcut = !this.showNewConversationShortcut;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
toggleShortAnswers() {
|
|
395
|
+
this.shortAnswerEnabled = !this.shortAnswerEnabled;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
handleConversationSelect(conversationId: string) {
|
|
399
|
+
if (!conversationId) {
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
399
402
|
|
|
400
403
|
this.showConversations = false;
|
|
401
404
|
this.conversationMenuId = null;
|
|
@@ -514,11 +517,11 @@ export class RioAssistWidget extends LitElement {
|
|
|
514
517
|
}
|
|
515
518
|
}
|
|
516
519
|
|
|
517
|
-
applyConversationDeletion(id: string) {
|
|
518
|
-
if (!id) {
|
|
519
|
-
return;
|
|
520
|
-
}
|
|
521
|
-
|
|
520
|
+
applyConversationDeletion(id: string) {
|
|
521
|
+
if (!id) {
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
|
|
522
525
|
const wasActive = this.currentConversationId === id;
|
|
523
526
|
const next = this.conversations.filter((conversation) => conversation.id !== id);
|
|
524
527
|
|
|
@@ -529,64 +532,64 @@ export class RioAssistWidget extends LitElement {
|
|
|
529
532
|
this.conversations = next;
|
|
530
533
|
|
|
531
534
|
if (wasActive) {
|
|
532
|
-
this.currentConversationId = null;
|
|
533
|
-
this.activeConversationTitle = null;
|
|
534
|
-
this.messages = [];
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
private restoreConversationSnapshot(snapshot: ConversationItem | undefined, index: number) {
|
|
539
|
-
if (!snapshot) {
|
|
540
|
-
return;
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
const exists = this.conversations.some((conversation) => conversation.id === snapshot.id);
|
|
544
|
-
if (exists) {
|
|
545
|
-
return;
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
const next = [...this.conversations];
|
|
549
|
-
const position = index >= 0 && index <= next.length ? index : next.length;
|
|
550
|
-
next.splice(position, 0, snapshot);
|
|
551
|
-
this.conversations = next;
|
|
552
|
-
}
|
|
535
|
+
this.currentConversationId = null;
|
|
536
|
+
this.activeConversationTitle = null;
|
|
537
|
+
this.messages = [];
|
|
538
|
+
}
|
|
539
|
+
}
|
|
553
540
|
|
|
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
|
-
|
|
541
|
+
private restoreConversationSnapshot(snapshot: ConversationItem | undefined, index: number) {
|
|
542
|
+
if (!snapshot) {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
const exists = this.conversations.some((conversation) => conversation.id === snapshot.id);
|
|
547
|
+
if (exists) {
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const next = [...this.conversations];
|
|
552
|
+
const position = index >= 0 && index <= next.length ? index : next.length;
|
|
553
|
+
next.splice(position, 0, snapshot);
|
|
554
|
+
this.conversations = next;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
async confirmDeleteConversation() {
|
|
558
|
+
const target = this.deleteConversationTarget;
|
|
559
|
+
if (!target) {
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const snapshot =
|
|
564
|
+
this.conversations[target.index] ??
|
|
565
|
+
this.conversations.find((item) => item.id === target.id) ?? {
|
|
566
|
+
id: target.id,
|
|
567
|
+
title: target.title,
|
|
568
|
+
updatedAt: new Date().toISOString(),
|
|
569
|
+
};
|
|
570
|
+
const isActive = this.currentConversationId === target.id;
|
|
571
|
+
this.pendingConversationAction = {
|
|
572
|
+
action: 'delete',
|
|
573
|
+
conversationId: target.id,
|
|
574
|
+
originalTitle: target.title,
|
|
575
|
+
index: target.index,
|
|
576
|
+
snapshot,
|
|
577
|
+
messagesSnapshot: isActive ? [...this.messages] : undefined,
|
|
578
|
+
wasActive: isActive,
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
const success = await this.dispatchConversationAction(
|
|
582
|
+
'delete',
|
|
583
|
+
{ id: target.id, title: target.title },
|
|
584
|
+
target.index,
|
|
585
|
+
);
|
|
586
|
+
if (success) {
|
|
587
|
+
this.deleteConversationTarget = null;
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
this.pendingConversationAction = null;
|
|
592
|
+
}
|
|
590
593
|
|
|
591
594
|
cancelDeleteConversation() {
|
|
592
595
|
this.deleteConversationTarget = null;
|
|
@@ -609,93 +612,93 @@ export class RioAssistWidget extends LitElement {
|
|
|
609
612
|
return;
|
|
610
613
|
}
|
|
611
614
|
|
|
612
|
-
const newTitle = target.draft.trim();
|
|
613
|
-
if (!newTitle) {
|
|
614
|
-
return;
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
this.pendingConversationAction = {
|
|
618
|
-
action: 'rename',
|
|
619
|
-
conversationId: target.id,
|
|
620
|
-
originalTitle: target.title,
|
|
621
|
-
index: target.index,
|
|
622
|
-
newTitle,
|
|
623
|
-
};
|
|
624
|
-
|
|
625
|
-
const success = await this.dispatchConversationAction(
|
|
626
|
-
'rename',
|
|
627
|
-
{ id: target.id, title: newTitle },
|
|
628
|
-
target.index,
|
|
629
|
-
newTitle,
|
|
630
|
-
);
|
|
631
|
-
if (success) {
|
|
632
|
-
this.renameConversationTarget = null;
|
|
633
|
-
return;
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
this.pendingConversationAction = null;
|
|
637
|
-
}
|
|
615
|
+
const newTitle = target.draft.trim();
|
|
616
|
+
if (!newTitle) {
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
638
619
|
|
|
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
|
-
index:
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
620
|
+
this.pendingConversationAction = {
|
|
621
|
+
action: 'rename',
|
|
622
|
+
conversationId: target.id,
|
|
623
|
+
originalTitle: target.title,
|
|
624
|
+
index: target.index,
|
|
625
|
+
newTitle,
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
const success = await this.dispatchConversationAction(
|
|
629
|
+
'rename',
|
|
630
|
+
{ id: target.id, title: newTitle },
|
|
631
|
+
target.index,
|
|
632
|
+
newTitle,
|
|
633
|
+
);
|
|
634
|
+
if (success) {
|
|
635
|
+
this.renameConversationTarget = null;
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
this.pendingConversationAction = null;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
cancelRenameConversation() {
|
|
643
|
+
this.renameConversationTarget = null;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
cancelConversationActionError() {
|
|
647
|
+
this.conversationActionError = null;
|
|
648
|
+
this.pendingConversationAction = null;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
async retryConversationAction() {
|
|
652
|
+
const errorState = this.conversationActionError;
|
|
653
|
+
if (!errorState) {
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
const indexFromState =
|
|
658
|
+
typeof errorState.index === 'number' ? errorState.index : this.conversations.findIndex(
|
|
659
|
+
(item) => item.id === errorState.conversationId,
|
|
660
|
+
);
|
|
661
|
+
const safeIndex =
|
|
662
|
+
indexFromState >= 0
|
|
663
|
+
? indexFromState
|
|
664
|
+
: this.conversations.length > 0
|
|
665
|
+
? this.conversations.length - 1
|
|
666
|
+
: 0;
|
|
667
|
+
|
|
668
|
+
const snapshot =
|
|
669
|
+
errorState.snapshot ??
|
|
670
|
+
this.conversations.find((item) => item.id === errorState.conversationId) ?? {
|
|
671
|
+
id: errorState.conversationId,
|
|
672
|
+
title: errorState.originalTitle,
|
|
673
|
+
updatedAt: new Date().toISOString(),
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
this.pendingConversationAction = {
|
|
677
|
+
action: errorState.action,
|
|
678
|
+
conversationId: errorState.conversationId,
|
|
679
|
+
originalTitle: errorState.originalTitle,
|
|
680
|
+
index: safeIndex,
|
|
681
|
+
newTitle: errorState.newTitle,
|
|
682
|
+
snapshot,
|
|
683
|
+
messagesSnapshot: errorState.messagesSnapshot,
|
|
684
|
+
wasActive: errorState.wasActive,
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
this.conversationActionError = null;
|
|
688
|
+
|
|
689
|
+
await this.dispatchConversationAction(
|
|
690
|
+
errorState.action,
|
|
691
|
+
{ id: errorState.conversationId, title: errorState.newTitle ?? errorState.originalTitle },
|
|
692
|
+
safeIndex,
|
|
693
|
+
errorState.newTitle,
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
private async dispatchConversationAction(
|
|
698
|
+
action: 'rename' | 'delete',
|
|
699
|
+
conversation: Pick<ConversationItem, 'id' | 'title'>,
|
|
700
|
+
index: number,
|
|
701
|
+
newTitle?: string,
|
|
699
702
|
) {
|
|
700
703
|
const eventName =
|
|
701
704
|
action === 'rename' ? 'rioassist:conversation-rename' : 'rioassist:conversation-delete';
|
|
@@ -770,97 +773,97 @@ export class RioAssistWidget extends LitElement {
|
|
|
770
773
|
const action = (message.action ?? '').toLowerCase();
|
|
771
774
|
if (action === 'conversationrenamed') {
|
|
772
775
|
const data = message.data as Record<string, unknown>;
|
|
773
|
-
const id = this.extractString(data, ['conversationId', 'id']);
|
|
774
|
-
const newTitle = this.extractString(data, ['newTitle', 'title']);
|
|
775
|
-
if (id && newTitle) {
|
|
776
|
-
this.applyConversationRename(id, newTitle);
|
|
777
|
-
this.conversationHistoryError = '';
|
|
778
|
-
if (
|
|
779
|
-
this.pendingConversationAction &&
|
|
780
|
-
this.pendingConversationAction.conversationId === id &&
|
|
781
|
-
this.pendingConversationAction.action === 'rename'
|
|
782
|
-
) {
|
|
783
|
-
this.pendingConversationAction = null;
|
|
784
|
-
this.conversationActionError = null;
|
|
785
|
-
}
|
|
786
|
-
}
|
|
787
|
-
return true;
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
if (action === 'conversationdeleted') {
|
|
776
|
+
const id = this.extractString(data, ['conversationId', 'id']);
|
|
777
|
+
const newTitle = this.extractString(data, ['newTitle', 'title']);
|
|
778
|
+
if (id && newTitle) {
|
|
779
|
+
this.applyConversationRename(id, newTitle);
|
|
780
|
+
this.conversationHistoryError = '';
|
|
781
|
+
if (
|
|
782
|
+
this.pendingConversationAction &&
|
|
783
|
+
this.pendingConversationAction.conversationId === id &&
|
|
784
|
+
this.pendingConversationAction.action === 'rename'
|
|
785
|
+
) {
|
|
786
|
+
this.pendingConversationAction = null;
|
|
787
|
+
this.conversationActionError = null;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
return true;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
if (action === 'conversationdeleted') {
|
|
791
794
|
const data = message.data as Record<string, unknown>;
|
|
792
|
-
const id = this.extractString(data, ['conversationId', 'id']);
|
|
793
|
-
if (id) {
|
|
794
|
-
this.applyConversationDeletion(id);
|
|
795
|
-
this.conversationHistoryError = '';
|
|
796
|
-
if (
|
|
797
|
-
this.pendingConversationAction &&
|
|
798
|
-
this.pendingConversationAction.conversationId === id &&
|
|
799
|
-
this.pendingConversationAction.action === 'delete'
|
|
800
|
-
) {
|
|
801
|
-
this.pendingConversationAction = null;
|
|
802
|
-
this.conversationActionError = null;
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
return true;
|
|
806
|
-
}
|
|
795
|
+
const id = this.extractString(data, ['conversationId', 'id']);
|
|
796
|
+
if (id) {
|
|
797
|
+
this.applyConversationDeletion(id);
|
|
798
|
+
this.conversationHistoryError = '';
|
|
799
|
+
if (
|
|
800
|
+
this.pendingConversationAction &&
|
|
801
|
+
this.pendingConversationAction.conversationId === id &&
|
|
802
|
+
this.pendingConversationAction.action === 'delete'
|
|
803
|
+
) {
|
|
804
|
+
this.pendingConversationAction = null;
|
|
805
|
+
this.conversationActionError = null;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
return true;
|
|
809
|
+
}
|
|
807
810
|
|
|
808
|
-
if (action === 'processing') {
|
|
809
|
-
return true;
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
return false;
|
|
813
|
-
}
|
|
814
|
-
|
|
815
|
-
private handleConversationActionError(message: RioIncomingMessage) {
|
|
816
|
-
const action = (message.action ?? '').toLowerCase();
|
|
817
|
-
if (action !== 'error') {
|
|
818
|
-
return false;
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
const data = message.data as Record<string, unknown>;
|
|
822
|
-
const errorText =
|
|
823
|
-
this.extractString(data, ['error', 'message', 'detail', 'description']) ||
|
|
824
|
-
(typeof message.text === 'string' && message.text.trim()
|
|
825
|
-
? message.text
|
|
826
|
-
: 'O agente retornou um erro ao processar a conversa.');
|
|
827
|
-
|
|
828
|
-
const pending = this.pendingConversationAction;
|
|
829
|
-
if (pending) {
|
|
830
|
-
if (pending.action === 'rename') {
|
|
831
|
-
this.applyConversationRename(pending.conversationId, pending.originalTitle);
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
if (pending.action === 'delete') {
|
|
835
|
-
this.restoreConversationSnapshot(pending.snapshot, pending.index);
|
|
836
|
-
if (pending.wasActive) {
|
|
837
|
-
this.currentConversationId = pending.conversationId;
|
|
838
|
-
this.activeConversationTitle = pending.originalTitle;
|
|
839
|
-
this.messages = pending.messagesSnapshot ?? this.messages;
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
this.conversationActionError = {
|
|
844
|
-
...pending,
|
|
845
|
-
message: errorText,
|
|
846
|
-
};
|
|
847
|
-
this.pendingConversationAction = null;
|
|
848
|
-
this.clearLoadingGuard();
|
|
849
|
-
this.isLoading = false;
|
|
850
|
-
return true;
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
this.errorMessage = errorText;
|
|
854
|
-
this.clearLoadingGuard();
|
|
855
|
-
this.isLoading = false;
|
|
856
|
-
return true;
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
private shouldIgnoreAssistantPayload(action?: string) {
|
|
860
|
-
if (!action) {
|
|
861
|
-
return false;
|
|
862
|
-
}
|
|
863
|
-
const normalized = action.toLowerCase();
|
|
811
|
+
if (action === 'processing') {
|
|
812
|
+
return true;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
return false;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
private handleConversationActionError(message: RioIncomingMessage) {
|
|
819
|
+
const action = (message.action ?? '').toLowerCase();
|
|
820
|
+
if (action !== 'error') {
|
|
821
|
+
return false;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
const data = message.data as Record<string, unknown>;
|
|
825
|
+
const errorText =
|
|
826
|
+
this.extractString(data, ['error', 'message', 'detail', 'description']) ||
|
|
827
|
+
(typeof message.text === 'string' && message.text.trim()
|
|
828
|
+
? message.text
|
|
829
|
+
: 'O agente retornou um erro ao processar a conversa.');
|
|
830
|
+
|
|
831
|
+
const pending = this.pendingConversationAction;
|
|
832
|
+
if (pending) {
|
|
833
|
+
if (pending.action === 'rename') {
|
|
834
|
+
this.applyConversationRename(pending.conversationId, pending.originalTitle);
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
if (pending.action === 'delete') {
|
|
838
|
+
this.restoreConversationSnapshot(pending.snapshot, pending.index);
|
|
839
|
+
if (pending.wasActive) {
|
|
840
|
+
this.currentConversationId = pending.conversationId;
|
|
841
|
+
this.activeConversationTitle = pending.originalTitle;
|
|
842
|
+
this.messages = pending.messagesSnapshot ?? this.messages;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
this.conversationActionError = {
|
|
847
|
+
...pending,
|
|
848
|
+
message: errorText,
|
|
849
|
+
};
|
|
850
|
+
this.pendingConversationAction = null;
|
|
851
|
+
this.clearLoadingGuard();
|
|
852
|
+
this.isLoading = false;
|
|
853
|
+
return true;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
this.errorMessage = errorText;
|
|
857
|
+
this.clearLoadingGuard();
|
|
858
|
+
this.isLoading = false;
|
|
859
|
+
return true;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
private shouldIgnoreAssistantPayload(action?: string) {
|
|
863
|
+
if (!action) {
|
|
864
|
+
return false;
|
|
865
|
+
}
|
|
866
|
+
const normalized = action.toLowerCase();
|
|
864
867
|
return (
|
|
865
868
|
normalized === 'processing' ||
|
|
866
869
|
normalized === 'conversationrenamed' ||
|
|
@@ -947,16 +950,39 @@ export class RioAssistWidget extends LitElement {
|
|
|
947
950
|
}
|
|
948
951
|
}
|
|
949
952
|
|
|
950
|
-
handleCreateConversation() {
|
|
951
|
-
if (!this.hasActiveConversation) {
|
|
952
|
-
return;
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
this.
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
this.
|
|
953
|
+
handleCreateConversation() {
|
|
954
|
+
if (!this.hasActiveConversation) {
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
this.newConversationConfirmOpen = true;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
confirmCreateConversation() {
|
|
962
|
+
if (!this.hasActiveConversation) {
|
|
963
|
+
this.newConversationConfirmOpen = false;
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
this.newConversationConfirmOpen = false;
|
|
968
|
+
|
|
969
|
+
this.startNewConversation();
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
cancelCreateConversation() {
|
|
973
|
+
this.newConversationConfirmOpen = false;
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
private startNewConversation() {
|
|
977
|
+
if (!this.hasActiveConversation) {
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
this.clearLoadingGuard();
|
|
982
|
+
this.isLoading = false;
|
|
983
|
+
this.messages = [];
|
|
984
|
+
this.message = '';
|
|
985
|
+
this.errorMessage = '';
|
|
960
986
|
this.showConversations = false;
|
|
961
987
|
this.teardownRioClient();
|
|
962
988
|
this.currentConversationId = this.generateConversationId();
|
|
@@ -1138,45 +1164,45 @@ export class RioAssistWidget extends LitElement {
|
|
|
1138
1164
|
? `Quero uma resposta curta sobre: ${content}`
|
|
1139
1165
|
: content;
|
|
1140
1166
|
const contentToDisplay = content;
|
|
1141
|
-
|
|
1142
|
-
if (!this.currentConversationId) {
|
|
1143
|
-
this.currentConversationId = this.generateConversationId();
|
|
1144
|
-
this.activeConversationTitle = null;
|
|
1145
|
-
}
|
|
1167
|
+
|
|
1168
|
+
if (!this.currentConversationId) {
|
|
1169
|
+
this.currentConversationId = this.generateConversationId();
|
|
1170
|
+
this.activeConversationTitle = null;
|
|
1171
|
+
}
|
|
1146
1172
|
|
|
1147
1173
|
const wasEmptyConversation = this.messages.length === 0;
|
|
1148
1174
|
|
|
1149
|
-
this.dispatchEvent(
|
|
1150
|
-
new CustomEvent('rioassist:send', {
|
|
1151
|
-
detail: {
|
|
1152
|
-
message: content,
|
|
1153
|
-
apiBaseUrl: this.apiBaseUrl,
|
|
1175
|
+
this.dispatchEvent(
|
|
1176
|
+
new CustomEvent('rioassist:send', {
|
|
1177
|
+
detail: {
|
|
1178
|
+
message: content,
|
|
1179
|
+
apiBaseUrl: this.apiBaseUrl,
|
|
1154
1180
|
token: this.rioToken,
|
|
1155
|
-
},
|
|
1156
|
-
bubbles: true,
|
|
1157
|
-
composed: true,
|
|
1158
|
-
}),
|
|
1159
|
-
);
|
|
1160
|
-
|
|
1181
|
+
},
|
|
1182
|
+
bubbles: true,
|
|
1183
|
+
composed: true,
|
|
1184
|
+
}),
|
|
1185
|
+
);
|
|
1186
|
+
|
|
1161
1187
|
const userMessage = this.createMessage('user', contentToDisplay);
|
|
1162
|
-
this.messages = [...this.messages, userMessage];
|
|
1163
|
-
if (wasEmptyConversation) {
|
|
1164
|
-
this.showNewConversationShortcut = true;
|
|
1165
|
-
this.refreshConversationsAfterResponse = true;
|
|
1166
|
-
}
|
|
1167
|
-
this.message = '';
|
|
1168
|
-
this.errorMessage = '';
|
|
1169
|
-
this.isLoading = true;
|
|
1170
|
-
this.startLoadingGuard();
|
|
1171
|
-
|
|
1172
|
-
try {
|
|
1173
|
-
const client = this.ensureRioClient();
|
|
1174
|
-
await client.sendMessage(contentToSend, this.currentConversationId);
|
|
1175
|
-
} catch (error) {
|
|
1176
|
-
this.clearLoadingGuard();
|
|
1177
|
-
this.isLoading = false;
|
|
1178
|
-
this.errorMessage = error instanceof Error
|
|
1179
|
-
? error.message
|
|
1188
|
+
this.messages = [...this.messages, userMessage];
|
|
1189
|
+
if (wasEmptyConversation) {
|
|
1190
|
+
this.showNewConversationShortcut = true;
|
|
1191
|
+
this.refreshConversationsAfterResponse = true;
|
|
1192
|
+
}
|
|
1193
|
+
this.message = '';
|
|
1194
|
+
this.errorMessage = '';
|
|
1195
|
+
this.isLoading = true;
|
|
1196
|
+
this.startLoadingGuard();
|
|
1197
|
+
|
|
1198
|
+
try {
|
|
1199
|
+
const client = this.ensureRioClient();
|
|
1200
|
+
await client.sendMessage(contentToSend, this.currentConversationId);
|
|
1201
|
+
} catch (error) {
|
|
1202
|
+
this.clearLoadingGuard();
|
|
1203
|
+
this.isLoading = false;
|
|
1204
|
+
this.errorMessage = error instanceof Error
|
|
1205
|
+
? error.message
|
|
1180
1206
|
: 'Nao foi possivel enviar a mensagem para o agente.';
|
|
1181
1207
|
}
|
|
1182
1208
|
}
|
|
@@ -1207,17 +1233,17 @@ export class RioAssistWidget extends LitElement {
|
|
|
1207
1233
|
return;
|
|
1208
1234
|
}
|
|
1209
1235
|
|
|
1210
|
-
if (this.handleConversationSystemAction(message)) {
|
|
1211
|
-
return;
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1214
|
-
if (this.handleConversationActionError(message)) {
|
|
1215
|
-
return;
|
|
1216
|
-
}
|
|
1217
|
-
|
|
1218
|
-
if (this.shouldIgnoreAssistantPayload(message.action)) {
|
|
1219
|
-
return;
|
|
1220
|
-
}
|
|
1236
|
+
if (this.handleConversationSystemAction(message)) {
|
|
1237
|
+
return;
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
if (this.handleConversationActionError(message)) {
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
if (this.shouldIgnoreAssistantPayload(message.action)) {
|
|
1245
|
+
return;
|
|
1246
|
+
}
|
|
1221
1247
|
|
|
1222
1248
|
console.info('[RioAssist][ws] resposta de mensagem recebida', {
|
|
1223
1249
|
action: message.action ?? 'message',
|