Skip to content

Commit

Permalink
feat: Event streaming (#145)
Browse files Browse the repository at this point in the history
Adds support for server-side event streaming via SSE/WebSocket connections.
  • Loading branch information
dnys1 authored Jun 7, 2024
1 parent 737b1c1 commit cd667c7
Show file tree
Hide file tree
Showing 44 changed files with 1,424 additions and 72 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/celest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ jobs:
- name: Format
working-directory: packages/celest
run: dart format --set-exit-if-changed .
- name: Test
working-directory: packages/celest
run: dart test
- name: Test (dart2js)
working-directory: packages/celest
run: dart test -p chrome
- name: Test (dart2wasm)
working-directory: packages/celest
run: dart test -p chrome -c dart2wasm
5 changes: 3 additions & 2 deletions examples/gemini/celest/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ class Celest with CelestBase {

late CelestEnvironment _currentEnvironment;

late final NativeStorage _storage = NativeStorage(scope: 'celest');
@override
late final NativeStorage nativeStorage = NativeStorage(scope: 'celest');

@override
late _$http.Client httpClient =
CelestHttpClient(secureStorage: _storage.secure);
CelestHttpClient(secureStorage: nativeStorage.secure);

late Uri _baseUri;

Expand Down
4 changes: 2 additions & 2 deletions examples/gemini/celest/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ environment:
sdk: ^3.3.0

dependencies:
celest: ^0.4.0-0
celest: ^0.4.0
google_generative_ai: ^0.2.0
http: ">=0.13.0 <2.0.0"

Expand All @@ -20,4 +20,4 @@ dependency_overrides:

dev_dependencies:
lints: ^4.0.0
test: ^1.24.0
test: ^1.25.0
5 changes: 3 additions & 2 deletions examples/openai/celest/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ class Celest with CelestBase {

late CelestEnvironment _currentEnvironment;

late final NativeStorage _storage = NativeStorage(scope: 'celest');
@override
late final NativeStorage nativeStorage = NativeStorage(scope: 'celest');

@override
late _$http.Client httpClient =
CelestHttpClient(secureStorage: _storage.secure);
CelestHttpClient(secureStorage: nativeStorage.secure);

late Uri _baseUri;

Expand Down
4 changes: 2 additions & 2 deletions examples/openai/celest/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ environment:
sdk: ^3.3.0

dependencies:
celest: ^0.4.0-0
celest: ^0.4.0
chat_gpt_sdk: ^2.2.8
http: ">=0.13.0 <2.0.0"

Expand All @@ -20,4 +20,4 @@ dependency_overrides:

dev_dependencies:
lints: ^4.0.0
test: ^1.24.0
test: ^1.25.0
5 changes: 3 additions & 2 deletions examples/todo/celest/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ class Celest with CelestBase {

late CelestEnvironment _currentEnvironment;

late final NativeStorage _storage = NativeStorage(scope: 'celest');
@override
late final NativeStorage nativeStorage = NativeStorage(scope: 'celest');

@override
late _$http.Client httpClient =
CelestHttpClient(secureStorage: _storage.secure);
CelestHttpClient(secureStorage: nativeStorage.secure);

late Uri _baseUri;

Expand Down
11 changes: 11 additions & 0 deletions examples/todo/celest/lib/src/client/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import 'dart:convert' as _$convert;
import 'package:celest/celest.dart';
import 'package:celest_backend/exceptions.dart' as _$exceptions;
import 'package:celest_backend/models.dart' as _$models;
import 'package:celest_core/src/exception/cloud_exception.dart';
import 'package:celest_core/src/exception/serialization_exception.dart';

import '../../client.dart';

Expand All @@ -25,6 +27,15 @@ class CelestFunctionsTasks {
final $code = ($error['code'] as String);
final $details = ($error['details'] as Map<String, Object?>?);
switch ($code) {
case r'BadRequestException':
throw Serializers.instance.deserialize<BadRequestException>($details);
case r'UnauthorizedException':
throw Serializers.instance.deserialize<UnauthorizedException>($details);
case r'InternalServerError':
throw Serializers.instance.deserialize<InternalServerError>($details);
case r'SerializationException':
throw Serializers.instance
.deserialize<SerializationException>($details);
case r'ServerException':
throw Serializers.instance
.deserialize<_$exceptions.ServerException>($details);
Expand Down
35 changes: 35 additions & 0 deletions examples/todo/celest/lib/src/client/serializers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import 'package:celest/celest.dart';
import 'package:celest_backend/exceptions.dart' as _$exceptions;
import 'package:celest_backend/models.dart' as _$models;
import 'package:celest_core/src/exception/cloud_exception.dart';
import 'package:celest_core/src/exception/serialization_exception.dart';

void initSerializers() {
Serializers.instance.put(
Expand Down Expand Up @@ -38,4 +40,37 @@ void initSerializers() {
);
},
));
Serializers.instance
.put(Serializer.define<BadRequestException, Map<String, Object?>>(
serialize: ($value) => {r'message': $value.message},
deserialize: ($serialized) {
return BadRequestException(($serialized[r'message'] as String));
},
));
Serializers.instance
.put(Serializer.define<InternalServerError, Map<String, Object?>>(
serialize: ($value) => {r'message': $value.message},
deserialize: ($serialized) {
return InternalServerError(($serialized[r'message'] as String));
},
));
Serializers.instance
.put(Serializer.define<UnauthorizedException, Map<String, Object?>?>(
serialize: ($value) => {r'message': $value.message},
deserialize: ($serialized) {
return UnauthorizedException(
(($serialized?[r'message'] as String?)) ?? 'Unauthorized');
},
));
Serializers.instance
.put(Serializer.define<SerializationException, Map<String, Object?>>(
serialize: ($value) => {
r'message': $value.message,
r'offset': $value.offset,
r'source': $value.source,
},
deserialize: ($serialized) {
return SerializationException(($serialized[r'message'] as String));
},
));
}
4 changes: 2 additions & 2 deletions examples/todo/celest/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ environment:
sdk: ^3.3.0

dependencies:
celest: ^0.4.0-0
celest: ^0.4.0
http: ">=0.13.0 <2.0.0"
uuid: ^4.3.3

Expand All @@ -20,4 +20,4 @@ dependency_overrides:

dev_dependencies:
lints: ^4.0.0
test: ^1.24.0
test: ^1.25.0
9 changes: 5 additions & 4 deletions packages/celest/example/celest/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ enum CelestEnvironment {

Uri get baseUri => switch (this) {
local => kIsWeb || !_$io.Platform.isAndroid
? Uri.parse('http://localhost:7777')
: Uri.parse('http://10.0.2.2:7777'),
? Uri.parse('http://localhost:7778')
: Uri.parse('http://10.0.2.2:7778'),
};
}

Expand All @@ -30,11 +30,12 @@ class Celest with CelestBase {

late CelestEnvironment _currentEnvironment;

late final NativeStorage _storage = NativeStorage(scope: 'celest');
@override
late final NativeStorage nativeStorage = NativeStorage(scope: 'celest');

@override
late _$http.Client httpClient =
CelestHttpClient(secureStorage: _storage.secure);
CelestHttpClient(secureStorage: nativeStorage.secure);

late Uri _baseUri;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CelestFunctionsGreeting {
Future<String> sayHello({required _$person.Person person}) async {
final $response = await celest.httpClient.post(
celest.baseUri.resolve('/greeting/say-hello'),
headers: const {'Content-Type': 'application/json; charset=utf-8'},
headers: {'Content-Type': 'application/json; charset=utf-8'},
body: _$convert.jsonEncode(
{r'person': Serializers.instance.serialize<_$person.Person>(person)}),
);
Expand Down
4 changes: 2 additions & 2 deletions packages/celest/example/celest/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ environment:
sdk: ^3.3.0

dependencies:
celest: ^0.4.0-0
celest: ^0.4.0
http: ">=0.13.0 <2.0.0"

dependency_overrides:
Expand All @@ -19,4 +19,4 @@ dependency_overrides:

dev_dependencies:
lints: ^4.0.0
test: ^1.24.0
test: ^1.25.0
16 changes: 16 additions & 0 deletions packages/celest/example/ios/Podfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cd667c7

Please sign in to comment.