Fetch prompts from scribePromptUrl
First, we try to fetch prompts from scribePromptUrl. At fallback, we use local prompts.
This commit is contained in:
@@ -4,6 +4,7 @@ export 'scribe/ai/data/datasource/ai_datasource.dart';
|
||||
export 'scribe/ai/data/repository/ai_repository_impl.dart';
|
||||
export 'scribe/ai/domain/model/ai_response.dart';
|
||||
export 'scribe/ai/domain/repository/ai_scribe_repository.dart';
|
||||
export 'scribe/ai/domain/service/prompt_service.dart';
|
||||
export 'scribe/ai/domain/state/generate_ai_text_state.dart';
|
||||
export 'scribe/ai/domain/usecases/generate_ai_text_interactor.dart';
|
||||
export 'scribe/ai/localizations/scribe_localizations.dart';
|
||||
|
||||
@@ -1,22 +1,85 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
import 'package:core/data/network/dio_client.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:scribe/scribe/ai/data/model/ai_message.dart';
|
||||
import 'package:scribe/scribe/ai/domain/model/prompt_data.dart';
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
|
||||
class PromptService {
|
||||
static final PromptService _instance = PromptService._internal();
|
||||
static PromptService? _instance;
|
||||
final DioClient _dioClient;
|
||||
|
||||
factory PromptService() => _instance;
|
||||
factory PromptService({DioClient? dioClient}) {
|
||||
_instance ??= PromptService._internal(dioClient: dioClient);
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
PromptService._internal();
|
||||
PromptService._internal({DioClient? dioClient})
|
||||
: _dioClient = dioClient ?? DioClient(Dio(BaseOptions(
|
||||
connectTimeout: const Duration(seconds: 10),
|
||||
receiveTimeout: const Duration(seconds: 10),
|
||||
)));
|
||||
|
||||
PromptData? _promptData;
|
||||
String? _promptUrl;
|
||||
|
||||
void setPromptUrl(String? url) {
|
||||
_promptUrl = url;
|
||||
}
|
||||
|
||||
String? get promptUrl => _promptUrl;
|
||||
|
||||
Future<PromptData> loadPrompts() async {
|
||||
// App in memory cache
|
||||
if (_promptData != null) {
|
||||
return _promptData!;
|
||||
}
|
||||
|
||||
// First try: remote prompts
|
||||
if (_promptUrl != null) {
|
||||
try {
|
||||
_promptData = await _fetchPromptsFromUrl(_promptUrl!);
|
||||
return _promptData!;
|
||||
} catch (e) {
|
||||
log('PromptService::loadPrompts: failed to fetch from remote URL: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: local prompts
|
||||
return _loadPromptsFromAssets();
|
||||
}
|
||||
|
||||
Future<PromptData> _fetchPromptsFromUrl(String url) async {
|
||||
log('PromptService::_fetchPromptsFromUrl: Fetching from $url');
|
||||
|
||||
try {
|
||||
final response = await _dioClient.get(url);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final jsonData = response.data;
|
||||
Map<String, dynamic> promptsMap;
|
||||
|
||||
if (jsonData is String) {
|
||||
promptsMap = jsonDecode(jsonData) as Map<String, dynamic>;
|
||||
} else if (jsonData is Map<String, dynamic>) {
|
||||
promptsMap = jsonData;
|
||||
} else {
|
||||
throw Exception('Failed to fetch prompts: invalid response format');
|
||||
}
|
||||
|
||||
log('PromptService::_fetchPromptsFromUrl: Successfully fetched prompts');
|
||||
return PromptData.fromJson(promptsMap);
|
||||
} else {
|
||||
throw Exception('Failed to fetch prompts: unexpected status code ${response.statusCode}');
|
||||
}
|
||||
} catch (e) {
|
||||
log('PromptService::_fetchPromptsFromUrl: Exception: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<PromptData> _loadPromptsFromAssets() async {
|
||||
try {
|
||||
final jsonString = await rootBundle.loadString('packages/scribe/assets/prompts.json');
|
||||
final jsonData = jsonDecode(jsonString) as Map<String, dynamic>;
|
||||
|
||||
Reference in New Issue
Block a user