ciyidogan commited on
Commit
fb0f303
·
verified ·
1 Parent(s): 69541a8

Update flare-ui/src/app/services/environment.service.ts

Browse files
flare-ui/src/app/services/environment.service.ts CHANGED
@@ -2,8 +2,8 @@
2
  // Path: /flare-ui/src/app/services/environment.service.ts
3
 
4
  import { Injectable } from '@angular/core';
5
- import { BehaviorSubject, Observable, throwError } from 'rxjs';
6
- import { Environment } from './api.service';
7
 
8
  export interface EnvironmentError {
9
  type: 'validation' | 'update' | 'unknown';
@@ -32,26 +32,22 @@ export class EnvironmentService {
32
  sttEnabled$ = this.sttEnabledSource.asObservable();
33
 
34
  constructor() {
35
- // Load saved preferences
36
  this.loadPreferences();
37
  }
38
 
39
  private loadPreferences(): void {
40
  try {
41
- // Load TTS preference
42
  const savedTTS = localStorage.getItem(this.TTS_KEY);
43
  if (savedTTS !== null) {
44
  this.ttsEnabledSource.next(savedTTS === 'true');
45
  }
46
 
47
- // Load STT preference
48
  const savedSTT = localStorage.getItem(this.STT_KEY);
49
  if (savedSTT !== null) {
50
  this.sttEnabledSource.next(savedSTT === 'true');
51
  }
52
  } catch (error) {
53
  console.error('Error loading preferences:', error);
54
- // Use defaults on error
55
  this.ttsEnabledSource.next(false);
56
  this.sttEnabledSource.next(false);
57
  }
@@ -65,7 +61,6 @@ export class EnvironmentService {
65
 
66
  this.ttsEnabledSource.next(enabled);
67
 
68
- // Save preference
69
  try {
70
  localStorage.setItem(this.TTS_KEY, enabled.toString());
71
  } catch (error) {
@@ -86,7 +81,6 @@ export class EnvironmentService {
86
 
87
  this.sttEnabledSource.next(enabled);
88
 
89
- // Save preference
90
  try {
91
  localStorage.setItem(this.STT_KEY, enabled.toString());
92
  } catch (error) {
@@ -109,30 +103,24 @@ export class EnvironmentService {
109
 
110
  updateEnvironment(env: Environment | null): void {
111
  try {
 
 
 
112
  if (env) {
113
- // Validate environment object
114
- this.validateEnvironment(env);
115
-
116
- // Check for changes
117
- const currentEnv = this.environmentSubject.value;
118
- const hasChanged = !currentEnv ||
119
- currentEnv.id !== env.id ||
120
- currentEnv.work_mode !== env.work_mode ||
121
- currentEnv.last_update_date !== env.last_update_date;
122
 
123
- if (hasChanged) {
124
- console.log('Environment updated:', {
125
- id: env.id,
126
- name: env.name,
127
- work_mode: env.work_mode,
128
- version: env.active_version_id
129
- });
130
  }
131
  }
132
-
133
- this.environmentSubject.next(env);
134
- this.errorSubject.next(null); // Clear any previous errors
135
-
136
  } catch (error: any) {
137
  this.handleError('update', error.message || 'Failed to update environment', error);
138
  }
@@ -142,10 +130,26 @@ export class EnvironmentService {
142
  return this.environmentSubject.value;
143
  }
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  isGPTMode(): boolean {
146
  try {
147
  const env = this.environmentSubject.value;
148
- return env?.work_mode?.toLowerCase()?.startsWith('gpt4o') || false;
 
149
  } catch (error) {
150
  console.error('Error checking GPT mode:', error);
151
  return false;
@@ -154,17 +158,17 @@ export class EnvironmentService {
154
 
155
  getWorkMode(): string | null {
156
  const env = this.environmentSubject.value;
157
- return env?.work_mode || null;
158
  }
159
 
160
- isEnvironmentActive(): boolean {
161
  const env = this.environmentSubject.value;
162
- return env?.enabled === true;
163
  }
164
 
165
- hasActiveVersion(): boolean {
166
  const env = this.environmentSubject.value;
167
- return !!env?.active_version_id;
168
  }
169
 
170
  // Check if environment supports a specific feature
@@ -174,18 +178,26 @@ export class EnvironmentService {
174
 
175
  switch (feature) {
176
  case 'tts':
177
- return this.isTTSEnabled() && env.enabled;
178
  case 'stt':
179
- return this.isSTTEnabled() && env.enabled;
180
  case 'realtime':
181
- return this.isGPTMode() && env.enabled;
182
  case 'streaming':
183
- return env.enabled;
184
  default:
185
  return false;
186
  }
187
  }
188
 
 
 
 
 
 
 
 
 
189
  // Reset all settings
190
  reset(): void {
191
  try {
@@ -194,7 +206,6 @@ export class EnvironmentService {
194
  this.setSTTEnabled(false);
195
  this.errorSubject.next(null);
196
 
197
- // Clear saved preferences
198
  try {
199
  localStorage.removeItem(this.TTS_KEY);
200
  localStorage.removeItem(this.STT_KEY);
@@ -210,8 +221,9 @@ export class EnvironmentService {
210
 
211
  // Get configuration summary
212
  getConfigSummary(): {
213
- environment: string | null;
214
- workMode: string | null;
 
215
  ttsEnabled: boolean;
216
  sttEnabled: boolean;
217
  features: string[];
@@ -225,37 +237,15 @@ export class EnvironmentService {
225
  if (this.supportsFeature('streaming')) features.push('Streaming');
226
 
227
  return {
228
- environment: env?.name || null,
229
- workMode: env?.work_mode || null,
 
230
  ttsEnabled: this.isTTSEnabled(),
231
  sttEnabled: this.isSTTEnabled(),
232
  features
233
  };
234
  }
235
 
236
- private validateEnvironment(env: Environment): void {
237
- if (!env.id) {
238
- throw new Error('Environment must have an ID');
239
- }
240
-
241
- if (!env.name || env.name.trim().length === 0) {
242
- throw new Error('Environment must have a name');
243
- }
244
-
245
- if (!env.project_id) {
246
- throw new Error('Environment must be associated with a project');
247
- }
248
-
249
- if (env.work_mode && !this.isValidWorkMode(env.work_mode)) {
250
- throw new Error(`Invalid work mode: ${env.work_mode}`);
251
- }
252
- }
253
-
254
- private isValidWorkMode(workMode: string): boolean {
255
- const validModes = ['stream', 'batch', 'gpt4o', 'gpt4o-mini', 'gpt4o-realtime'];
256
- return validModes.includes(workMode.toLowerCase());
257
- }
258
-
259
  private handleError(type: EnvironmentError['type'], message: string, details?: any): void {
260
  const error: EnvironmentError = {
261
  type,
@@ -271,7 +261,14 @@ export class EnvironmentService {
271
  isReady(): Observable<boolean> {
272
  return new Observable(subscriber => {
273
  const sub = this.environment$.subscribe(env => {
274
- subscriber.next(!!env && env.enabled && !!env.active_version_id);
 
 
 
 
 
 
 
275
  });
276
 
277
  return () => sub.unsubscribe();
 
2
  // Path: /flare-ui/src/app/services/environment.service.ts
3
 
4
  import { Injectable } from '@angular/core';
5
+ import { BehaviorSubject, Observable } from 'rxjs';
6
+ import { Environment, ProviderSettings } from './api.service';
7
 
8
  export interface EnvironmentError {
9
  type: 'validation' | 'update' | 'unknown';
 
32
  sttEnabled$ = this.sttEnabledSource.asObservable();
33
 
34
  constructor() {
 
35
  this.loadPreferences();
36
  }
37
 
38
  private loadPreferences(): void {
39
  try {
 
40
  const savedTTS = localStorage.getItem(this.TTS_KEY);
41
  if (savedTTS !== null) {
42
  this.ttsEnabledSource.next(savedTTS === 'true');
43
  }
44
 
 
45
  const savedSTT = localStorage.getItem(this.STT_KEY);
46
  if (savedSTT !== null) {
47
  this.sttEnabledSource.next(savedSTT === 'true');
48
  }
49
  } catch (error) {
50
  console.error('Error loading preferences:', error);
 
51
  this.ttsEnabledSource.next(false);
52
  this.sttEnabledSource.next(false);
53
  }
 
61
 
62
  this.ttsEnabledSource.next(enabled);
63
 
 
64
  try {
65
  localStorage.setItem(this.TTS_KEY, enabled.toString());
66
  } catch (error) {
 
81
 
82
  this.sttEnabledSource.next(enabled);
83
 
 
84
  try {
85
  localStorage.setItem(this.STT_KEY, enabled.toString());
86
  } catch (error) {
 
103
 
104
  updateEnvironment(env: Environment | null): void {
105
  try {
106
+ this.environmentSubject.next(env);
107
+ this.errorSubject.next(null);
108
+
109
  if (env) {
110
+ console.log('Environment updated:', {
111
+ llm_provider: env.llm_provider.name,
112
+ tts_provider: env.tts_provider.name,
113
+ stt_provider: env.stt_provider.name
114
+ });
 
 
 
 
115
 
116
+ // Update TTS/STT enabled states based on provider
117
+ if (env.tts_provider.name !== 'no_tts') {
118
+ this.setTTSEnabled(true);
119
+ }
120
+ if (env.stt_provider.name !== 'no_stt') {
121
+ this.setSTTEnabled(true);
 
122
  }
123
  }
 
 
 
 
124
  } catch (error: any) {
125
  this.handleError('update', error.message || 'Failed to update environment', error);
126
  }
 
130
  return this.environmentSubject.value;
131
  }
132
 
133
+ getCurrentLLMProvider(): ProviderSettings | null {
134
+ const env = this.environmentSubject.value;
135
+ return env?.llm_provider || null;
136
+ }
137
+
138
+ getCurrentTTSProvider(): ProviderSettings | null {
139
+ const env = this.environmentSubject.value;
140
+ return env?.tts_provider || null;
141
+ }
142
+
143
+ getCurrentSTTProvider(): ProviderSettings | null {
144
+ const env = this.environmentSubject.value;
145
+ return env?.stt_provider || null;
146
+ }
147
+
148
  isGPTMode(): boolean {
149
  try {
150
  const env = this.environmentSubject.value;
151
+ const llmName = env?.llm_provider?.name?.toLowerCase();
152
+ return llmName?.startsWith('gpt4o') || false;
153
  } catch (error) {
154
  console.error('Error checking GPT mode:', error);
155
  return false;
 
158
 
159
  getWorkMode(): string | null {
160
  const env = this.environmentSubject.value;
161
+ return env?.llm_provider?.name || null;
162
  }
163
 
164
+ isTTSAvailable(): boolean {
165
  const env = this.environmentSubject.value;
166
+ return env?.tts_provider?.name !== 'no_tts' && env?.tts_provider?.name !== undefined;
167
  }
168
 
169
+ isSTTAvailable(): boolean {
170
  const env = this.environmentSubject.value;
171
+ return env?.stt_provider?.name !== 'no_stt' && env?.stt_provider?.name !== undefined;
172
  }
173
 
174
  // Check if environment supports a specific feature
 
178
 
179
  switch (feature) {
180
  case 'tts':
181
+ return this.isTTSAvailable() && this.isTTSEnabled();
182
  case 'stt':
183
+ return this.isSTTAvailable() && this.isSTTEnabled();
184
  case 'realtime':
185
+ return this.isGPTMode();
186
  case 'streaming':
187
+ return true;
188
  default:
189
  return false;
190
  }
191
  }
192
 
193
+ // Get available providers
194
+ getAvailableProviders(type: 'llm' | 'tts' | 'stt'): any[] {
195
+ const env = this.environmentSubject.value;
196
+ if (!env?.providers) return [];
197
+
198
+ return env.providers.filter(p => p.type === type);
199
+ }
200
+
201
  // Reset all settings
202
  reset(): void {
203
  try {
 
206
  this.setSTTEnabled(false);
207
  this.errorSubject.next(null);
208
 
 
209
  try {
210
  localStorage.removeItem(this.TTS_KEY);
211
  localStorage.removeItem(this.STT_KEY);
 
221
 
222
  // Get configuration summary
223
  getConfigSummary(): {
224
+ llmProvider: string | null;
225
+ ttsProvider: string | null;
226
+ sttProvider: string | null;
227
  ttsEnabled: boolean;
228
  sttEnabled: boolean;
229
  features: string[];
 
237
  if (this.supportsFeature('streaming')) features.push('Streaming');
238
 
239
  return {
240
+ llmProvider: env?.llm_provider?.name || null,
241
+ ttsProvider: env?.tts_provider?.name || null,
242
+ sttProvider: env?.stt_provider?.name || null,
243
  ttsEnabled: this.isTTSEnabled(),
244
  sttEnabled: this.isSTTEnabled(),
245
  features
246
  };
247
  }
248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  private handleError(type: EnvironmentError['type'], message: string, details?: any): void {
250
  const error: EnvironmentError = {
251
  type,
 
261
  isReady(): Observable<boolean> {
262
  return new Observable(subscriber => {
263
  const sub = this.environment$.subscribe(env => {
264
+ // Environment is ready if we have all providers configured
265
+ const isReady = !!(
266
+ env &&
267
+ env.llm_provider &&
268
+ env.tts_provider &&
269
+ env.stt_provider
270
+ );
271
+ subscriber.next(isReady);
272
  });
273
 
274
  return () => sub.unsubscribe();