Skip to content

Commit

Permalink
Merge branch 'master' into 7218-chat-add-streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Mutugiii committed Dec 15, 2023
2 parents e81f02d + c94bc63 commit 770a5c4
Show file tree
Hide file tree
Showing 41 changed files with 295 additions and 207 deletions.
3 changes: 2 additions & 1 deletion chatapi/src/models/db-doc.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export interface DbDoc {
_id: string;
_rev: string;
user: string;
user: any;
title: string;
time: number;
conversations: [];
}
1 change: 1 addition & 0 deletions chatapi/src/services/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export async function chat(data: any, stream?: boolean, callback?: (response: st
if (dbData._id) {
await retrieveChatHistory(dbData, messages);
} else {
dbData.title = content;
dbData.conversations = [];
}

Expand Down
19 changes: 13 additions & 6 deletions chatapi/src/utils/db.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ import { ChatMessage } from '../models/chat-message.model';
async function getChatDocument(id: string) {
try {
const res = await db.get(id) as DbDoc;
return res.conversations;
// Should return user, team data as well particularly for the /conversations endpoint
return {
'conversations': res.conversations,
'title': res.title
};
// Should return user, team data as well particularly for the "/conversations" endpoint
} catch (error) {
return [];
return {
'conversations': [],
'title': ''
};
}
}

export async function retrieveChatHistory(dbData: any, messages: ChatMessage[]) {
const history = await getChatDocument(dbData._id);
dbData.conversations = history;
const { conversations, title } = await getChatDocument(dbData._id);
dbData.conversations = conversations;
dbData.title = title;

for (const { query, response } of history) {
for (const { query, response } of conversations) {
messages.push({ 'role': 'user', 'content': query });
messages.push({ 'role': 'assistant', 'content': response });
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "planet",
"license": "AGPL-3.0",
"version": "0.13.75",
"version": "0.13.92",
"myplanet": {
"latest": "v0.11.46",
"min": "v0.11.21"
"latest": "v0.11.97",
"min": "v0.11.72"
},
"scripts": {
"ng": "ng",
Expand Down
22 changes: 18 additions & 4 deletions src/app/chat/chat-sidebar/chat-sidebar.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@
<mat-form-field class="font-size-1 margin-lr-3">
<input matInput i18n-placeholder placeholder="Search" [(ngModel)]="titleSearch" (input)="onSearchChange()">
</mat-form-field>
<button mat-icon-button color="primary" (click)="resetFilter()"><mat-icon>delete</mat-icon></button><br>
<span style="font-size: small; font-style: italic;" i18n>Full Text Search </span>
<mat-checkbox [checked]="fullTextSearch" (change)="toggleSearchType()"></mat-checkbox>
<button mat-icon-button color="primary" (click)="resetFilter()" [disabled]="!titleSearch && !searchType"><mat-icon>delete</mat-icon></button><br>
<div>
<span style="font-size: small; font-style: italic;" i18n>Full Conversation Search </span>
<mat-checkbox [checked]="fullTextSearch" (change)="toggleSearchType()"></mat-checkbox>
<button style="text-align: end;" mat-icon-button (mouseenter)="toggleOverlay()" (mouseleave)="toggleOverlay()" cdkOverlayOrigin #trigger="cdkOverlayOrigin">
<mat-icon>help_outline</mat-icon>
</button>
<ng-template cdkConnectedOverlay [cdkConnectedOverlayOrigin]="trigger" [cdkConnectedOverlayOpen]="overlayOpen">
<span class="overlay-text" i18n>search through the entire conversation, not just the title</span>
</ng-template>
<div>
<mat-button-toggle-group *ngIf="fullTextSearch" [(ngModel)]="searchType" (change)="filterConversations()">
<mat-button-toggle value="questions" i18n>Questions</mat-button-toggle>
<mat-button-toggle value="responses" i18n>Responses</mat-button-toggle>
</mat-button-toggle-group>
</div>
</div>
</div>
<ng-container *ngIf="filteredConversations?.length; else noChats">
<ul>
<li *ngFor="let conversation of filteredConversations; let i = index" (click)="selectConversation(conversation)">
<li *ngFor="let conversation of filteredConversations.reverse(); let i = index" (click)="selectConversation(conversation)">
<ng-container *ngIf="isEditing; else notEditing">
<ng-container *ngIf="selectedConversation?._id === conversation?._id; else conversationTitle">
<form [formGroup]="titleForm[conversation?._id]" (ngSubmit)="submitTitle(conversation)">
Expand Down
31 changes: 20 additions & 11 deletions src/app/chat/chat-sidebar/chat-sidebar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ChatService } from '../../shared/chat.service';
import { CouchService } from '../../shared/couchdb.service';
import { SearchService } from '../../shared/forms/search.service';
import { showFormErrors } from '../../shared/table-helpers';
import { UserService } from '../../shared/user.service';

@Component({
selector: 'planet-chat-sidebar',
Expand All @@ -21,6 +22,8 @@ export class ChatSidebarComponent implements OnInit, OnDestroy {
selectedConversation: any;
isEditing: boolean;
fullTextSearch = false;
searchType: 'questions' | 'responses';
overlayOpen = false;
titleForm: { [key: string]: FormGroup } = {};
private _titleSearch = '';
get titleSearch(): string { return this._titleSearch.trim(); }
Expand All @@ -33,8 +36,9 @@ export class ChatSidebarComponent implements OnInit, OnDestroy {
constructor(
private chatService: ChatService,
private couchService: CouchService,
private formBuilder: FormBuilder,
private searchService: SearchService,
private formBuilder: FormBuilder
private userService: UserService
) {}

ngOnInit() {
Expand Down Expand Up @@ -64,6 +68,10 @@ export class ChatSidebarComponent implements OnInit, OnDestroy {
this.isEditing = !this.isEditing;
}

toggleOverlay() {
this.overlayOpen = !this.overlayOpen;
}

updateConversation(conversation, title) {
this.couchService.updateDocument(
this.dbName, { ...conversation, title: title, updatedTime: this.couchService.datePlaceholder }
Expand Down Expand Up @@ -92,7 +100,7 @@ export class ChatSidebarComponent implements OnInit, OnDestroy {
}

getChatHistory() {
this.chatService.findConversations([], {}).subscribe(
this.chatService.findConversations([], [ this.userService.get().name ]).subscribe(
(conversations) => {
this.conversations = conversations;
this.filteredConversations = [ ...conversations ];
Expand All @@ -116,6 +124,7 @@ export class ChatSidebarComponent implements OnInit, OnDestroy {

resetFilter() {
this.titleSearch = '';
this.searchType = null;
}

recordSearch(complete = false) {
Expand All @@ -135,23 +144,23 @@ export class ChatSidebarComponent implements OnInit, OnDestroy {
this.getChatHistory();
}

this.filteredConversations = this.conversations.filter(conversation => {
// full-text search
this.filteredConversations = this.conversations?.filter(conversation => {
if (this.fullTextSearch) {
const conversationMatches = conversation.conversations.some(chat => {
const queryMatch = chat.query?.toLowerCase().includes(this.titleSearch.toLowerCase());
const responseMatch = chat.response?.toLowerCase().includes(this.titleSearch.toLowerCase());
return queryMatch || responseMatch;
if (this.searchType === 'questions') {
return queryMatch;
} else if (this.searchType === 'responses') {
return responseMatch;
} else {
return queryMatch || responseMatch;
}
});
return conversationMatches;
}

const titleMatch = conversation.title?.toLowerCase().includes(this.titleSearch.toLowerCase());
const initialQueryMatch = conversation.conversations[0].query?.toLowerCase().includes(
this.titleSearch.toLowerCase()
);

return conversation.title ? titleMatch : initialQueryMatch;
return conversation.title?.toLowerCase().includes(this.titleSearch.toLowerCase());
});
}
}
10 changes: 10 additions & 0 deletions src/app/chat/chat-sidebar/chat-sidebar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ li:hover {
align-self: flex-end;
}

.overlay-text {
width: 100px;
border: solid 1px #ccc;
border-radius: 5px;
background: #fff;
text-align: center;
padding: 10px;
margin: 0;
}

@media only screen and (max-width: $screen-md) {
.expand-button {
top: 10px;
Expand Down
6 changes: 3 additions & 3 deletions src/app/chat/chat-window/chat-window.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { takeUntil } from 'rxjs/operators';
import { CustomValidators } from '../../validators/custom-validators';
import { ChatService } from '../../shared/chat.service';
import { CouchService } from '../../shared/couchdb.service';
import { UserService } from '../../shared/user.service';
import { showFormErrors } from '../../shared/table-helpers';
import { UserService } from '../../shared/user.service';

@Component({
selector: 'planet-chat-window',
Expand All @@ -23,7 +23,7 @@ export class ChatWindowComponent implements OnInit, OnDestroy {
selectedConversationId: any;
promptForm: FormGroup;
data = {
user: this.userService.get(),
user: this.userService.get().name,
time: this.couchService.datePlaceholder,
content: '',
_id: '',
Expand Down Expand Up @@ -80,7 +80,7 @@ export class ChatWindowComponent implements OnInit, OnDestroy {

fetchConversation(id) {
if (id) {
this.chatService.findConversations([ id ], {}).subscribe(
this.chatService.findConversations([ id ]).subscribe(
(conversation: Object) => {
const messages = conversation[0]?.conversations;

Expand Down
8 changes: 2 additions & 6 deletions src/app/community/community.component.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<div class="space-container">
<div class="view-container view-full-height community-view">
<mat-toolbar class="action-buttons">
<span>{{configuration?.name}}</span>
</mat-toolbar>
<div class="community-news">
<mat-tab-group class="tabs-padding" (selectedTabChange)="tabChanged($event)">
<mat-tab i18n-label label="News">
<h3>
<span i18n>Your Voices</span>
<mat-tab i18n-label label="Our Voices">
<h3 style="text-align: right; margin-right: 0.5rem;">
<ng-container *planetAuthorizedRoles="'learner'">
<button mat-stroked-button (click)="openAddMessageDialog()" *ngIf="showNewsButton" i18n>New Voice</button>
</ng-container>
Expand Down
6 changes: 3 additions & 3 deletions src/app/community/community.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export class CommunityComponent implements OnInit, OnDestroy {

openDescriptionDialog() {
const submitDescription = ({ description }) => {
this.teamsService.updateTeam({ ...this.team, description: description.text, images: description.images }).pipe(
this.teamsService.updateTeam({ ...this.team, description: description.text }).pipe(
finalize(() => this.dialogsLoadingService.stop())
).subscribe(newTeam => {
this.team = newTeam;
Expand All @@ -310,8 +310,8 @@ export class CommunityComponent implements OnInit, OnDestroy {
this.dialogsFormService.openDialogsForm(
this.team.description ? $localize`Edit Description` : $localize`Add Description`,
[ { name: 'description', placeholder: $localize`Description`, type: 'markdown', required: true, imageGroup: 'community' } ],
{ description: { text: this.team.description || '', images: this.team.images || [] } },
{ onSubmit: submitDescription }
{ description: this.team.description || '' },
{ autoFocus: true, onSubmit: submitDescription }
);
}

Expand Down
42 changes: 4 additions & 38 deletions src/app/community/community.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,26 @@

display: grid;
grid-template-columns: 2fr 1fr;
grid-template-rows: 64px calc(100% - 64px - 0.5rem);
grid-template-areas:
"links calendar"
"news calendar";
grid-template-areas: "news calendar";
gap: 0.5rem;

@media only screen and (max-width: #{$screen-sm}) {
@media only screen and (max-width: #{$screen-md}) {
grid-template-columns: 1fr;
grid-template-areas:
"links"
"news";
grid-template-areas: "news";
}
}

.fc .fc-toolbar {
display: flex;
flex-direction: column;
align-items: center;
}

.view-container mat-toolbar {
grid-area: links;
overflow: hidden;
}

.community-news {

grid-area: news;
overflow-y: auto;

h3 {

margin: 0;

*:not(:last-child) {
margin-right: 0.25rem;
}

}

}

planet-calendar {
Expand All @@ -58,25 +38,11 @@ planet-calendar {
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
grid-template-rows: repeat(auto-fill, 125px);
grid-auto-rows: 125px;
margin: 0 0.5rem;
.top-right-icon {
position: absolute;
top: -0.25rem;
right: -0.25rem;
}
}

.fc-h-event .fc-event-title {
text-overflow: ellipsis;
}

@media only screen and (min-width: #{$screen-md}) {
.fc .fc-more-popover .fc-popover-body {
min-width: unset !important;

.fc-event-title-container {
max-width: 100px;
}
}
}
10 changes: 6 additions & 4 deletions src/app/courses/courses.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,17 @@ export class CoursesComponent implements OnInit, OnChanges, AfterViewInit, OnDes
const { inShelf, notInShelf } = this.userService.countInShelf(selected.filter(id => this.hasSteps(id)), 'courseIds');
this.selectedEnrolled = inShelf;
this.selectedNotEnrolled = notInShelf;
this.selectedLocal = selected.filter(id => this.isLocal(id)).length;
this.selectedLocal = selected.filter(id => this.isLocalOrNation(id)).length;
}

hasSteps(id: string) {
return this.courses.data.find((course: any) => course._id === id && course.doc.steps.length > 0);
}

isLocal(id: string) {
return this.courses.data.find((course: any) => course._id === id && course.doc.sourcePlanet === this.planetConfiguration.code);
isLocalOrNation(id: string) {
return this.courses.data.find((course: any) =>
course._id === id && (course.doc.sourcePlanet === this.planetConfiguration.code || this.planetConfiguration.parentCode === 'earth')
);
}

onFilterChange(filterValue: string, field: string) {
Expand Down Expand Up @@ -382,7 +384,7 @@ export class CoursesComponent implements OnInit, OnChanges, AfterViewInit, OnDes
}

shareLocal(selectedIds) {
const localSelections = selectedIds.filter(id => this.isLocal(id) !== undefined);
const localSelections = selectedIds.filter(id => this.isLocalOrNation(id) !== undefined);
this.shareCourse('push', localSelections);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ <h3 class="margin-lr-3 ellipsis-title"><ng-container i18n>Step</ng-container> {{
</mat-form-field>
<span>{{stepNum}}/{{maxStep}}</span>
<button mat-icon-button [disabled]="stepNum === 1" (click)="changeStep(-1)"><mat-icon>navigate_before</mat-icon></button>
<button mat-icon-button [disabled]="stepNum === maxStep || (!parent && stepDetail?.exam?.questions.length > 0 && !attempts)" (click)="changeStep(1)"><mat-icon>navigate_next</mat-icon></button>
<button mat-icon-button *ngIf="stepNum !== maxStep" [disabled]="stepNum === maxStep || (!parent && stepDetail?.exam?.questions.length > 0 && !attempts)" (click)="changeStep(1)"><mat-icon>navigate_next</mat-icon></button>
<button *ngIf="stepNum === maxStep" mat-raised-button color="basic" (click)="backToCourseDetail()">Finish</button>
</div>
</mat-toolbar>
<div class="view-container view-full-height" *ngIf="stepDetail?.description || resource?._attachments; else emptyRecord">
Expand Down
Loading

0 comments on commit 770a5c4

Please sign in to comment.