Skip to content

Commit

Permalink
Better error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Sep 7, 2023
1 parent f196bbf commit 447203b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,31 @@
</div>

<div class="item" *ngFor="let item of snapshot.chatTalk">
<div class="row mt-3" *ngIf="item.isUser">
<div class="row mt-3" *ngIf="item.type === 'user'">
<div class="col">
<div class="bubble bubble-left">
{{item.prompt}}
{{item.text}}
</div>
</div>
<div class="col-auto">
<img class="user-picture" title="{{user.displayName}}" [src]="user.id | sqxUserIdPicture" />
</div>
</div>

<div class="row mt-3" *ngIf="!item.isUser">
<div class="row mt-3" *ngIf="item.type === 'system'">
<div class="col-auto">
<div class="squid squid-sm d-flex align-items-center justify-content-center">
<img src="./images/squid.svg">
</div>
</div>
<div class="col">
<div class="bubble bubble-right">
{{ item.text | sqxTranslate}}
</div>
</div>
</div>

<div class="row mt-3" *ngIf="item.type === 'bot'">
<div class="col-auto">
<div class="squid squid-sm d-flex align-items-center justify-content-center">
<img src="./images/squid.svg">
Expand All @@ -43,10 +56,10 @@
{{ 'chat.answer' | sqxTranslate}}
</div>

<textarea class="form-control" readonly [ngModel]="item.prompt"></textarea>
<textarea class="form-control" readonly [ngModel]="item.text"></textarea>

<div class="mt-2">
<button type="button" class="btn btn-primary" (click)="select.emit(item.prompt)">
<button type="button" class="btn btn-primary" (click)="select.emit(item.text)">
{{ 'chat.use' | sqxTranslate }}
</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { Component, ElementRef, EventEmitter, Output, ViewChild } from '@angular/core';
import { delay } from 'rxjs/operators';
import { AppsState, AuthService, StatefulComponent, TranslationsService } from '@app/shared';

interface State {
Expand All @@ -16,7 +17,7 @@ interface State {
chatQuestion: string;

// The answers.
chatTalk: ReadonlyArray<{ prompt: string; isUser?: boolean }>;
chatTalk: ReadonlyArray<{ text: string; type: 'user' | 'bot' | 'system' }>;
}

@Component({
Expand Down Expand Up @@ -64,22 +65,34 @@ export class ChatDialogComponent extends StatefulComponent<State> {
chatQuestion: '',
chatTalk: [
...s.chatTalk,
{ prompt, isUser: true },
{ text: prompt, type: 'user' },
],
isRunning: true,
}));

this.translator.ask(this.appsState.appName, { prompt })
this.translator.ask(this.appsState.appName, { prompt }).pipe(delay(500))
.subscribe({
next: chatAnswers => {
this.next(s => ({
...s,
chatTalk: [
...s.chatTalk,
...chatAnswers.map(answer => ({ prompt: answer })),
],
isRunning: false,
}));
if (chatAnswers.length === 0) {
this.next(s => ({
...s,
chatQuestion: '',
chatTalk: [
...s.chatTalk,
{ text: 'i18n:chat.answersEmpty', type: 'system' },
],
isRunning: true,
}));
} else {
this.next(s => ({
...s,
chatTalk: [
...s.chatTalk,
...chatAnswers.map(text => ({ text, type: 'bot' } as any)),
],
isRunning: false,
}));
}

setTimeout(() => {
this.input.nativeElement.focus();
Expand Down

0 comments on commit 447203b

Please sign in to comment.