Skip to content

Commit

Permalink
Merge pull request #50 from boldare/chore/packages-update
Browse files Browse the repository at this point in the history
Chore: packages update
  • Loading branch information
sebastianmusial authored Mar 29, 2024
2 parents 7c2c53b + e1ff507 commit 40d1910
Show file tree
Hide file tree
Showing 20 changed files with 3,122 additions and 5,789 deletions.
24 changes: 10 additions & 14 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ What kind of change does this PR introduce?

<!-- Please check the one that applies to this PR using "x". -->

```
[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:
```
- [ ] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, local variables)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] CI related changes
- [ ] Documentation content changes
- [ ] Other... Please describe:

## What is the current behavior?

Expand All @@ -25,10 +23,8 @@ Issue Number: N/A

## Does this PR introduce a breaking change?

```
[ ] Yes
[ ] No
```
- [ ] Yes
- [ ] No

<!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. -->

Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/app/chat/agents/currency/get-currency.agent.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable } from '@nestjs/common';
import { AssistantCreateParams } from 'openai/resources/beta';
import { FunctionTool } from 'openai/resources/beta';
import { AgentBase, AgentData, AgentService } from '@boldare/openai-assistant';
import { CurrencyService } from './currency.service';

@Injectable()
export class GetCurrencyAgent extends AgentBase {
override definition: AssistantCreateParams.AssistantToolsFunction = {
override definition: FunctionTool = {
type: 'function',
function: {
name: this.constructor.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable } from '@nestjs/common';
import { AssistantCreateParams } from 'openai/resources/beta';
import { AgentBase, AgentService } from '@boldare/openai-assistant';
import { PokemonService } from './pokemon.service';
import { FunctionTool } from 'openai/resources/beta';

@Injectable()
export class GetPokemonListAgent extends AgentBase {
override definition: AssistantCreateParams.AssistantToolsFunction = {
override definition: FunctionTool = {
type: 'function',
function: {
name: this.constructor.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable } from '@nestjs/common';
import { AssistantCreateParams } from 'openai/resources/beta';
import { FunctionTool } from 'openai/resources/beta';
import { AgentBase, AgentData, AgentService } from '@boldare/openai-assistant';
import { PokemonService } from './pokemon.service';

@Injectable()
export class GetPokemonStatsAgent extends AgentBase {
override definition: AssistantCreateParams.AssistantToolsFunction = {
override definition: FunctionTool = {
type: 'function',
function: {
name: this.constructor.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable } from '@nestjs/common';
import { AssistantCreateParams } from 'openai/resources/beta';
import { FunctionTool } from 'openai/resources/beta';
import { AgentBase, AgentData, AgentService } from '@boldare/openai-assistant';
import { WeatherService } from './weather.service';

@Injectable()
export class GetCurrentWeatherAgent extends AgentBase {
override definition: AssistantCreateParams.AssistantToolsFunction = {
override definition: FunctionTool = {
type: 'function',
function: {
name: this.constructor.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, Input, OnInit } from '@angular/core';
import { ChatClientService } from '../../../modules/+chat/shared/chat-client.service';
import { Message, SpeechVoice } from '../../../modules/+chat/shared/chat.model';
import { ChatMessage, SpeechVoice } from '../../../modules/+chat/shared/chat.model';
import { environment } from '../../../../environments/environment';
import { MatIconModule } from '@angular/material/icon';
import { delay } from 'rxjs';
Expand All @@ -15,7 +15,7 @@ import { NgClass } from '@angular/common';
styleUrl: './chat-audio.component.scss',
})
export class ChatAudioComponent implements OnInit {
@Input() message!: Message;
@Input() message!: ChatMessage;
isAudioEnabled = environment.isAudioEnabled;
isStarted = false;
audio = new Audio();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, HostBinding, Input } from '@angular/core';
import { ChatRole, Message } from '../../../modules/+chat/shared/chat.model';
import { ChatRole, ChatMessage } from '../../../modules/+chat/shared/chat.model';
import { MarkdownComponent } from 'ngx-markdown';
import { ChatAudioComponent } from '../chat-audio/chat-audio.component';
import { NgClass } from '@angular/common';
Expand All @@ -18,7 +18,7 @@ import { ChatAvatarComponent } from '../chat-avatar/chat-avatar.component';
],
})
export class ChatMessageComponent {
@Input() message!: Message;
@Input() message!: ChatMessage;
@Input() class = '';
chatRole = ChatRole;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SimpleChanges,
ViewChildren,
} from '@angular/core';
import { Message } from '../../../modules/+chat/shared/chat.model';
import { ChatMessage } from '../../../modules/+chat/shared/chat.model';
import { ChatMessageComponent } from '../chat-message/chat-message.component';
import { ChatTypingComponent } from '../chat-typing/chat-typing.component';
import { ChatContentComponent } from '../chat-content/chat-content.component';
Expand All @@ -29,7 +29,7 @@ import { ChatTipsComponent } from '../chat-tips/chat-tips.component';
],
})
export class ChatMessagesComponent implements AfterViewInit, OnChanges {
@Input() messages: Message[] = [];
@Input() messages: ChatMessage[] = [];
@Input() isTyping = false;
@Input() tips: string[] = [];
@Output() tipSelected$ = new EventEmitter<string>();
Expand Down
4 changes: 2 additions & 2 deletions apps/spa/src/app/modules/+chat/shared/chat.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export enum ChatRole {
System = 'system',
}

export interface Message {
export interface ChatMessage {
metadata?: Record<string, unknown>;
content: string;
role: ChatRole;
Expand All @@ -19,7 +19,7 @@ export enum ChatEvents {
MessageReceived = 'message_received',
}

export enum MessageStatus {
export enum ChatMessageStatus {
Invisible = 'invisible',
}

Expand Down
26 changes: 15 additions & 11 deletions apps/spa/src/app/modules/+chat/shared/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import {
take,
tap,
} from 'rxjs';
import { ChatRole, Message, MessageStatus } from './chat.model';
import { ChatRole, ChatMessage, ChatMessageStatus } from './chat.model';
import { ChatGatewayService } from './chat-gateway.service';
import { ChatClientService } from './chat-client.service';
import { ThreadService } from './thread.service';
import { ChatFilesService } from './chat-files.service';
import { environment } from '../../../../environments/environment';
import { OpenAiFile, GetThreadResponseDto } from '@boldare/openai-assistant';
import { Threads } from 'openai/resources/beta';
import MessageContentText = Threads.MessageContentText;
import { ThreadMessage } from 'openai/resources/beta/threads';
import { Message } from 'openai/resources/beta/threads/messages';
import { TextContentBlock } from 'openai/resources/beta/threads/messages/messages';


@Injectable({ providedIn: 'root' })
export class ChatService {
isLoading$ = new BehaviorSubject<boolean>(false);
isVisible$ = new BehaviorSubject<boolean>(environment.isAutoOpen);
isTyping$ = new BehaviorSubject<boolean>(false);
messages$ = new BehaviorSubject<Message[]>([]);
messages$ = new BehaviorSubject<ChatMessage[]>([]);

constructor(
private readonly chatGatewayService: ChatGatewayService,
Expand All @@ -40,24 +40,28 @@ export class ChatService {
this.watchVisibility();
}

isMessageInvisible(message: ThreadMessage): boolean {
isMessageInvisible(message: Message): boolean {
const metadata = message.metadata as Record<string, unknown>;
return metadata?.['status'] === MessageStatus.Invisible;
return metadata?.['status'] === ChatMessageStatus.Invisible;
}

isTextMessage(message: ThreadMessage): boolean {
isTextMessage(message: Message): boolean {
return message.content?.[0]?.type === 'text';
}

parseMessages(thread: GetThreadResponseDto): Message[] {
parseMessages(thread: GetThreadResponseDto): ChatMessage[] {
if (!thread.messages) {
return [];
}

return thread.messages
.reverse()
.filter(
message =>
this.isTextMessage(message) && !this.isMessageInvisible(message),
)
.map(message => ({
content: (message.content[0] as MessageContentText).text.value,
content: (message.content[0] as TextContentBlock).text.value,
role: message.role as ChatRole,
}));
}
Expand Down Expand Up @@ -92,7 +96,7 @@ export class ChatService {
this.messages$.next([]);
}

addMessage(message: Message): void {
addMessage(message: ChatMessage): void {
this.messages$.next([...this.messages$.value, message]);
}

Expand Down
9 changes: 7 additions & 2 deletions apps/spa/src/app/modules/+chat/shared/thread.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, Subject, take, tap } from 'rxjs';
import { BehaviorSubject, catchError, Observable, Subject, take, tap } from 'rxjs';
import { environment } from '../../../../environments/environment';
import { ThreadClientService } from './thread-client.service';
import { ConfigurationFormService } from '../../+configuration/shared/configuration-form.service';
Expand Down Expand Up @@ -43,6 +43,11 @@ export class ThreadService {
}

getThread(id: string): Observable<GetThreadResponseDto> {
return this.threadClientService.getThread(id).pipe(take(1));
return this.threadClientService
.getThread(id)
.pipe(
take(1),
catchError(() => this.start()),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { CreateThreadDto } from '@boldare/openai-assistant';
import { ConfigurationForm } from './configuration.model';
import { MessageStatus, SpeechVoice } from '../../+chat/shared/chat.model';
import { ChatMessageStatus, SpeechVoice } from '../../+chat/shared/chat.model';

@Injectable({ providedIn: 'root' })
export class ConfigurationFormService {
Expand All @@ -20,7 +20,7 @@ export class ConfigurationFormService {
* first name: ${this.form.controls.firstName.value || '-'}
`,
metadata: {
status: MessageStatus.Invisible,
status: ChatMessageStatus.Invisible,
},
},
],
Expand Down
4 changes: 2 additions & 2 deletions libs/openai-assistant/src/lib/agent/agent.base.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { OnModuleInit } from '@nestjs/common';
import { AssistantCreateParams } from 'openai/resources/beta';
import { FunctionTool } from 'openai/resources/beta';
import { AgentService } from './agent.service';
import { AgentData } from './agent.model';

export class AgentBase implements OnModuleInit {
definition!: AssistantCreateParams.AssistantToolsFunction;
definition!: FunctionTool;

onModuleInit(): void {
this.agentService.add(this.definition, this.output.bind(this));
Expand Down
6 changes: 3 additions & 3 deletions libs/openai-assistant/src/lib/agent/agent.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Injectable } from '@nestjs/common';
import { Agent, Agents } from './agent.model';
import { AssistantCreateParams } from 'openai/resources/beta';
import { FunctionTool } from 'openai/resources/beta';

@Injectable()
export class AgentService {
public agents: Agents = {};
public tools: AssistantCreateParams.AssistantToolsFunction[] = [];
public tools: FunctionTool[] = [];

add(
definition: AssistantCreateParams.AssistantToolsFunction,
definition: FunctionTool,
fn: Agent,
): void {
this.tools.push(definition);
Expand Down
4 changes: 3 additions & 1 deletion libs/openai-assistant/src/lib/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export class ChatGateway implements OnGatewayConnection {
this.logger = new Logger(ChatGateway.name);
}

async handleConnection() {}
async handleConnection() {
this.logger.log('Client connected');
}

@SubscribeMessage(ChatEvents.SendMessage)
async listenForMessages(
Expand Down
11 changes: 5 additions & 6 deletions libs/openai-assistant/src/lib/chat/chat.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Injectable } from '@nestjs/common';
import {
MessageContentText,
Run,
ThreadMessage,
Message,
Run, TextContentBlock,
} from 'openai/resources/beta/threads';
import { AiService } from '../ai';

Expand All @@ -18,19 +17,19 @@ export class ChatHelpers {
return this.parseThreadMessage(lastThreadMessage);
}

parseThreadMessage(message?: ThreadMessage): string {
parseThreadMessage(message?: Message): string {
if (!message) {
return `Seems I'm lost, would you mind reformulating your question`;
}

const content = message.content[0] as MessageContentText;
const content = message.content[0] as TextContentBlock;
return content.text.value;
}

async getLastMessage(
run: Run,
role = 'assistant',
): Promise<ThreadMessage | undefined> {
): Promise<Message | undefined> {
const messages = await this.threads.messages.list(run.thread_id);
return (
messages.data
Expand Down
10 changes: 4 additions & 6 deletions libs/openai-assistant/src/lib/threads/threads.model.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import {
MessageContentImageFile,
MessageContentText,
} from 'openai/resources/beta/threads/messages/messages';
import { IsOptional } from 'class-validator';
import { ImageFileContentBlock, TextContentBlock } from 'openai/resources/beta/threads/messages/messages';
import { Message } from 'openai/resources/beta/threads';

export class GetThreadDto {
@ApiProperty({ description: 'Unique identifier of the thread.' })
Expand Down Expand Up @@ -45,7 +43,7 @@ export class ThreadMessage {
@ApiProperty({
description: 'Content of the message in array of text and/or images.',
})
content!: Array<MessageContentImageFile | MessageContentText>;
content!: Array<ImageFileContentBlock | TextContentBlock>;

@ApiProperty({
description: 'Role of the message author.',
Expand Down Expand Up @@ -83,7 +81,7 @@ export class GetThreadResponseDto {
type: ThreadMessage,
isArray: true,
})
messages!: ThreadMessage[];
messages!: Message[];
}

export class CreateThreadResponseDto {
Expand Down
Loading

0 comments on commit 40d1910

Please sign in to comment.