Skip to content

Commit a797f98

Browse files
committed
update ai logic
1 parent d9b499a commit a797f98

7 files changed

Lines changed: 44 additions & 12 deletions

File tree

src/app/service/backend-api/post/post-api.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class PostApiService {
5959
this.userService.update('last_backup', this.configService.getLocalTime());
6060
this.userService.update('has_music_url_access', res.has_music_url_access);
6161
this.userService.update('has_ai_access', res.has_ai_access);
62+
this.userService.update('ai_key', res.ai_key);
6263
},
6364
error: err => {
6465
console.error('Error posting user data', err);

src/app/service/config/config.service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { environment } from '../../../environments/environments';
3+
import { UserService } from '../localStorage/user.service';
34

45
/**
56
* Service responsible for providing configuration values across the application,
@@ -44,8 +45,9 @@ export class ConfigService {
4445

4546
/**
4647
* Creates an instance of ConfigService.
48+
* @param userService Service for managing user preferences
4749
*/
48-
constructor() { }
50+
constructor(private userService: UserService) { }
4951

5052
/**
5153
* Returns the appropriate API base URL depending on the environment.
@@ -116,4 +118,11 @@ export class ConfigService {
116118
console.log(localISOString); // e.g. "2025-10-11T13:35:58.942"
117119
return localISOString;
118120
}
121+
122+
/**
123+
* Returns the Gemini Api Key
124+
*/
125+
getGeminiApiKey(): string | null {
126+
return this.userService.getValue<string>('ai_key');
127+
}
119128
}

src/app/service/gemini-api/gemini-api.service.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Injectable } from '@angular/core';
22
import { HttpClient, HttpHeaders } from '@angular/common/http';
3-
import { environment } from '../../../environments/environments';
43
import { firstValueFrom } from 'rxjs';
54
import { GlobalLoaderService } from '../global-loader/global-loader.service';
65
import { ExpenseService, Expense } from '../localStorage/expense.service';
@@ -18,10 +17,16 @@ import { ConfigService } from '../config/config.service';
1817
providedIn: 'root'
1918
})
2019
export class GeminiApiService {
20+
2121
/**
22-
* API URL for the Gemini model including the environment API key.
22+
* API URL for the Gemini model.
2323
*/
24-
private apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${environment.geminiApiKey}`;
24+
private geminiApiUrl = '';
25+
26+
/**
27+
* API key for accessing the Gemini AI API.
28+
*/
29+
private geminiAPIKey: string | null = null;
2530

2631
/**
2732
* Creates an instance of GeminiApiService.
@@ -36,7 +41,10 @@ export class GeminiApiService {
3641
private globalLoaderService: GlobalLoaderService,
3742
private expenseService: ExpenseService,
3843
private configService: ConfigService
39-
) {}
44+
) {
45+
this.geminiAPIKey = this.configService.getGeminiApiKey() || '';
46+
this.geminiApiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${this.geminiAPIKey}`;
47+
}
4048

4149
/**
4250
* Sends a prompt to the Gemini API and returns the generated response.
@@ -57,7 +65,7 @@ export class GeminiApiService {
5765
};
5866

5967
try {
60-
const res: any = await firstValueFrom(this.http.post(this.apiUrl, body, { headers }));
68+
const res: any = await firstValueFrom(this.http.post(this.geminiApiUrl, body, { headers }));
6169
const parts = res?.candidates?.[0]?.content?.parts;
6270
this.globalLoaderService.hide();
6371
return parts?.map((p: any) => p.text).join('\n\n') || 'No response';

src/app/service/localStorage/storage.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export class StorageService {
7272
has_ai_access: false,
7373
rose_amount: 1000,
7474
emerald_amount: 500,
75+
ai_key: null
7576
};
7677

7778
/** Schema for budget */

src/app/service/localStorage/user.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export interface User {
4141
/** Stores the amount associated with the Emerald heatmap color */
4242
emerald_amount: number;
4343

44+
/** AI key */
45+
ai_key: string
46+
4447
}
4548

4649
/**

src/app/service/saavan-api/saavan.service.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Injectable } from '@angular/core';
22
import { HttpClient, HttpHeaders } from '@angular/common/http';
33
import { GlobalLoaderService } from '../../service/global-loader/global-loader.service';
44
import { finalize } from 'rxjs/operators';
5-
import { environment } from '../../../environments/environments';
65
import { firstValueFrom } from 'rxjs';
6+
import { ConfigService } from '../config/config.service';
77

88
/**
99
* ChatMessage interface
@@ -39,20 +39,30 @@ export class SaavnService {
3939
private savvanApiUrl = 'https://saavn.dev/api/search/songs';
4040

4141
/**
42-
* Gemini API URL with authentication key from environment.
42+
* Gemini API URL.
4343
*/
44-
private geminiApiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${environment.geminiApiKey}`;
44+
private geminiApiUrl = '';
45+
46+
/**
47+
* API key for accessing the Gemini AI API.
48+
*/
49+
private geminiAPIKey: string | null = null;
4550

4651
/**
4752
* Creates an instance of SaavnService.
4853
*
4954
* @param http Angular HttpClient for API calls.
5055
* @param globalLoaderService Global loader service to show/hide loading UI.
56+
* @param configService Service to fetch configuration values
5157
*/
5258
constructor(
5359
private http: HttpClient,
54-
private globalLoaderService: GlobalLoaderService
55-
) { }
60+
private globalLoaderService: GlobalLoaderService,
61+
private configService: ConfigService
62+
) {
63+
this.geminiAPIKey = this.configService.getGeminiApiKey() || '';
64+
this.geminiApiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${this.geminiAPIKey}`;
65+
}
5666

5767
/**
5868
* Searches for songs using the Saavn API.

src/environments/environments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const environment = {
1919
* API key for accessing the Gemini AI API.
2020
* Replace or secure this key before deploying to production.
2121
*/
22-
geminiApiKey: 'AIzaSyDTDiOZvUYymqHU11EqbnCzk66mexWt2y4',
22+
geminiApiKey: '',
2323

2424
/**
2525
* Current environment type.

0 commit comments

Comments
 (0)