Skip to content

Commit

Permalink
feat(openai-assistant): assistants streaming events added
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianmusial committed Mar 28, 2024
1 parent e1ff507 commit 399a1d2
Show file tree
Hide file tree
Showing 24 changed files with 544 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Component, Input, OnInit } from '@angular/core';
import { ChatClientService } from '../../../modules/+chat/shared/chat-client.service';
import { ChatMessage, 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 Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Component, HostBinding, Input } from '@angular/core';
import { ChatRole, ChatMessage } 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@

<ai-spinner [isActive]="isLoading()" />
@if (isConfigEnabled && !threadId()) {
<ai-configuration-form class="chat__content" />
<ai-configuration-form class="chat__content" />
} @else {
<ai-chat-messages
[messages]="messages()"
[isTyping]="isTyping()"
[tips]="tips"
(tipSelected$)="chatService.sendMessage($event)"
class="chat__content" />
<ai-chat-footer
[isDisabled]="isTyping()"
[isTranscriptionEnabled]="isTranscriptionEnabled"
[isAttachmentEnabled]="isAttachmentEnabled"
(sendMessage$)="chatService.sendMessage($event)"
(sendAudio$)="chatService.sendAudio($event)" />
<ai-chat-messages
[messages]="messages()"
[isTyping]="isTyping()"
[tips]="tips"
(tipSelected$)="chatService.sendMessage($event)"
class="chat__content" />
<ai-chat-footer
[isDisabled]="isTyping()"
[isTranscriptionEnabled]="isTranscriptionEnabled"
[isAttachmentEnabled]="isAttachmentEnabled"
(sendMessage$)="chatService.sendMessage($event)"
(sendAudio$)="chatService.sendAudio($event)" />
}
</ai-card>
4 changes: 2 additions & 2 deletions apps/spa/src/app/modules/+chat/shared/chat-gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export class ChatGatewayService {
private socket = io(environment.websocketUrl);

sendMessage(payload: ChatCallDto): void {
this.socket.emit(ChatEvents.SendMessage, payload);
this.socket.emit(ChatEvents.CallStart, payload);
}

getMessages(): Observable<ChatCallDto> {
return new Observable<ChatCallDto>(observer => {
this.socket.on(ChatEvents.MessageReceived, data => observer.next(data));
this.socket.on(ChatEvents.CallDone, data => observer.next(data));
return () => this.socket.disconnect();
});
}
Expand Down
17 changes: 15 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 @@ -15,8 +15,21 @@ export interface ChatMessage {
}

export enum ChatEvents {
SendMessage = 'send_message',
MessageReceived = 'message_received',
CallStart = 'callStart',
CallDone = 'callDone',
TextDone = 'textCreated',
TextCreated = 'textDelta',
TextDelta = 'textDone',
MessageCreated = 'messageCreated',
MessageDelta = 'messageDelta',
MessageDone = 'messageDone',
ImageFileDone = 'imageFileDone',
ToolCallCreated = 'toolCallCreated',
ToolCallDelta = 'toolCallDelta',
ToolCallDone = 'toolCallDone',
RunStepCreated = 'runStepCreated',
RunStepDelta = 'runStepDelta',
RunStepDone = 'runStepDone',
}

export enum ChatMessageStatus {
Expand Down
1 change: 0 additions & 1 deletion apps/spa/src/app/modules/+chat/shared/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { OpenAiFile, GetThreadResponseDto } from '@boldare/openai-assistant';
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);
Expand Down
19 changes: 12 additions & 7 deletions apps/spa/src/app/modules/+chat/shared/thread.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject, catchError, 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,11 +50,9 @@ export class ThreadService {
}

getThread(id: string): Observable<GetThreadResponseDto> {
return this.threadClientService
.getThread(id)
.pipe(
take(1),
catchError(() => this.start()),
);
return this.threadClientService.getThread(id).pipe(
take(1),
catchError(() => this.start()),
);
}
}
4 changes: 2 additions & 2 deletions libs/openai-assistant/src/lib/agent/agent.mock.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AssistantCreateParams } from 'openai/resources/beta';
import { FunctionTool } from 'openai/resources/beta';

export const agentNameMock = 'agent-name';

export const agentMock = async () => 'agent-result';

export const definitionMock: AssistantCreateParams.AssistantToolsFunction = {
export const definitionMock: FunctionTool = {
type: 'function',
function: { name: agentNameMock },
};
5 changes: 1 addition & 4 deletions libs/openai-assistant/src/lib/agent/agent.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ export class AgentService {
public agents: Agents = {};
public tools: FunctionTool[] = [];

add(
definition: FunctionTool,
fn: Agent,
): void {
add(definition: FunctionTool, fn: Agent): void {
this.tools.push(definition);
this.agents[definition.function.name] = fn;
}
Expand Down
2 changes: 1 addition & 1 deletion libs/openai-assistant/src/lib/chat/chat.gateway.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('ChatGateway', () => {

await chatGateway.listenForMessages(request, {} as Socket);

expect(chatService.call).toHaveBeenCalledWith(request);
expect(chatService.call).toHaveBeenCalled();
});
});

Expand Down
Loading

0 comments on commit 399a1d2

Please sign in to comment.