Use own Dio instance for prompt service
This commit is contained in:
@@ -72,6 +72,7 @@ class NetworkBindings extends Bindings {
|
|||||||
Get.put(AppAuthWebPlugin());
|
Get.put(AppAuthWebPlugin());
|
||||||
Get.put(OIDCHttpClient(Get.find<DioClient>()));
|
Get.put(OIDCHttpClient(Get.find<DioClient>()));
|
||||||
Get.put(AuthenticationClientBase());
|
Get.put(AuthenticationClientBase());
|
||||||
|
Get.put(Dio(), tag: 'prompt');
|
||||||
}
|
}
|
||||||
|
|
||||||
void _bindingSharing() {
|
void _bindingSharing() {
|
||||||
@@ -147,6 +148,6 @@ class NetworkBindings extends Bindings {
|
|||||||
|
|
||||||
void _bindingServices() {
|
void _bindingServices() {
|
||||||
Get.put(DnsLookupManager());
|
Get.put(DnsLookupManager());
|
||||||
Get.put(PromptService(Get.find<DioClient>()));
|
Get.put(PromptService(Get.find<Dio>(tag: 'prompt')));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,21 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:core/data/network/dio_client.dart';
|
|
||||||
import 'package:core/utils/app_logger.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/data/model/ai_message.dart';
|
||||||
import 'package:scribe/scribe/ai/domain/model/prompt_data.dart';
|
import 'package:scribe/scribe/ai/domain/model/prompt_data.dart';
|
||||||
import 'package:flutter/services.dart' show rootBundle;
|
import 'package:flutter/services.dart' show rootBundle;
|
||||||
|
|
||||||
class PromptService {
|
class PromptService {
|
||||||
static const String _defaultAssetPath = 'packages/scribe/assets/prompts.json';
|
static const String _defaultAssetPath = 'packages/scribe/assets/prompts.json';
|
||||||
|
|
||||||
final DioClient _dioClient;
|
final Dio _dio;
|
||||||
|
|
||||||
String? _promptUrl;
|
String? _promptUrl;
|
||||||
PromptData? _promptData;
|
PromptData? _promptData;
|
||||||
Future<PromptData>? _loadingFuture;
|
Future<PromptData>? _loadingFuture;
|
||||||
|
|
||||||
PromptService(this._dioClient);
|
PromptService(this._dio);
|
||||||
|
|
||||||
void setPromptUrl(String? url) {
|
void setPromptUrl(String? url) {
|
||||||
if (_promptUrl == url) return;
|
if (_promptUrl == url) return;
|
||||||
@@ -62,10 +62,11 @@ class PromptService {
|
|||||||
log('PromptService::_fetchPromptsFromUrl: Fetching from $url');
|
log('PromptService::_fetchPromptsFromUrl: Fetching from $url');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final data = await _dioClient.get(url);
|
final response = await _dio.get(url);
|
||||||
|
final data = response.data;
|
||||||
final promptsMap = data is String
|
|
||||||
? jsonDecode(data) as Map<String, dynamic>
|
final promptsMap = data is String
|
||||||
|
? jsonDecode(data) as Map<String, dynamic>
|
||||||
: data as Map<String, dynamic>;
|
: data as Map<String, dynamic>;
|
||||||
|
|
||||||
return PromptData.fromJson(promptsMap);
|
return PromptData.fromJson(promptsMap);
|
||||||
|
|||||||
@@ -1,54 +1,38 @@
|
|||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:core/data/network/dio_client.dart';
|
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:scribe/scribe/ai/data/service/prompt_service.dart';
|
import 'package:scribe/scribe/ai/data/service/prompt_service.dart';
|
||||||
import 'package:scribe/scribe/ai/domain/model/prompt_data.dart';
|
import 'package:scribe/scribe/ai/domain/model/prompt_data.dart';
|
||||||
import 'package:scribe/scribe/ai/data/model/ai_message.dart';
|
import 'package:scribe/scribe/ai/data/model/ai_message.dart';
|
||||||
|
|
||||||
class TestDioClient implements DioClient {
|
class _ThrowingAdapter implements HttpClientAdapter {
|
||||||
@override
|
@override
|
||||||
Future<dynamic> get(String path, {Map<String, dynamic>? queryParameters, Options? options, CancelToken? cancelToken, ProgressCallback? onReceiveProgress}) {
|
Future<ResponseBody> fetch(
|
||||||
|
RequestOptions options,
|
||||||
|
Stream<Uint8List>? requestStream,
|
||||||
|
Future<void>? cancelFuture,
|
||||||
|
) async {
|
||||||
throw Exception('Not found');
|
throw Exception('Not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<dynamic> post(String path, {data, Map<String, dynamic>? queryParameters, Options? options, CancelToken? cancelToken, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, bool useJMAPHeader = true}) {
|
void close({bool force = false}) {}
|
||||||
throw UnsupportedError('post not implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<dynamic> delete(String path, {data, Map<String, dynamic>? queryParameters, Options? options, CancelToken? cancelToken}) {
|
|
||||||
throw UnsupportedError('delete not implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<dynamic> put(String path, {data, Map<String, dynamic>? queryParameters, Options? options, CancelToken? cancelToken, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress}) {
|
|
||||||
throw UnsupportedError('put not implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Map<String, dynamic> getHeaders() {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Dio _throwingDio() => Dio()..httpClientAdapter = _ThrowingAdapter();
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
setUpAll(() {
|
setUpAll(() {
|
||||||
TestWidgetsFlutterBinding.ensureInitialized();
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
});
|
});
|
||||||
|
|
||||||
group('PromptService', () {
|
group('PromptService', () {
|
||||||
late TestDioClient testDioClient;
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
testDioClient = TestDioClient();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('buildPromptByName should build prompt with input text', () async {
|
test('buildPromptByName should build prompt with input text', () async {
|
||||||
// Arrange
|
// Arrange
|
||||||
final service = PromptService(testDioClient);
|
final service = PromptService(_throwingDio());
|
||||||
|
|
||||||
// Act - this will use the real prompts.json file since the test client throws
|
// Act - this will use the real prompts.json file since the adapter throws
|
||||||
final messages = await service.buildPromptByName('change-tone-casual', 'Hello, how are you?');
|
final messages = await service.buildPromptByName('change-tone-casual', 'Hello, how are you?');
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
@@ -60,7 +44,7 @@ void main() {
|
|||||||
|
|
||||||
test('buildPromptByName should build prompt with input text and task', () async {
|
test('buildPromptByName should build prompt with input text and task', () async {
|
||||||
// Arrange
|
// Arrange
|
||||||
final service = PromptService(testDioClient);
|
final service = PromptService(_throwingDio());
|
||||||
|
|
||||||
// Act - this will use the real prompts.json file since the test client throws
|
// Act - this will use the real prompts.json file since the test client throws
|
||||||
final messages = await service.buildPromptByName('custom-prompt-mail', 'Hello, how are you?', task: 'Make it more casual');
|
final messages = await service.buildPromptByName('custom-prompt-mail', 'Hello, how are you?', task: 'Make it more casual');
|
||||||
|
|||||||
Reference in New Issue
Block a user