Skip to content

Commit

Permalink
Reload llm cache on profile save to update configured llms
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcampagnolitg committed Sep 19, 2024
1 parent c955b88 commit 2598664
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
20 changes: 17 additions & 3 deletions frontend/src/app/profile/profile.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FormGroup, FormControl } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { environment } from '@env/environment';
import { MatSnackBar } from '@angular/material/snack-bar';
import { LlmService } from '../shared/services/llm.service';

@Component({
selector: 'app-profile',
Expand All @@ -11,7 +12,7 @@ import { MatSnackBar } from '@angular/material/snack-bar';
})
export class ProfileComponent implements OnInit {
profileForm: FormGroup;
constructor(private http: HttpClient, private snackBar: MatSnackBar) {
constructor(private http: HttpClient, private snackBar: MatSnackBar, private llmService: LlmService) {
this.profileForm = this.createProfileForm();
}

Expand Down Expand Up @@ -66,6 +67,8 @@ export class ProfileComponent implements OnInit {
this.http.post(updateUrl, { user: formValue }).subscribe({
next: () => {
this.snackBar.open('Profile updated', 'Close', { duration: 3000 });
this.llmService.clearCache();
this.loadLlmList();
},
error: (error) => {
this.snackBar.open('Failed to save profile.', 'Close', { duration: 3000 });
Expand All @@ -74,9 +77,8 @@ export class ProfileComponent implements OnInit {
});
}

// ... (rest of the component methods)
private loadUserProfile(): void {
console.log('Loading profile profile...');
console.log('Loading user profile...');
const profileUrl = `${environment.serverUrl}/profile/view`;
this.http.get(profileUrl).subscribe(
(response: any) => {
Expand All @@ -89,4 +91,16 @@ export class ProfileComponent implements OnInit {
}
);
}

private loadLlmList(): void {
this.llmService.getLlms().subscribe({
next: (llms) => {
console.log('LLM list loaded:', llms);
},
error: (error) => {
console.error('Failed to load LLM list:', error);
this.snackBar.open('Failed to load LLM list', 'Close', { duration: 3000 });
},
});
}
}
30 changes: 14 additions & 16 deletions frontend/src/app/shared/services/llm.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,31 @@ describe('LlmService', () => {
expect(llms).toEqual(mockLlms);
});

const req = httpMock.expectOne(LLM_LIST_API_URL);
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 }];
it('should cache LLMs per user', () => {
const mockLlms1: LLM[] = [{ id: 'llm1', name: 'LLM 1', isConfigured: true }];

service.getLlms().subscribe();
httpMock.expectOne(LLM_LIST_API_URL).flush({ data: mockLlms });
httpMock.expectOne(`${LLM_LIST_API_URL}`).flush({ data: mockLlms1 });

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

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');
},
});
it('should clear the cache', () => {
const mockLlms: LLM[] = [{ id: 'llm1', name: 'LLM 1', isConfigured: true }];

const req = httpMock.expectOne(LLM_LIST_API_URL);
req.flush('Not Found', { status: 404, statusText: 'Not Found' });
service.getLlms().subscribe();
httpMock.expectOne(`${LLM_LIST_API_URL}`).flush({ data: mockLlms });

service.clearCache();

service.getLlms().subscribe();
httpMock.expectOne(`${LLM_LIST_API_URL}`);
});
});
7 changes: 7 additions & 0 deletions frontend/src/app/shared/services/llm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable, BehaviorSubject, throwError } from 'rxjs';
import { tap, shareReplay, map, catchError, retry } from 'rxjs/operators';
import { environment } from '@env/environment';
import { CredentialsService } from '@app/auth';

export interface LLM {
id: string;
Expand Down Expand Up @@ -40,6 +41,12 @@ export class LlmService {
);
}

clearCache() {
this.llmsLoaded = false;
this.llmsSubject.next([]);
this.getLlms();
}

private handleError(error: HttpErrorResponse) {
let errorMessage = 'An error occurred';
if (error.error instanceof ErrorEvent) {
Expand Down
1 change: 1 addition & 0 deletions src/routes/llms/llm-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AppFastifyInstance } from '../../app';
const basePath = '/api/llms';

export async function llmRoutes(fastify: AppFastifyInstance) {
// Returns the LLMs which are configured for the current user
fastify.get(`${basePath}/list`, async (req, reply) => {
const configuredLLMs = LLM_TYPES.map((llm) => getLLM(llm.id))
.filter((llm) => llm.isConfigured())
Expand Down
2 changes: 1 addition & 1 deletion src/swe/lang/nodejs/typescriptTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class TypescriptTools implements LanguageTools {
if (packageJson.devDependencies) {
info += '<development>\n';
for (const [pkg, version] of Object.entries(packageJson.devDependencies)) {
info += `${pkg}: ${version}\n`;
if (!pkg.startsWith('@types/')) info += `${pkg}: ${version}\n`;
}
info += '</development>\n';
}
Expand Down

0 comments on commit 2598664

Please sign in to comment.