Mathias Mogensen 620e027c3e
feat: ai billing (#5741)
* feat: start on AI plan+billing UI

* chore: enable plan and billing

* feat: cache workspace subscription + minor fixes (#5705)

* feat: update api from billing

* feat: add api for workspace subscription info (#5717)

* feat: refactor and start integrating AI plans

* feat: refine UI and add business logic for AI

* feat: complete UIUX for AI and limits

* chore: remove resolved todo

* chore: localize remove addon dialog

* chore: fix spacing issue for usage

* fix: interpret subscription + usage on action

* chore: update api for billing (#5735)

* chore: update revisions

* fix: remove subscription cache

* fix: copy improvements + use consistent dialog

* chore: update to the latest client api

* feat: support updating billing period

* Feat/ai billing cancel reason (#5752)

* chore: add cancellation reason field

* fix: ci add one retry for concurrent sign up

* chore: merge with main

* chore: half merge

* chore: fix conflict

* chore: observer error

* chore: remove unneeded protobuf and remove unwrap

* feat: added subscription plan details

* chore: check error code and update sidebar toast

* chore: periodically check billing state

* chore: editor ai error

* chore: return file upload error

* chore: fmt

* chore: clippy

* chore: disable upload image when exceed storage limitation

* chore: remove todo

* chore: remove openai i18n

* chore: update log

* chore: update client-api to fix stream error

* chore: clippy

* chore: fix language file

* chore: disable billing UI

---------

Co-authored-by: Zack Fu Zi Xiang <speed2exe@live.com.sg>
Co-authored-by: nathan <nathan@appflowy.io>
2024-07-22 15:43:48 +08:00

146 lines
4.6 KiB
Dart

import 'dart:async';
import 'dart:convert' show utf8;
import 'dart:ffi';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:appflowy_backend/ffi.dart' as ffi;
import 'package:appflowy_backend/log.dart';
// ignore: unnecessary_import
import 'package:appflowy_backend/protobuf/dart-ffi/ffi_response.pb.dart';
import 'package:appflowy_backend/protobuf/dart-ffi/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-document/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-search/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-chat/protobuf.dart';
import 'package:appflowy_result/appflowy_result.dart';
import 'package:ffi/ffi.dart';
import 'package:isolates/isolates.dart';
import 'package:isolates/ports.dart';
import 'package:protobuf/protobuf.dart';
import '../protobuf/flowy-config/entities.pb.dart';
import '../protobuf/flowy-config/event_map.pb.dart';
import '../protobuf/flowy-date/entities.pb.dart';
import '../protobuf/flowy-date/event_map.pb.dart';
import 'error.dart';
part 'dart_event/flowy-folder/dart_event.dart';
part 'dart_event/flowy-user/dart_event.dart';
part 'dart_event/flowy-database2/dart_event.dart';
part 'dart_event/flowy-document/dart_event.dart';
part 'dart_event/flowy-config/dart_event.dart';
part 'dart_event/flowy-date/dart_event.dart';
part 'dart_event/flowy-search/dart_event.dart';
part 'dart_event/flowy-chat/dart_event.dart';
enum FFIException {
RequestIsEmpty,
}
class DispatchException implements Exception {
FFIException type;
DispatchException(this.type);
}
class Dispatch {
static Future<FlowyResult<Uint8List, Uint8List>> asyncRequest(
FFIRequest request) {
// FFIRequest => Rust SDK
final bytesFuture = _sendToRust(request);
// Rust SDK => FFIResponse
final responseFuture = _extractResponse(bytesFuture);
// FFIResponse's payload is the bytes of the Response object
final payloadFuture = _extractPayload(responseFuture);
return payloadFuture;
}
}
Future<FlowyResult<Uint8List, Uint8List>> _extractPayload(
Future<FlowyResult<FFIResponse, FlowyInternalError>> responseFuture) {
return responseFuture.then((result) {
return result.fold(
(response) {
switch (response.code) {
case FFIStatusCode.Ok:
return FlowySuccess(Uint8List.fromList(response.payload));
case FFIStatusCode.Err:
final errorBytes = Uint8List.fromList(response.payload);
GlobalErrorCodeNotifier.receiveErrorBytes(errorBytes);
return FlowyFailure(errorBytes);
case FFIStatusCode.Internal:
final error = utf8.decode(response.payload);
Log.error("Dispatch internal error: $error");
return FlowyFailure(emptyBytes());
default:
Log.error("Impossible to here");
return FlowyFailure(emptyBytes());
}
},
(error) {
Log.error("Response should not be empty $error");
return FlowyFailure(emptyBytes());
},
);
});
}
Future<FlowyResult<FFIResponse, FlowyInternalError>> _extractResponse(
Completer<Uint8List> bytesFuture) {
return bytesFuture.future.then((bytes) {
try {
final response = FFIResponse.fromBuffer(bytes);
return FlowySuccess(response);
} catch (e, s) {
final error = StackTraceError(e, s);
Log.error('Deserialize response failed. ${error.toString()}');
return FlowyFailure(error.asFlowyError());
}
});
}
Completer<Uint8List> _sendToRust(FFIRequest request) {
Uint8List bytes = request.writeToBuffer();
assert(bytes.isEmpty == false);
if (bytes.isEmpty) {
throw DispatchException(FFIException.RequestIsEmpty);
}
final Pointer<Uint8> input = calloc.allocate<Uint8>(bytes.length);
final list = input.asTypedList(bytes.length);
list.setAll(0, bytes);
final completer = Completer<Uint8List>();
final port = singleCompletePort(completer);
ffi.async_event(port.nativePort, input, bytes.length);
calloc.free(input);
return completer;
}
Uint8List requestToBytes<T extends GeneratedMessage>(T? message) {
try {
if (message != null) {
return message.writeToBuffer();
} else {
return emptyBytes();
}
} catch (e, s) {
final error = StackTraceError(e, s);
Log.error('Serial request failed. ${error.toString()}');
return emptyBytes();
}
}
Uint8List emptyBytes() {
return Uint8List.fromList([]);
}