diff --git a/lib/llm/plugins/chat_api/.gitkeep b/lib/llm/plugins/chat_api/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/lib/llm/plugins/chat_url/.gitkeep b/lib/llm/plugins/chat_url/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/lib/llm/template_editor/components/chain_flow.dart b/lib/llm/template_editor/components/chain_flow.dart index 8aeb12e..f5276ef 100644 --- a/lib/llm/template_editor/components/chain_flow.dart +++ b/lib/llm/template_editor/components/chain_flow.dart @@ -26,7 +26,7 @@ class _ChainFlowState extends ConsumerState { final ScrollController controller2 = ScrollController(); late double childrenHeight = 0; - List<(String, AttributeType)> items = []; + List<(String, AttributeType, String?)> items = []; init() async { items = diff --git a/lib/llm/template_editor/notifiers/chain_flow_notifier.dart b/lib/llm/template_editor/notifiers/chain_flow_notifier.dart index 34e8b3d..3ea3b8c 100644 --- a/lib/llm/template_editor/notifiers/chain_flow_notifier.dart +++ b/lib/llm/template_editor/notifiers/chain_flow_notifier.dart @@ -25,11 +25,12 @@ class ChainFlowNotifier extends Notifier { /// FIXME /// BUG /// 不灵活的实现方案 - Future> toRust() async { - List<(String, int, int?, AttributeType)> result = []; + Future> toRust() async { + List<(String, int, int?, AttributeType, String?)> result = []; final items = await templateToPrompts(template: state.content); final itemTuple = items - .mapIndexed((index, element) => ((index, element.$1, element.$2))) + .mapIndexed( + (index, element) => ((index, element.$1, element.$2, element.$3))) .toList(); List ids = []; @@ -37,10 +38,16 @@ class ChainFlowNotifier extends Notifier { ids.addAll(i.ids); for (int j = 0; j < i.ids.length; j++) { if (j < i.ids.length - 1) { - result - .add((i.contents[j], i.ids[j], ids[j + 1], AttributeType.prompt)); + result.add(( + i.contents[j], + i.ids[j], + ids[j + 1], + AttributeType.prompt, + null + )); } else { - result.add((i.contents[j], i.ids[j], null, AttributeType.prompt)); + result + .add((i.contents[j], i.ids[j], null, AttributeType.prompt, null)); } } } @@ -48,7 +55,7 @@ class ChainFlowNotifier extends Notifier { itemTuple.retainWhere((element) => !ids.contains(element.$1)); for (final t in itemTuple) { - result.add((t.$2, t.$1, null, t.$3)); + result.add((t.$2, t.$1, null, t.$3, t.$4)); } return result; diff --git a/lib/llm/template_editor/template_editor.dart b/lib/llm/template_editor/template_editor.dart index fc9bb36..f2d4f4a 100644 --- a/lib/llm/template_editor/template_editor.dart +++ b/lib/llm/template_editor/template_editor.dart @@ -137,6 +137,7 @@ class _TemplateEditorState extends ConsumerState { heroTag: null, child: const Icon(Bootstrap.file_word), onPressed: () async { + // print(_editorState.document.toJson()); ref .read(chainFlowProvider.notifier) .changeContent(jsonEncode(_editorState.document.toJson())); @@ -151,7 +152,8 @@ class _TemplateEditorState extends ConsumerState { return const LoadingDialog(); }); - generateFromTemplate(v: l).then((value) async { + generateFromTemplate(v: l, enablePlugin: true) + .then((value) async { final md = await optimizeDoc(s: _editorState.toStr()); setState( () { diff --git a/lib/src/rust/api/llm_api.dart b/lib/src/rust/api/llm_api.dart index a8c99df..68d4cfa 100644 --- a/lib/src/rust/api/llm_api.dart +++ b/lib/src/rust/api/llm_api.dart @@ -41,13 +41,16 @@ Future sequentialChainChat( Future templateRenderer({required String template, dynamic hint}) => RustLib.instance.api.templateRenderer(template: template, hint: hint); -Future> templateToPrompts( +Future> templateToPrompts( {required String template, dynamic hint}) => RustLib.instance.api.templateToPrompts(template: template, hint: hint); Future generateFromTemplate( - {required List<(String, int, int?, AttributeType)> v, dynamic hint}) => - RustLib.instance.api.generateFromTemplate(v: v, hint: hint); + {required List<(String, int, int?, AttributeType, String?)> v, + required bool enablePlugin, + dynamic hint}) => + RustLib.instance.api + .generateFromTemplate(v: v, enablePlugin: enablePlugin, hint: hint); Future optimizeDoc({required String s, dynamic hint}) => RustLib.instance.api.optimizeDoc(s: s, hint: hint); diff --git a/lib/src/rust/frb_generated.dart b/lib/src/rust/frb_generated.dart index 896f807..44178ad 100644 --- a/lib/src/rust/frb_generated.dart +++ b/lib/src/rust/frb_generated.dart @@ -85,7 +85,9 @@ abstract class RustLibApi extends BaseApi { dynamic hint}); Future generateFromTemplate( - {required List<(String, int, int?, AttributeType)> v, dynamic hint}); + {required List<(String, int, int?, AttributeType, String?)> v, + required bool enablePlugin, + dynamic hint}); Root? getDocRootFromStr({required String s, dynamic hint}); @@ -106,7 +108,7 @@ abstract class RustLibApi extends BaseApi { Stream templateStateStream({dynamic hint}); - Future> templateToPrompts( + Future> templateToPrompts( {required String template, dynamic hint}); Future?> eval( @@ -200,12 +202,15 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { @override Future generateFromTemplate( - {required List<(String, int, int?, AttributeType)> v, dynamic hint}) { + {required List<(String, int, int?, AttributeType, String?)> v, + required bool enablePlugin, + dynamic hint}) { return handler.executeNormal(NormalTask( callFfi: (port_) { final serializer = SseSerializer(generalizedFrbRustBinding); - sse_encode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type( + sse_encode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( v, serializer); + sse_encode_bool(enablePlugin, serializer); pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 10, port: port_); }, @@ -214,7 +219,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { decodeErrorData: null, ), constMeta: kGenerateFromTemplateConstMeta, - argValues: [v], + argValues: [v, enablePlugin], apiImpl: this, hint: hint, )); @@ -222,7 +227,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { TaskConstMeta get kGenerateFromTemplateConstMeta => const TaskConstMeta( debugName: "generate_from_template", - argNames: ["v"], + argNames: ["v", "enablePlugin"], ); @override @@ -452,7 +457,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { ); @override - Future> templateToPrompts( + Future> templateToPrompts( {required String template, dynamic hint}) { return handler.executeNormal(NormalTask( callFfi: (port_) { @@ -462,7 +467,8 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { funcId: 9, port: port_); }, codec: SseCodec( - decodeSuccessData: sse_decode_list_record_string_attribute_type, + decodeSuccessData: + sse_decode_list_record_string_attribute_type_opt_string, decodeErrorData: null, ), constMeta: kTemplateToPromptsConstMeta, @@ -963,8 +969,8 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { if (arr.length != 4) throw Exception('unexpected arr length: expect 4 but see ${arr.length}'); return Attributes( - bold: dco_decode_bool(arr[0]), - italic: dco_decode_bool(arr[1]), + bold: dco_decode_opt_box_autoadd_bool(arr[0]), + italic: dco_decode_opt_box_autoadd_bool(arr[1]), file: dco_decode_opt_String(arr[2]), sql: dco_decode_opt_String(arr[3]), ); @@ -982,6 +988,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { return dco_decode_attributes(raw); } + @protected + bool dco_decode_box_autoadd_bool(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + return raw as bool; + } + @protected CpuInfo dco_decode_box_autoadd_cpu_info(dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs @@ -1204,11 +1216,11 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } @protected - List<(String, AttributeType)> dco_decode_list_record_string_attribute_type( - dynamic raw) { + List<(String, AttributeType, String?)> + dco_decode_list_record_string_attribute_type_opt_string(dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs return (raw as List) - .map(dco_decode_record_string_attribute_type) + .map(dco_decode_record_string_attribute_type_opt_string) .toList(); } @@ -1228,12 +1240,13 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } @protected - List<(String, int, int?, AttributeType)> - dco_decode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type( + List<(String, int, int?, AttributeType, String?)> + dco_decode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs return (raw as List) - .map(dco_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type) + .map( + dco_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string) .toList(); } @@ -1334,6 +1347,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { return raw == null ? null : dco_decode_box_autoadd_attributes(raw); } + @protected + bool? dco_decode_opt_box_autoadd_bool(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + return raw == null ? null : dco_decode_box_autoadd_bool(raw); + } + @protected CpuInfo? dco_decode_opt_box_autoadd_cpu_info(dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs @@ -1442,15 +1461,17 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } @protected - (String, AttributeType) dco_decode_record_string_attribute_type(dynamic raw) { + (String, AttributeType, String?) + dco_decode_record_string_attribute_type_opt_string(dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs final arr = raw as List; - if (arr.length != 2) { - throw Exception('Expected 2 elements, got ${arr.length}'); + if (arr.length != 3) { + throw Exception('Expected 3 elements, got ${arr.length}'); } return ( dco_decode_String(arr[0]), dco_decode_attribute_type(arr[1]), + dco_decode_opt_String(arr[2]), ); } @@ -1481,19 +1502,25 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } @protected - (String, int, int?, AttributeType) - dco_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type( - dynamic raw) { + ( + String, + int, + int?, + AttributeType, + String? + ) dco_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( + dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs final arr = raw as List; - if (arr.length != 4) { - throw Exception('Expected 4 elements, got ${arr.length}'); + if (arr.length != 5) { + throw Exception('Expected 5 elements, got ${arr.length}'); } return ( dco_decode_String(arr[0]), dco_decode_u_32(arr[1]), dco_decode_opt_box_autoadd_u_32(arr[2]), dco_decode_attribute_type(arr[3]), + dco_decode_opt_String(arr[4]), ); } @@ -1701,8 +1728,8 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { @protected Attributes sse_decode_attributes(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs - var var_bold = sse_decode_bool(deserializer); - var var_italic = sse_decode_bool(deserializer); + var var_bold = sse_decode_opt_box_autoadd_bool(deserializer); + var var_italic = sse_decode_opt_box_autoadd_bool(deserializer); var var_file = sse_decode_opt_String(deserializer); var var_sql = sse_decode_opt_String(deserializer); return Attributes( @@ -1721,6 +1748,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { return (sse_decode_attributes(deserializer)); } + @protected + bool sse_decode_box_autoadd_bool(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + return (sse_decode_bool(deserializer)); + } + @protected CpuInfo sse_decode_box_autoadd_cpu_info(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -1976,14 +2009,16 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } @protected - List<(String, AttributeType)> sse_decode_list_record_string_attribute_type( - SseDeserializer deserializer) { + List<(String, AttributeType, String?)> + sse_decode_list_record_string_attribute_type_opt_string( + SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs var len_ = sse_decode_i_32(deserializer); - var ans_ = <(String, AttributeType)>[]; + var ans_ = <(String, AttributeType, String?)>[]; for (var idx_ = 0; idx_ < len_; ++idx_) { - ans_.add(sse_decode_record_string_attribute_type(deserializer)); + ans_.add( + sse_decode_record_string_attribute_type_opt_string(deserializer)); } return ans_; } @@ -2015,16 +2050,16 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } @protected - List<(String, int, int?, AttributeType)> - sse_decode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type( + List<(String, int, int?, AttributeType, String?)> + sse_decode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs var len_ = sse_decode_i_32(deserializer); - var ans_ = <(String, int, int?, AttributeType)>[]; + var ans_ = <(String, int, int?, AttributeType, String?)>[]; for (var idx_ = 0; idx_ < len_; ++idx_) { ans_.add( - sse_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type( + sse_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( deserializer)); } return ans_; @@ -2163,6 +2198,17 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } } + @protected + bool? sse_decode_opt_box_autoadd_bool(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + + if (sse_decode_bool(deserializer)) { + return (sse_decode_box_autoadd_bool(deserializer)); + } else { + return null; + } + } + @protected CpuInfo? sse_decode_opt_box_autoadd_cpu_info(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -2323,12 +2369,14 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } @protected - (String, AttributeType) sse_decode_record_string_attribute_type( - SseDeserializer deserializer) { + (String, AttributeType, String?) + sse_decode_record_string_attribute_type_opt_string( + SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs var var_field0 = sse_decode_String(deserializer); var var_field1 = sse_decode_attribute_type(deserializer); - return (var_field0, var_field1); + var var_field2 = sse_decode_opt_String(deserializer); + return (var_field0, var_field1, var_field2); } @protected @@ -2350,15 +2398,21 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } @protected - (String, int, int?, AttributeType) - sse_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type( - SseDeserializer deserializer) { + ( + String, + int, + int?, + AttributeType, + String? + ) sse_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( + SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs var var_field0 = sse_decode_String(deserializer); var var_field1 = sse_decode_u_32(deserializer); var var_field2 = sse_decode_opt_box_autoadd_u_32(deserializer); var var_field3 = sse_decode_attribute_type(deserializer); - return (var_field0, var_field1, var_field2, var_field3); + var var_field4 = sse_decode_opt_String(deserializer); + return (var_field0, var_field1, var_field2, var_field3, var_field4); } @protected @@ -2571,8 +2625,8 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { @protected void sse_encode_attributes(Attributes self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs - sse_encode_bool(self.bold, serializer); - sse_encode_bool(self.italic, serializer); + sse_encode_opt_box_autoadd_bool(self.bold, serializer); + sse_encode_opt_box_autoadd_bool(self.italic, serializer); sse_encode_opt_String(self.file, serializer); sse_encode_opt_String(self.sql, serializer); } @@ -2590,6 +2644,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { sse_encode_attributes(self, serializer); } + @protected + void sse_encode_box_autoadd_bool(bool self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + sse_encode_bool(self, serializer); + } + @protected void sse_encode_box_autoadd_cpu_info(CpuInfo self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -2812,12 +2872,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } @protected - void sse_encode_list_record_string_attribute_type( - List<(String, AttributeType)> self, SseSerializer serializer) { + void sse_encode_list_record_string_attribute_type_opt_string( + List<(String, AttributeType, String?)> self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs sse_encode_i_32(self.length, serializer); for (final item in self) { - sse_encode_record_string_attribute_type(item, serializer); + sse_encode_record_string_attribute_type_opt_string(item, serializer); } } @@ -2842,12 +2902,14 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } @protected - void sse_encode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type( - List<(String, int, int?, AttributeType)> self, SseSerializer serializer) { + void + sse_encode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( + List<(String, int, int?, AttributeType, String?)> self, + SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs sse_encode_i_32(self.length, serializer); for (final item in self) { - sse_encode_record_string_u_32_opt_box_autoadd_u_32_attribute_type( + sse_encode_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( item, serializer); } } @@ -2958,6 +3020,16 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } } + @protected + void sse_encode_opt_box_autoadd_bool(bool? self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + + sse_encode_bool(self != null, serializer); + if (self != null) { + sse_encode_box_autoadd_bool(self, serializer); + } + } + @protected void sse_encode_opt_box_autoadd_cpu_info( CpuInfo? self, SseSerializer serializer) { @@ -3103,11 +3175,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } @protected - void sse_encode_record_string_attribute_type( - (String, AttributeType) self, SseSerializer serializer) { + void sse_encode_record_string_attribute_type_opt_string( + (String, AttributeType, String?) self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs sse_encode_String(self.$1, serializer); sse_encode_attribute_type(self.$2, serializer); + sse_encode_opt_String(self.$3, serializer); } @protected @@ -3127,13 +3200,16 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { } @protected - void sse_encode_record_string_u_32_opt_box_autoadd_u_32_attribute_type( - (String, int, int?, AttributeType) self, SseSerializer serializer) { + void + sse_encode_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( + (String, int, int?, AttributeType, String?) self, + SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs sse_encode_String(self.$1, serializer); sse_encode_u_32(self.$2, serializer); sse_encode_opt_box_autoadd_u_32(self.$3, serializer); sse_encode_attribute_type(self.$4, serializer); + sse_encode_opt_String(self.$5, serializer); } @protected diff --git a/lib/src/rust/frb_generated.io.dart b/lib/src/rust/frb_generated.io.dart index 99c5c74..90269df 100644 --- a/lib/src/rust/frb_generated.io.dart +++ b/lib/src/rust/frb_generated.io.dart @@ -90,6 +90,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected Attributes dco_decode_box_autoadd_attributes(dynamic raw); + @protected + bool dco_decode_box_autoadd_bool(dynamic raw); + @protected CpuInfo dco_decode_box_autoadd_cpu_info(dynamic raw); @@ -177,8 +180,8 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { List<(int, String)> dco_decode_list_record_i_64_string(dynamic raw); @protected - List<(String, AttributeType)> dco_decode_list_record_string_attribute_type( - dynamic raw); + List<(String, AttributeType, String?)> + dco_decode_list_record_string_attribute_type_opt_string(dynamic raw); @protected List<(String, CellType)> dco_decode_list_record_string_cell_type(dynamic raw); @@ -187,8 +190,8 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { List<(String, String)> dco_decode_list_record_string_string(dynamic raw); @protected - List<(String, int, int?, AttributeType)> - dco_decode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type( + List<(String, int, int?, AttributeType, String?)> + dco_decode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( dynamic raw); @protected @@ -224,6 +227,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected Attributes? dco_decode_opt_box_autoadd_attributes(dynamic raw); + @protected + bool? dco_decode_opt_box_autoadd_bool(dynamic raw); + @protected CpuInfo? dco_decode_opt_box_autoadd_cpu_info(dynamic raw); @@ -268,7 +274,8 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { dynamic raw); @protected - (String, AttributeType) dco_decode_record_string_attribute_type(dynamic raw); + (String, AttributeType, String?) + dco_decode_record_string_attribute_type_opt_string(dynamic raw); @protected (String, CellType) dco_decode_record_string_cell_type(dynamic raw); @@ -277,9 +284,14 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { (String, String) dco_decode_record_string_string(dynamic raw); @protected - (String, int, int?, AttributeType) - dco_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type( - dynamic raw); + ( + String, + int, + int?, + AttributeType, + String? + ) dco_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( + dynamic raw); @protected Root dco_decode_root(dynamic raw); @@ -375,6 +387,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected Attributes sse_decode_box_autoadd_attributes(SseDeserializer deserializer); + @protected + bool sse_decode_box_autoadd_bool(SseDeserializer deserializer); + @protected CpuInfo sse_decode_box_autoadd_cpu_info(SseDeserializer deserializer); @@ -466,8 +481,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { SseDeserializer deserializer); @protected - List<(String, AttributeType)> sse_decode_list_record_string_attribute_type( - SseDeserializer deserializer); + List<(String, AttributeType, String?)> + sse_decode_list_record_string_attribute_type_opt_string( + SseDeserializer deserializer); @protected List<(String, CellType)> sse_decode_list_record_string_cell_type( @@ -478,8 +494,8 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { SseDeserializer deserializer); @protected - List<(String, int, int?, AttributeType)> - sse_decode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type( + List<(String, int, int?, AttributeType, String?)> + sse_decode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( SseDeserializer deserializer); @protected @@ -518,6 +534,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { Attributes? sse_decode_opt_box_autoadd_attributes( SseDeserializer deserializer); + @protected + bool? sse_decode_opt_box_autoadd_bool(SseDeserializer deserializer); + @protected CpuInfo? sse_decode_opt_box_autoadd_cpu_info(SseDeserializer deserializer); @@ -569,8 +588,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { SseDeserializer deserializer); @protected - (String, AttributeType) sse_decode_record_string_attribute_type( - SseDeserializer deserializer); + (String, AttributeType, String?) + sse_decode_record_string_attribute_type_opt_string( + SseDeserializer deserializer); @protected (String, CellType) sse_decode_record_string_cell_type( @@ -581,9 +601,14 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { SseDeserializer deserializer); @protected - (String, int, int?, AttributeType) - sse_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type( - SseDeserializer deserializer); + ( + String, + int, + int?, + AttributeType, + String? + ) sse_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( + SseDeserializer deserializer); @protected Root sse_decode_root(SseDeserializer deserializer); @@ -680,6 +705,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { void sse_encode_box_autoadd_attributes( Attributes self, SseSerializer serializer); + @protected + void sse_encode_box_autoadd_bool(bool self, SseSerializer serializer); + @protected void sse_encode_box_autoadd_cpu_info(CpuInfo self, SseSerializer serializer); @@ -778,8 +806,8 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { List<(int, String)> self, SseSerializer serializer); @protected - void sse_encode_list_record_string_attribute_type( - List<(String, AttributeType)> self, SseSerializer serializer); + void sse_encode_list_record_string_attribute_type_opt_string( + List<(String, AttributeType, String?)> self, SseSerializer serializer); @protected void sse_encode_list_record_string_cell_type( @@ -790,8 +818,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { List<(String, String)> self, SseSerializer serializer); @protected - void sse_encode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type( - List<(String, int, int?, AttributeType)> self, SseSerializer serializer); + void + sse_encode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( + List<(String, int, int?, AttributeType, String?)> self, + SseSerializer serializer); @protected void sse_encode_list_software(List self, SseSerializer serializer); @@ -831,6 +861,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { void sse_encode_opt_box_autoadd_attributes( Attributes? self, SseSerializer serializer); + @protected + void sse_encode_opt_box_autoadd_bool(bool? self, SseSerializer serializer); + @protected void sse_encode_opt_box_autoadd_cpu_info( CpuInfo? self, SseSerializer serializer); @@ -885,8 +918,8 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { (Int64List, String) self, SseSerializer serializer); @protected - void sse_encode_record_string_attribute_type( - (String, AttributeType) self, SseSerializer serializer); + void sse_encode_record_string_attribute_type_opt_string( + (String, AttributeType, String?) self, SseSerializer serializer); @protected void sse_encode_record_string_cell_type( @@ -897,8 +930,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { (String, String) self, SseSerializer serializer); @protected - void sse_encode_record_string_u_32_opt_box_autoadd_u_32_attribute_type( - (String, int, int?, AttributeType) self, SseSerializer serializer); + void + sse_encode_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( + (String, int, int?, AttributeType, String?) self, + SseSerializer serializer); @protected void sse_encode_root(Root self, SseSerializer serializer); diff --git a/lib/src/rust/frb_generated.web.dart b/lib/src/rust/frb_generated.web.dart index 94d908b..7985476 100644 --- a/lib/src/rust/frb_generated.web.dart +++ b/lib/src/rust/frb_generated.web.dart @@ -89,6 +89,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected Attributes dco_decode_box_autoadd_attributes(dynamic raw); + @protected + bool dco_decode_box_autoadd_bool(dynamic raw); + @protected CpuInfo dco_decode_box_autoadd_cpu_info(dynamic raw); @@ -176,8 +179,8 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { List<(int, String)> dco_decode_list_record_i_64_string(dynamic raw); @protected - List<(String, AttributeType)> dco_decode_list_record_string_attribute_type( - dynamic raw); + List<(String, AttributeType, String?)> + dco_decode_list_record_string_attribute_type_opt_string(dynamic raw); @protected List<(String, CellType)> dco_decode_list_record_string_cell_type(dynamic raw); @@ -186,8 +189,8 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { List<(String, String)> dco_decode_list_record_string_string(dynamic raw); @protected - List<(String, int, int?, AttributeType)> - dco_decode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type( + List<(String, int, int?, AttributeType, String?)> + dco_decode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( dynamic raw); @protected @@ -223,6 +226,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected Attributes? dco_decode_opt_box_autoadd_attributes(dynamic raw); + @protected + bool? dco_decode_opt_box_autoadd_bool(dynamic raw); + @protected CpuInfo? dco_decode_opt_box_autoadd_cpu_info(dynamic raw); @@ -267,7 +273,8 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { dynamic raw); @protected - (String, AttributeType) dco_decode_record_string_attribute_type(dynamic raw); + (String, AttributeType, String?) + dco_decode_record_string_attribute_type_opt_string(dynamic raw); @protected (String, CellType) dco_decode_record_string_cell_type(dynamic raw); @@ -276,9 +283,14 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { (String, String) dco_decode_record_string_string(dynamic raw); @protected - (String, int, int?, AttributeType) - dco_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type( - dynamic raw); + ( + String, + int, + int?, + AttributeType, + String? + ) dco_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( + dynamic raw); @protected Root dco_decode_root(dynamic raw); @@ -374,6 +386,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected Attributes sse_decode_box_autoadd_attributes(SseDeserializer deserializer); + @protected + bool sse_decode_box_autoadd_bool(SseDeserializer deserializer); + @protected CpuInfo sse_decode_box_autoadd_cpu_info(SseDeserializer deserializer); @@ -465,8 +480,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { SseDeserializer deserializer); @protected - List<(String, AttributeType)> sse_decode_list_record_string_attribute_type( - SseDeserializer deserializer); + List<(String, AttributeType, String?)> + sse_decode_list_record_string_attribute_type_opt_string( + SseDeserializer deserializer); @protected List<(String, CellType)> sse_decode_list_record_string_cell_type( @@ -477,8 +493,8 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { SseDeserializer deserializer); @protected - List<(String, int, int?, AttributeType)> - sse_decode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type( + List<(String, int, int?, AttributeType, String?)> + sse_decode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( SseDeserializer deserializer); @protected @@ -517,6 +533,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { Attributes? sse_decode_opt_box_autoadd_attributes( SseDeserializer deserializer); + @protected + bool? sse_decode_opt_box_autoadd_bool(SseDeserializer deserializer); + @protected CpuInfo? sse_decode_opt_box_autoadd_cpu_info(SseDeserializer deserializer); @@ -568,8 +587,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { SseDeserializer deserializer); @protected - (String, AttributeType) sse_decode_record_string_attribute_type( - SseDeserializer deserializer); + (String, AttributeType, String?) + sse_decode_record_string_attribute_type_opt_string( + SseDeserializer deserializer); @protected (String, CellType) sse_decode_record_string_cell_type( @@ -580,9 +600,14 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { SseDeserializer deserializer); @protected - (String, int, int?, AttributeType) - sse_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type( - SseDeserializer deserializer); + ( + String, + int, + int?, + AttributeType, + String? + ) sse_decode_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( + SseDeserializer deserializer); @protected Root sse_decode_root(SseDeserializer deserializer); @@ -679,6 +704,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { void sse_encode_box_autoadd_attributes( Attributes self, SseSerializer serializer); + @protected + void sse_encode_box_autoadd_bool(bool self, SseSerializer serializer); + @protected void sse_encode_box_autoadd_cpu_info(CpuInfo self, SseSerializer serializer); @@ -777,8 +805,8 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { List<(int, String)> self, SseSerializer serializer); @protected - void sse_encode_list_record_string_attribute_type( - List<(String, AttributeType)> self, SseSerializer serializer); + void sse_encode_list_record_string_attribute_type_opt_string( + List<(String, AttributeType, String?)> self, SseSerializer serializer); @protected void sse_encode_list_record_string_cell_type( @@ -789,8 +817,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { List<(String, String)> self, SseSerializer serializer); @protected - void sse_encode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type( - List<(String, int, int?, AttributeType)> self, SseSerializer serializer); + void + sse_encode_list_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( + List<(String, int, int?, AttributeType, String?)> self, + SseSerializer serializer); @protected void sse_encode_list_software(List self, SseSerializer serializer); @@ -830,6 +860,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { void sse_encode_opt_box_autoadd_attributes( Attributes? self, SseSerializer serializer); + @protected + void sse_encode_opt_box_autoadd_bool(bool? self, SseSerializer serializer); + @protected void sse_encode_opt_box_autoadd_cpu_info( CpuInfo? self, SseSerializer serializer); @@ -884,8 +917,8 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { (Int64List, String) self, SseSerializer serializer); @protected - void sse_encode_record_string_attribute_type( - (String, AttributeType) self, SseSerializer serializer); + void sse_encode_record_string_attribute_type_opt_string( + (String, AttributeType, String?) self, SseSerializer serializer); @protected void sse_encode_record_string_cell_type( @@ -896,8 +929,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { (String, String) self, SseSerializer serializer); @protected - void sse_encode_record_string_u_32_opt_box_autoadd_u_32_attribute_type( - (String, int, int?, AttributeType) self, SseSerializer serializer); + void + sse_encode_record_string_u_32_opt_box_autoadd_u_32_attribute_type_opt_string( + (String, int, int?, AttributeType, String?) self, + SseSerializer serializer); @protected void sse_encode_root(Root self, SseSerializer serializer); diff --git a/lib/src/rust/llm/app_flowy_model.dart b/lib/src/rust/llm/app_flowy_model.dart index d0f263a..343ed11 100644 --- a/lib/src/rust/llm/app_flowy_model.dart +++ b/lib/src/rust/llm/app_flowy_model.dart @@ -13,14 +13,14 @@ enum AttributeType { } class Attributes { - final bool bold; - final bool italic; + final bool? bold; + final bool? italic; final String? file; final String? sql; const Attributes({ - required this.bold, - required this.italic, + this.bold, + this.italic, this.file, this.sql, }); diff --git a/pubspec.yaml b/pubspec.yaml index 5885911..171c57c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: all_in_one description: "A new Flutter project." publish_to: 'none' # Remove this line if you wish to publish to pub.dev -version: 0.0.6 +version: 0.0.7 environment: sdk: '>=3.3.0 <4.0.0' diff --git a/rust/src/api/llm_api.rs b/rust/src/api/llm_api.rs index c5ffd2a..d546194 100644 --- a/rust/src/api/llm_api.rs +++ b/rust/src/api/llm_api.rs @@ -63,20 +63,25 @@ pub fn template_renderer(template: String) -> Option { None } -pub fn template_to_prompts(template: String) -> Vec<(String, AttributeType)> { +pub fn template_to_prompts(template: String) -> Vec<(String, AttributeType, Option)> { let r = crate::llm::app_flowy_model::get_all_cadidates(template); if let Ok(_r) = r { return _r; + }else{ + println!("template_to_prompts error {:?}",r.err()); } return vec![]; } -pub fn generate_from_template(v: Vec<(String, u32, Option, AttributeType)>) { +pub fn generate_from_template( + v: Vec<(String, u32, Option, AttributeType, Option)>, + enable_plugin: bool, +) { let rt = tokio::runtime::Runtime::new().unwrap(); rt.block_on(async { let list = generate_template_items_from_list(v); let mut a = AppFlowyTemplate::from(list); - a.execute(false).await; + a.execute(enable_plugin).await; }); } diff --git a/rust/src/frb_generated.rs b/rust/src/frb_generated.rs index 745fe4c..c602dbe 100644 --- a/rust/src/frb_generated.rs +++ b/rust/src/frb_generated.rs @@ -106,11 +106,16 @@ fn wire_generate_from_template_impl( u32, Option, crate::llm::app_flowy_model::AttributeType, + Option, )>>::sse_decode(&mut deserializer); + let api_enable_plugin = ::sse_decode(&mut deserializer); deserializer.end(); move |context| { transform_result_sse((move || { - Result::<_, ()>::Ok(crate::api::llm_api::generate_from_template(api_v)) + Result::<_, ()>::Ok(crate::api::llm_api::generate_from_template( + api_v, + api_enable_plugin, + )) })()) } }, @@ -1054,8 +1059,8 @@ impl SseDecode for crate::llm::app_flowy_model::AttributeType { impl SseDecode for crate::llm::app_flowy_model::Attributes { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { - let mut var_bold = ::sse_decode(deserializer); - let mut var_italic = ::sse_decode(deserializer); + let mut var_bold = >::sse_decode(deserializer); + let mut var_italic = >::sse_decode(deserializer); let mut var_file = >::sse_decode(deserializer); let mut var_sql = >::sse_decode(deserializer); return crate::llm::app_flowy_model::Attributes { @@ -1337,15 +1342,23 @@ impl SseDecode for Vec<(i64, String)> { } } -impl SseDecode for Vec<(String, crate::llm::app_flowy_model::AttributeType)> { +impl SseDecode + for Vec<( + String, + crate::llm::app_flowy_model::AttributeType, + Option, + )> +{ // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { let mut len_ = ::sse_decode(deserializer); let mut ans_ = vec![]; for idx_ in 0..len_ { - ans_.push( - <(String, crate::llm::app_flowy_model::AttributeType)>::sse_decode(deserializer), - ); + ans_.push(<( + String, + crate::llm::app_flowy_model::AttributeType, + Option, + )>::sse_decode(deserializer)); } return ans_; } @@ -1383,6 +1396,7 @@ impl SseDecode u32, Option, crate::llm::app_flowy_model::AttributeType, + Option, )> { // Codec=Sse (Serialization based), see doc to use other codecs @@ -1395,6 +1409,7 @@ impl SseDecode u32, Option, crate::llm::app_flowy_model::AttributeType, + Option, )>::sse_decode(deserializer)); } return ans_; @@ -1559,6 +1574,17 @@ impl SseDecode for Option { } } +impl SseDecode for Option { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { + if (::sse_decode(deserializer)) { + return Some(::sse_decode(deserializer)); + } else { + return None; + } + } +} + impl SseDecode for Option { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { @@ -1724,12 +1750,19 @@ impl SseDecode for (Vec, String) { } } -impl SseDecode for (String, crate::llm::app_flowy_model::AttributeType) { +impl SseDecode + for ( + String, + crate::llm::app_flowy_model::AttributeType, + Option, + ) +{ // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { let mut var_field0 = ::sse_decode(deserializer); let mut var_field1 = ::sse_decode(deserializer); - return (var_field0, var_field1); + let mut var_field2 = >::sse_decode(deserializer); + return (var_field0, var_field1, var_field2); } } @@ -1758,6 +1791,7 @@ impl SseDecode u32, Option, crate::llm::app_flowy_model::AttributeType, + Option, ) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -1766,7 +1800,8 @@ impl SseDecode let mut var_field1 = ::sse_decode(deserializer); let mut var_field2 = >::sse_decode(deserializer); let mut var_field3 = ::sse_decode(deserializer); - return (var_field0, var_field1, var_field2, var_field3); + let mut var_field4 = >::sse_decode(deserializer); + return (var_field0, var_field1, var_field2, var_field3, var_field4); } } @@ -2561,8 +2596,8 @@ impl SseEncode for crate::llm::app_flowy_model::AttributeType { impl SseEncode for crate::llm::app_flowy_model::Attributes { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { - ::sse_encode(self.bold, serializer); - ::sse_encode(self.italic, serializer); + >::sse_encode(self.bold, serializer); + >::sse_encode(self.italic, serializer); >::sse_encode(self.file, serializer); >::sse_encode(self.sql, serializer); } @@ -2774,12 +2809,22 @@ impl SseEncode for Vec<(i64, String)> { } } -impl SseEncode for Vec<(String, crate::llm::app_flowy_model::AttributeType)> { +impl SseEncode + for Vec<( + String, + crate::llm::app_flowy_model::AttributeType, + Option, + )> +{ // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { ::sse_encode(self.len() as _, serializer); for item in self { - <(String, crate::llm::app_flowy_model::AttributeType)>::sse_encode(item, serializer); + <( + String, + crate::llm::app_flowy_model::AttributeType, + Option, + )>::sse_encode(item, serializer); } } } @@ -2810,6 +2855,7 @@ impl SseEncode u32, Option, crate::llm::app_flowy_model::AttributeType, + Option, )> { // Codec=Sse (Serialization based), see doc to use other codecs @@ -2821,6 +2867,7 @@ impl SseEncode u32, Option, crate::llm::app_flowy_model::AttributeType, + Option, )>::sse_encode(item, serializer); } } @@ -2938,6 +2985,16 @@ impl SseEncode for Option { } } +impl SseEncode for Option { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { + ::sse_encode(self.is_some(), serializer); + if let Some(value) = self { + ::sse_encode(value, serializer); + } + } +} + impl SseEncode for Option { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { @@ -3074,11 +3131,18 @@ impl SseEncode for (Vec, String) { } } -impl SseEncode for (String, crate::llm::app_flowy_model::AttributeType) { +impl SseEncode + for ( + String, + crate::llm::app_flowy_model::AttributeType, + Option, + ) +{ // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { ::sse_encode(self.0, serializer); ::sse_encode(self.1, serializer); + >::sse_encode(self.2, serializer); } } @@ -3104,6 +3168,7 @@ impl SseEncode u32, Option, crate::llm::app_flowy_model::AttributeType, + Option, ) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -3112,6 +3177,7 @@ impl SseEncode ::sse_encode(self.1, serializer); >::sse_encode(self.2, serializer); ::sse_encode(self.3, serializer); + >::sse_encode(self.4, serializer); } } diff --git a/rust/src/llm/app_flowy_model.rs b/rust/src/llm/app_flowy_model.rs index 8fd6324..c2a5745 100644 --- a/rust/src/llm/app_flowy_model.rs +++ b/rust/src/llm/app_flowy_model.rs @@ -44,8 +44,8 @@ pub struct Delum { #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Attributes { - pub bold: bool, - pub italic: bool, + pub bold: Option, + pub italic: Option, pub file: Option, pub sql: Option, } @@ -60,15 +60,24 @@ pub fn doc_to_str(doc: &Root) -> anyhow::Result { anyhow::Ok(s) } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum AttributeType { Prompt, File, Sql, } -pub fn get_all_cadidates(s: String) -> anyhow::Result> { - let mut v: Vec<(String, AttributeType)> = Vec::new(); +pub fn get_all_cadidates( + s: String, +) -> anyhow::Result< + Vec<( + /*prompt*/ String, + /*type*/ AttributeType, + /*sql,filepath,...*/ Option, + )>, +> { + let mut v: Vec<(String, AttributeType, Option)> = Vec::new(); + // println!("{:?}", s); let root = str_to_doc(s)?; let re = Regex::new(r"\{\{(.*?)\}\}").unwrap(); let doc = root.document; @@ -79,17 +88,27 @@ pub fn get_all_cadidates(s: String) -> anyhow::Result anyhow::Result { let params = ENV_PARAMS.read().unwrap(); @@ -32,13 +58,26 @@ pub async fn get_file_content(p: String) -> anyhow::Result { ); let response = client - .post(_p.base + "/files") - .header("Authorization", format!("Bearer {}", _p.sk.unwrap())) + .post(_p.base.clone() + "/files") + .header( + "Authorization", + format!("Bearer {}", _p.sk.clone().unwrap()), + ) .multipart(form) .send() .await?; - return Ok(response.text().await?); + let res: UploadFileResponse = response.json().await?; + let file_content_response = client + .get(_p.base + "/files/" + &res.id.clone() + "/content") + .header("Authorization", format!("Bearer {}", _p.sk.unwrap())) + .send() + .await?; + + return Ok(file_content_response + .json::() + .await? + .content); } None => anyhow::bail!("open ai client is None"), } @@ -49,7 +88,7 @@ mod tests { async fn test_read_file() { crate::llm::init("env".to_owned()); let s = crate::llm::plugins::chat_file::get_file_content( - r"C:\Users\xiaoshuyui\Desktop\json.md".to_owned(), + r"d:\Desktop\个保法方案.pdf".to_owned(), ) .await; match s { diff --git a/rust/src/llm/template.rs b/rust/src/llm/template.rs index 5373269..0c964e9 100644 --- a/rust/src/llm/template.rs +++ b/rust/src/llm/template.rs @@ -13,7 +13,7 @@ use langchain_rust::{ use crate::frb_generated::StreamSink; -use super::{app_flowy_model::AttributeType, OPENAI}; +use super::{app_flowy_model::AttributeType, plugins::chat_file::get_file_content, OPENAI}; pub enum TemplateRunningStage { Format, @@ -32,21 +32,23 @@ pub struct TemplateItem { pub index: u32, pub next: Option, pub attr_type: AttributeType, + pub extra: Option, } impl TemplateItem { - pub fn from(i: (String, u32, Option, AttributeType)) -> Self { + pub fn from(i: (String, u32, Option, AttributeType, Option)) -> Self { Self { prompt: i.0, index: i.1, next: i.2, attr_type: i.3, + extra: i.4, } } } pub fn generate_template_items_from_list( - list: Vec<(String, u32, Option, AttributeType)>, + list: Vec<(String, u32, Option, AttributeType, Option)>, ) -> Vec { let mut v = Vec::new(); for i in list { @@ -224,7 +226,7 @@ impl AppFlowyTemplate { if !enable_plugin { s = items_to_chain(&i, open_ai.clone()); } else { - s = items_to_chain_with_plugins(&i, open_ai.clone()); + s = items_to_chain_with_plugins(&i, open_ai.clone()).await; } Self::execute_worker(s, i).await; @@ -392,10 +394,85 @@ fn items_to_chain( (None, None) } -fn items_to_chain_with_plugins( +/// TODO: 暂时sql和文件问答不支持顺序链 +async fn items_to_chain_with_plugins( items: &Vec<&TemplateItem>, llm: OpenAI, ) -> (Option>, Option) { + if items.is_empty() { + return (None, None); + } + + if items.len() == 1 { + let mut extra_content = "".to_owned(); + let item = items.get(0).unwrap(); + // sql + + // file + if item.attr_type == AttributeType::File { + let file_content = get_file_content(item.extra.clone().unwrap_or("".to_owned())).await; + match file_content { + Ok(_f) => { + extra_content = _f; + } + Err(_e) => { + println!("[rust-error] file content is None"); + return (None, None); + } + } + } + + // 返回一般的LLMChain + let prompt = HumanMessagePromptTemplate::new(PromptTemplate::new( + items.first().unwrap().prompt.clone() + &extra_content, + vec![], + TemplateFormat::FString, + )); + let normal_chain = LLMChainBuilder::new() + .prompt(prompt) + .llm(llm.clone()) + .build() + .unwrap(); + return (Some(Box::new(normal_chain)), None); + } + + if items.len() > 1 { + // let mut chains: Vec = Vec::new(); + let mut seq_chain_builder = SequentialChainBuilder::new(); + let mut i = 0; + while i < items.len() { + let input = format!("input{}", i); + let output = format!("input{}", i + 1); + let prompt_str; + if i == 0 { + prompt_str = + "请根据以下要求,帮我生成对应的文案。 {{".to_owned() + &input + "}}"; + } else { + prompt_str = "请根据以下内容和额外要求,帮我生成对应的文案。内容: {{".to_owned() + + &input + + "}}, 额外要求: " + + &items.get(i).unwrap().prompt; + } + + let prompt = HumanMessagePromptTemplate::new(template_jinja2!(prompt_str, input)); + let c = LLMChainBuilder::new() + .prompt(prompt) + .llm(llm.clone()) + .output_key(output) + .build() + .unwrap(); + // chains.push(c); + seq_chain_builder = seq_chain_builder.add_chain(c); + i += 1; + } + + let seq_chain = seq_chain_builder.build(); + return ( + Some(Box::new(seq_chain)), + Some(items.first().unwrap().prompt.clone()), + ); + } + (None, None) } @@ -426,12 +503,14 @@ mod tests { index: 1, next: Some(2), attr_type: AttributeType::Prompt, + extra: None, }, TemplateItem { prompt: "请帮我分析学习计划中的难点".to_string(), index: 2, next: Some(3), attr_type: AttributeType::Prompt, + extra: None, }, ], }; @@ -505,6 +584,7 @@ mod tests { index: 1, next: None, attr_type: AttributeType::Prompt, + extra: None, }], }; @@ -563,36 +643,42 @@ mod tests { index: 1, next: Some(2), attr_type: AttributeType::Prompt, + extra: None, }, TemplateItem { prompt: "Second".to_string(), index: 2, next: Some(3), attr_type: AttributeType::Prompt, + extra: None, }, TemplateItem { prompt: "Third".to_string(), index: 3, next: None, attr_type: AttributeType::Prompt, + extra: None, }, TemplateItem { prompt: "4".to_string(), index: 4, next: Some(5), attr_type: AttributeType::Prompt, + extra: None, }, TemplateItem { prompt: "5".to_string(), index: 5, next: None, attr_type: AttributeType::Prompt, + extra: None, }, TemplateItem { prompt: "6".to_string(), index: 6, next: None, attr_type: AttributeType::Prompt, + extra: None, }, ], };