From 174fe2ab336135012a732402342d4aca697087e5 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Thu, 14 Nov 2024 15:45:15 -0800 Subject: [PATCH 1/4] remove an experiemntal 'gemini' call from the backend --- pkgs/dart_services/.gitignore | 3 ++ pkgs/dart_services/lib/src/common_server.dart | 49 ------------------- .../lib/src/common_server.g.dart | 5 -- pkgs/dart_services/pubspec.yaml | 1 - pkgs/dartpad_shared/lib/model.dart | 17 ------- pkgs/dartpad_shared/lib/model.g.dart | 10 ---- pkgs/dartpad_shared/lib/services.dart | 6 --- pkgs/dartpad_ui/lib/model.dart | 4 -- 8 files changed, 3 insertions(+), 92 deletions(-) diff --git a/pkgs/dart_services/.gitignore b/pkgs/dart_services/.gitignore index 2e678174e..6555e56a6 100644 --- a/pkgs/dart_services/.gitignore +++ b/pkgs/dart_services/.gitignore @@ -31,3 +31,6 @@ config.properties .vscode/ bin/server + +# Any local environment variable configuration +.env diff --git a/pkgs/dart_services/lib/src/common_server.dart b/pkgs/dart_services/lib/src/common_server.dart index 79a27a308..963ab5373 100644 --- a/pkgs/dart_services/lib/src/common_server.dart +++ b/pkgs/dart_services/lib/src/common_server.dart @@ -7,7 +7,6 @@ import 'dart:convert'; import 'dart:io'; import 'package:dartpad_shared/model.dart' as api; -import 'package:google_generative_ai/google_generative_ai.dart' as google_ai; import 'package:http/http.dart' as http; import 'package:logging/logging.dart'; import 'package:shelf/shelf.dart'; @@ -232,50 +231,6 @@ class CommonServerApi { } } - static final String? geminiApiKey = Platform.environment['GEMINI_API_KEY']; - http.Client? geminiHttpClient; - - @Route.post('$apiPrefix/_gemini') - Future gemini(Request request, String apiVersion) async { - if (apiVersion != api3) return unhandledVersion(apiVersion); - - // Read the api key from env variables (populated on the server). - final apiKey = geminiApiKey; - if (apiKey == null) { - return Response.internalServerError( - body: 'gemini key not configured on server'); - } - - // Only allow the call from dartpad.dev. - final origin = request.origin; - if (origin != 'https://dartpad.dev') { - return Response.badRequest( - body: 'Gemini calls only allowed from the DartPad front-end'); - } - - final sourceRequest = - api.SourceRequest.fromJson(await request.readAsJson()); - - geminiHttpClient ??= http.Client(); - - final model = google_ai.GenerativeModel( - model: 'models/gemini-1.5-flash-latest', - apiKey: apiKey, - httpClient: geminiHttpClient, - ); - - final result = await serialize(() async { - // call gemini - final result = await model.generateContent([ - google_ai.Content.text(sourceRequest.source), - ]); - - return api.GeminiResponse(response: result.text!); - }); - - return ok(result.toJson()); - } - Response ok(Map json) { return Response.ok( _jsonEncoder.convert(json), @@ -403,7 +358,3 @@ String _formatMessage( return message; } - -extension on Request { - String? get origin => headers['origin']; -} diff --git a/pkgs/dart_services/lib/src/common_server.g.dart b/pkgs/dart_services/lib/src/common_server.g.dart index 71da0773f..f01227bf0 100644 --- a/pkgs/dart_services/lib/src/common_server.g.dart +++ b/pkgs/dart_services/lib/src/common_server.g.dart @@ -53,10 +53,5 @@ Router _$CommonServerApiRouter(CommonServerApi service) { r'/api//openInIDX', service.openInIdx, ); - router.add( - 'POST', - r'/api//_gemini', - service.gemini, - ); return router; } diff --git a/pkgs/dart_services/pubspec.yaml b/pkgs/dart_services/pubspec.yaml index e6b598694..f3b13659f 100644 --- a/pkgs/dart_services/pubspec.yaml +++ b/pkgs/dart_services/pubspec.yaml @@ -12,7 +12,6 @@ dependencies: bazel_worker: ^1.1.1 dartpad_shared: ^0.0.0 encrypt: ^5.0.3 - google_generative_ai: ^0.4.6 http: ^1.2.1 json_annotation: ^4.9.0 logging: ^1.2.0 diff --git a/pkgs/dartpad_shared/lib/model.dart b/pkgs/dartpad_shared/lib/model.dart index 252cd6b77..d6ab5fc62 100644 --- a/pkgs/dartpad_shared/lib/model.dart +++ b/pkgs/dartpad_shared/lib/model.dart @@ -375,23 +375,6 @@ class VersionResponse { Map toJson() => _$VersionResponseToJson(this); } -@JsonSerializable() -class GeminiResponse { - final String response; - - GeminiResponse({ - required this.response, - }); - - factory GeminiResponse.fromJson(Map json) => - _$GeminiResponseFromJson(json); - - Map toJson() => _$GeminiResponseToJson(this); - - @override - String toString() => 'GeminiResponse[response=$response]'; -} - @JsonSerializable() class OpenInIdxRequest { final String code; diff --git a/pkgs/dartpad_shared/lib/model.g.dart b/pkgs/dartpad_shared/lib/model.g.dart index 507e7ac27..3be14016f 100644 --- a/pkgs/dartpad_shared/lib/model.g.dart +++ b/pkgs/dartpad_shared/lib/model.g.dart @@ -296,16 +296,6 @@ Map _$VersionResponseToJson(VersionResponse instance) => 'packages': instance.packages, }; -GeminiResponse _$GeminiResponseFromJson(Map json) => - GeminiResponse( - response: json['response'] as String, - ); - -Map _$GeminiResponseToJson(GeminiResponse instance) => - { - 'response': instance.response, - }; - OpenInIdxRequest _$OpenInIdxRequestFromJson(Map json) => OpenInIdxRequest( code: json['code'] as String, diff --git a/pkgs/dartpad_shared/lib/services.dart b/pkgs/dartpad_shared/lib/services.dart index 73047311d..091c38279 100644 --- a/pkgs/dartpad_shared/lib/services.dart +++ b/pkgs/dartpad_shared/lib/services.dart @@ -5,7 +5,6 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:meta/meta.dart'; import 'model.dart'; @@ -44,11 +43,6 @@ class ServicesClient { Future openInIdx(OpenInIdxRequest request) => _requestPost('openInIDX', request.toJson(), OpenInIdxResponse.fromJson); - /// Note: this API is experimental and could change or be removed at any time. - @experimental - Future gemini(SourceRequest request) => - _requestPost('_gemini', request.toJson(), GeminiResponse.fromJson); - void dispose() => client.close(); Future _requestGet( diff --git a/pkgs/dartpad_ui/lib/model.dart b/pkgs/dartpad_ui/lib/model.dart index eb73ae351..02875b6cf 100644 --- a/pkgs/dartpad_ui/lib/model.dart +++ b/pkgs/dartpad_ui/lib/model.dart @@ -339,10 +339,6 @@ class AppServices { return await services.document(request); } - Future gemini(SourceRequest request) async { - return await services.gemini(request); - } - Future compile(CompileRequest request) async { try { appModel.compilingBusy.value = true; From e9d94475c4eb1202c0c52d78e9588979836eb3a2 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Thu, 14 Nov 2024 16:00:58 -0800 Subject: [PATCH 2/4] update pub deps for the main channel --- .../dependencies/pub_dependencies_main.json | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/pkgs/dart_services/tool/dependencies/pub_dependencies_main.json b/pkgs/dart_services/tool/dependencies/pub_dependencies_main.json index 1c1694261..c4eb4befc 100644 --- a/pkgs/dart_services/tool/dependencies/pub_dependencies_main.json +++ b/pkgs/dart_services/tool/dependencies/pub_dependencies_main.json @@ -4,26 +4,26 @@ "animations": "2.0.11", "archive": "3.6.1", "args": "2.6.0", - "async": "2.11.0", + "async": "2.12.0", "basics": "0.10.0", "bloc": "8.1.4", - "boolean_selector": "2.1.1", + "boolean_selector": "2.1.2", "characters": "1.3.0", - "clock": "1.1.1", + "clock": "1.1.2", "collection": "1.19.0", "convert": "3.1.2", - "coverage": "1.10.0", + "coverage": "1.11.0", "creator": "0.3.3", "creator_core": "0.3.3", "cross_file": "0.3.4+2", "crypto": "3.0.6", - "csslib": "1.0.0", + "csslib": "1.0.2", "dart_earcut": "1.1.0", "dartz": "0.10.1", "english_words": "4.0.0", "equatable": "2.0.5", - "fake_async": "1.3.1", - "fast_immutable_collections": "10.2.4", + "fake_async": "1.3.2", + "fast_immutable_collections": "11.0.0", "ffi": "2.1.3", "file": "7.0.1", "flame": "1.21.0", @@ -37,15 +37,15 @@ "flutter_map": "7.0.2", "flutter_markdown": "0.7.4+1", "flutter_riverpod": "2.6.1", - "flutter_svg": "2.0.10+1", + "flutter_svg": "2.0.14", "forge2d": "0.13.1", "frontend_server_client": "4.0.0", "glob": "2.1.2", - "go_router": "14.3.0", + "go_router": "14.6.0", "google_fonts": "6.2.1", "google_generative_ai": "0.4.6", "hooks_riverpod": "2.6.1", - "html": "0.15.4", + "html": "0.15.5", "http": "1.2.2", "http_multi_server": "3.2.1", "http_parser": "4.1.1", @@ -73,9 +73,9 @@ "node_preamble": "2.0.2", "ordered_set": "6.1.1", "package_config": "2.1.0", - "path": "1.9.0", - "path_parsing": "1.0.2", - "path_provider": "2.1.4", + "path": "1.9.1", + "path_parsing": "1.1.0", + "path_provider": "2.1.5", "path_provider_android": "2.2.12", "path_provider_foundation": "2.4.0", "path_provider_linux": "2.2.1", @@ -94,7 +94,7 @@ "rohd": "0.5.3", "rohd_vf": "0.5.0", "rxdart": "0.28.0", - "shared_preferences": "2.3.2", + "shared_preferences": "2.3.3", "shared_preferences_android": "2.3.3", "shared_preferences_foundation": "2.5.3", "shared_preferences_linux": "2.4.1", @@ -114,33 +114,33 @@ "stack_trace": "1.12.0", "state_notifier": "1.0.0", "stream_channel": "2.1.2", - "string_scanner": "1.3.0", + "string_scanner": "1.4.0", "term_glyph": "1.2.1", "test": "1.25.8", "test_api": "0.7.3", "test_core": "0.6.5", "tiled": "0.10.2", - "timezone": "0.9.4", + "timezone": "0.10.0", "typed_data": "1.4.0", "unicode": "0.3.1", "url_launcher": "6.3.1", - "url_launcher_android": "6.3.13", + "url_launcher_android": "6.3.14", "url_launcher_ios": "6.3.1", - "url_launcher_linux": "3.2.0", + "url_launcher_linux": "3.2.1", "url_launcher_macos": "3.2.1", "url_launcher_platform_interface": "2.3.2", "url_launcher_web": "2.3.3", "url_launcher_windows": "3.1.3", - "vector_graphics": "1.1.11+1", - "vector_graphics_codec": "1.1.11+1", - "vector_graphics_compiler": "1.1.11+1", + "vector_graphics": "1.1.14", + "vector_graphics_codec": "1.1.12", + "vector_graphics_compiler": "1.1.15", "vector_math": "2.1.4", "video_player": "2.9.2", "video_player_android": "2.7.16", "video_player_avfoundation": "2.6.2", "video_player_platform_interface": "6.2.3", "video_player_web": "2.3.3", - "vm_service": "14.3.0", + "vm_service": "14.3.1", "watcher": "1.1.0", "web": "1.1.0", "web_socket": "0.1.6", From e023e7f7fcc083e0408c43073108e1db4c114fcb Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Thu, 14 Nov 2024 16:35:35 -0800 Subject: [PATCH 3/4] check in logging code --- pkgs/dart_services/lib/src/analysis.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkgs/dart_services/lib/src/analysis.dart b/pkgs/dart_services/lib/src/analysis.dart index ebbbb9241..1ba9cefa0 100644 --- a/pkgs/dart_services/lib/src/analysis.dart +++ b/pkgs/dart_services/lib/src/analysis.dart @@ -220,6 +220,8 @@ class AnalysisServerWrapper { ); }).catchError((dynamic error) { _logger.fine('format error: $error'); + print('error from format'); + print(' $error'); return api.FormatResponse(source: src, offset: offset); }); } From 93f8f2909a2c20ad094c93a689dfba256e92a627 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Fri, 15 Nov 2024 08:53:13 -0800 Subject: [PATCH 4/4] remove logging code --- pkgs/dart_services/lib/src/analysis.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkgs/dart_services/lib/src/analysis.dart b/pkgs/dart_services/lib/src/analysis.dart index 1ba9cefa0..ebbbb9241 100644 --- a/pkgs/dart_services/lib/src/analysis.dart +++ b/pkgs/dart_services/lib/src/analysis.dart @@ -220,8 +220,6 @@ class AnalysisServerWrapper { ); }).catchError((dynamic error) { _logger.fine('format error: $error'); - print('error from format'); - print(' $error'); return api.FormatResponse(source: src, offset: offset); }); }