Skip to content

Commit

Permalink
lint and test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcampagnolitg committed Sep 18, 2024
1 parent 109afaa commit 2a6d9f8
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 46 deletions.
2 changes: 1 addition & 1 deletion frontend/src/app/@shared/services/llm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface LLM {
}

@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class LlmService {
private apiUrl = `${environment.serverUrl}/api/llms`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
</mat-select>
</mat-form-field>

<button mat-fab color="primary" (click)="submit()" [disabled]="!chatForm.get('message')?.value || isSending || !chatForm.get('selectedLlm')?.value">
<button
mat-fab
color="primary"
(click)="submit()"
[disabled]="!chatForm.get('message')?.value || isSending || !chatForm.get('selectedLlm')?.value"
>
<mat-icon *ngIf="!isSending">send</mat-icon>
<mat-spinner *ngIf="isSending" diameter="24"></mat-spinner>
</button>
Expand Down
22 changes: 10 additions & 12 deletions frontend/src/app/chat/chat-controls/chat-controls.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,30 @@ export class ChatControlsComponent implements OnInit {
isSending: boolean = false;
llms: any[] = [];

constructor(
private chatService: ApiChatService,
private llmService: LlmService,
private fb: FormBuilder
) {
constructor(private chatService: ApiChatService, private llmService: LlmService, private fb: FormBuilder) {
this.chatForm = this.fb.group({
message: [''],
selectedLlm: ['']
selectedLlm: [''],
});
}

ngOnInit() {
this.scrollBottom();
this.fetchLlms();

this.chatForm.get('message')?.valueChanges
.pipe(
this.chatForm
.get('message')
?.valueChanges.pipe(
filter((data: string) => data !== ''),
throttleTime(1400)
)
.subscribe(() => {
// Implement typing indicator if needed
});

this.chatForm.get('message')?.valueChanges
.pipe(
this.chatForm
.get('message')
?.valueChanges.pipe(
filter((data: string) => data !== ''),
debounceTime(1500)
)
Expand All @@ -63,7 +61,7 @@ export class ChatControlsComponent implements OnInit {
error: (error) => {
console.error('Error fetching LLMs:', error);
// Consider showing a user-friendly error message here
}
},
});
}

Expand Down Expand Up @@ -94,7 +92,7 @@ export class ChatControlsComponent implements OnInit {
console.error('Error sending message:', err);
this.isSending = false;
alert('Failed to send message. Please try again.');
}
},
});
}

Expand Down
9 changes: 2 additions & 7 deletions frontend/src/app/chat/chat/chat.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,11 @@ export class ChatComponent implements OnInit, OnDestroy {
error: (error) => {
console.error('Error loading chat:', error);
// Handle error (e.g., show error message to user)
}
},
});
}

this.scrollEvent$
.pipe(
debounceTime(200),
takeUntil(this.destroy$)
)
.subscribe(() => this.checkScrollPosition());
this.scrollEvent$.pipe(debounceTime(200), takeUntil(this.destroy$)).subscribe(() => this.checkScrollPosition());
}

ngOnDestroy() {
Expand Down
1 change: 0 additions & 1 deletion frontend/src/app/chat/model/chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export interface LlmMessage {
role: 'system' | 'user' | 'assistant';
text: string;
Expand Down
1 change: 0 additions & 1 deletion frontend/src/app/chat/services/api/api-chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ export class ApiChatService {
sendMessage(chatId: string, content: string, llmId: string): Observable<string> {
return this.http.post<string>(`/chat/${chatId}/send`, { text: content, llmId });
}

}
6 changes: 3 additions & 3 deletions frontend/src/app/runAgent/runAgent.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ export class RunAgentComponent implements OnInit {
(this.runAgentForm as FormGroup).addControl('function' + index, new FormControl(false));
});
});

this.llmService.getLlms().subscribe({
next: (llms) => {
this.llms = llms;
},
error: (error) => {
console.error('Error fetching LLMs:', error);
this.snackBar.open('Failed to load LLMs', 'Close', { duration: 3000 });
}
},
});

this.loadUserProfile();
}

Expand Down
24 changes: 12 additions & 12 deletions frontend/src/app/shared/services/llm.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { HttpClientTestingModule, HttpTestingController } from '@angular/common/
import { LlmService, LLM } from './llm.service';
import { environment } from '@env/environment';

const LLM_LIST_API_URL = `${environment.serverUrl}/llms/list`;

describe('LlmService', () => {
let service: LlmService;
let httpMock: HttpTestingController;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [LlmService]
providers: [LlmService],
});
service = TestBed.inject(LlmService);
httpMock = TestBed.inject(HttpTestingController);
Expand All @@ -27,42 +29,40 @@ describe('LlmService', () => {
it('should fetch LLMs from the server', () => {
const mockLlms: LLM[] = [
{ id: 'llm1', name: 'LLM 1', isConfigured: true },
{ id: 'llm2', name: 'LLM 2', isConfigured: false }
{ id: 'llm2', name: 'LLM 2', isConfigured: false },
];

service.getLlms().subscribe(llms => {
service.getLlms().subscribe((llms) => {
expect(llms).toEqual(mockLlms);
});

const req = httpMock.expectOne(`${environment.serverUrl}/api/llms/list`);
const req = httpMock.expectOne(LLM_LIST_API_URL);
expect(req.request.method).toBe('GET');
req.flush({ data: mockLlms });
});

it('should cache LLMs after the first request', () => {
const mockLlms: LLM[] = [
{ id: 'llm1', name: 'LLM 1', isConfigured: true }
];
const mockLlms: LLM[] = [{ id: 'llm1', name: 'LLM 1', isConfigured: true }];

service.getLlms().subscribe();
httpMock.expectOne(`${environment.serverUrl}/api/llms/list`).flush({ data: mockLlms });
httpMock.expectOne(LLM_LIST_API_URL).flush({ data: mockLlms });

service.getLlms().subscribe(llms => {
service.getLlms().subscribe((llms) => {
expect(llms).toEqual(mockLlms);
});

httpMock.expectNone(`${environment.serverUrl}/api/llms/list`);
httpMock.expectNone(LLM_LIST_API_URL);
});

it('should handle errors when fetching LLMs', () => {
service.getLlms().subscribe({
next: () => fail('should have failed with the 404 error'),
error: (error) => {
expect(error.message).toContain('Error Code: 404');
}
},
});

const req = httpMock.expectOne(`${environment.serverUrl}/api/llms/list`);
const req = httpMock.expectOne(LLM_LIST_API_URL);
req.flush('Not Found', { status: 404, statusText: 'Not Found' });
});
});
15 changes: 7 additions & 8 deletions frontend/src/app/shared/services/llm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface LLM {
}

@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class LlmService {
private llmsSubject = new BehaviorSubject<LLM[]>([]);
Expand All @@ -22,7 +22,7 @@ export class LlmService {
getLlms(): Observable<LLM[]> {
if (!this.llmsLoaded) {
return this.fetchLlms().pipe(
tap(llms => {
tap((llms) => {
this.llmsSubject.next(llms);
this.llmsLoaded = true;
}),
Expand All @@ -33,12 +33,11 @@ export class LlmService {
}

private fetchLlms(): Observable<LLM[]> {
return this.http.get<{ data: LLM[] }>(`${environment.serverUrl}/llms/list`)
.pipe(
map(response => response.data),
retry(3),
catchError(this.handleError)
);
return this.http.get<{ data: LLM[] }>(`${environment.serverUrl}/llms/list`).pipe(
map((response) => response.data),
retry(3),
catchError(this.handleError)
);
}

private handleError(error: HttpErrorResponse) {
Expand Down

0 comments on commit 2a6d9f8

Please sign in to comment.