22// it can be checked into version control.
33// ignore_for_file: type=lint, unused_local_variable, unnecessary_cast, unnecessary_import
44
5- library ;
5+ library ; // ignore_for_file: no_leading_underscores_for_library_prefixes
66
7- import 'dart:convert' ;
7+ import 'dart:convert' as _$convert ;
88
99import 'package:celest/celest.dart' ;
10- import 'package:celest_backend/models.dart' ;
1110import 'package:celest_core/src/exception/cloud_exception.dart' ;
11+ import 'package:celest_core/src/exception/serialization_exception.dart' ;
12+ import 'package:google_generative_ai/src/error.dart' as _$error;
13+ import 'package:http/src/exception.dart' as _$exception;
1214
1315import '../../client.dart' ;
1416
@@ -17,18 +19,10 @@ class CelestFunctions {
1719}
1820
1921class CelestFunctionsGemini {
20- /// Returns a list of available models.
21- Future <List <String >> availableModels () async {
22- final $response = await celest.httpClient.post (
23- celest.baseUri.resolve ('/gemini/available-models' ),
24- headers: const {'Content-Type' : 'application/json; charset=utf-8' },
25- );
26- final $body = (jsonDecode ($response.body) as Map <String , Object ?>);
27- if ($response.statusCode == 200 ) {
28- return ($body['response' ] as Iterable <Object ?>)
29- .map ((el) => (el as String ))
30- .toList ();
31- }
22+ Never _throwError ({
23+ required int $statusCode,
24+ required Map <String , Object ?> $body,
25+ }) {
3226 final $error = ($body['error' ] as Map <String , Object ?>);
3327 final $code = ($error['code' ] as String );
3428 final $details = ($error['details' ] as Map <String , Object ?>? );
@@ -38,8 +32,25 @@ class CelestFunctionsGemini {
3832 case r'InternalServerException' :
3933 throw Serializers .instance
4034 .deserialize <InternalServerException >($details);
35+ case r'SerializationException' :
36+ throw Serializers .instance
37+ .deserialize <SerializationException >($details);
38+ case r'GenerativeAIException' :
39+ throw Serializers .instance
40+ .deserialize< _$error.GenerativeAIException > ($details);
41+ case r'InvalidApiKey' :
42+ throw Serializers .instance.deserialize< _$error.InvalidApiKey > ($details);
43+ case r'UnsupportedUserLocation' :
44+ throw Serializers .instance
45+ .deserialize< _$error.UnsupportedUserLocation > ($details);
46+ case r'ServerException' :
47+ throw Serializers .instance
48+ .deserialize< _$error.ServerException > ($details);
49+ case r'ClientException' :
50+ throw Serializers .instance
51+ .deserialize< _$exception.ClientException > ($details);
4152 case _:
42- switch ($response. statusCode) {
53+ switch ($statusCode) {
4354 case 400 :
4455 throw BadRequestException ($code);
4556 case _:
@@ -48,44 +59,48 @@ class CelestFunctionsGemini {
4859 }
4960 }
5061
62+ /// Returns a list of available models.
63+ Future <List <String >> availableModels () async {
64+ final $response = await celest.httpClient.post (
65+ celest.baseUri.resolve ('/gemini/available-models' ),
66+ headers: const {'Content-Type' : 'application/json; charset=utf-8' },
67+ );
68+ final $body =
69+ (_$convert.jsonDecode ($response.body) as Map <String , Object ?>);
70+ if ($response.statusCode != 200 ) {
71+ _throwError (
72+ $statusCode: $response.statusCode,
73+ $body: $body,
74+ );
75+ }
76+ return ($body['response' ] as Iterable <Object ?>)
77+ .map ((el) => (el as String ))
78+ .toList ();
79+ }
80+
5181 /// Prompts the Gemini [modelName] with the given [prompt] and [parameters] .
5282 ///
5383 /// Returns the generated text.
5484 Future <String > generateContent ({
5585 required String modelName,
5686 required String prompt,
57- ModelParameters parameters = const ModelParameters (),
5887 }) async {
5988 final $response = await celest.httpClient.post (
6089 celest.baseUri.resolve ('/gemini/generate-content' ),
6190 headers: const {'Content-Type' : 'application/json; charset=utf-8' },
62- body: jsonEncode ({
91+ body: _$convert. jsonEncode ({
6392 r'modelName' : modelName,
6493 r'prompt' : prompt,
65- r'parameters' :
66- Serializers .instance.serialize <ModelParameters >(parameters),
6794 }),
6895 );
69- final $body = (jsonDecode ($response.body) as Map <String , Object ?>);
70- if ($response.statusCode == 200 ) {
71- return ($body['response' ] as String );
72- }
73- final $error = ($body['error' ] as Map <String , Object ?>);
74- final $code = ($error['code' ] as String );
75- final $details = ($error['details' ] as Map <String , Object ?>? );
76- switch ($code) {
77- case r'BadRequestException' :
78- throw Serializers .instance.deserialize <BadRequestException >($details);
79- case r'InternalServerException' :
80- throw Serializers .instance
81- .deserialize <InternalServerException >($details);
82- case _:
83- switch ($response.statusCode) {
84- case 400 :
85- throw BadRequestException ($code);
86- case _:
87- throw InternalServerException ($code);
88- }
96+ final $body =
97+ (_$convert.jsonDecode ($response.body) as Map <String , Object ?>);
98+ if ($response.statusCode != 200 ) {
99+ _throwError (
100+ $statusCode: $response.statusCode,
101+ $body: $body,
102+ );
89103 }
104+ return ($body['response' ] as String );
90105 }
91106}
0 commit comments