@@ -13,6 +13,7 @@ library dart_bridge_rag;
1313import 'dart:convert' ;
1414import 'dart:ffi' ;
1515import 'dart:io' ;
16+ import 'dart:isolate' ;
1617
1718import 'package:ffi/ffi.dart' ;
1819
@@ -333,4 +334,152 @@ class DartBridgeRAG {
333334 throw StateError ('RAG pipeline not created. Call createPipeline() first.' );
334335 }
335336 }
337+
338+ /// Create pipeline on a background isolate.
339+ Future <void > createPipelineAsync (RAGConfiguration config) async {
340+ final jsonStr = jsonEncode (config.toJson ());
341+ _logger.debug ('createPipelineAsync config: $jsonStr ' );
342+
343+ final result = await Isolate .run (() => _isolateCreatePipeline (jsonStr));
344+ if (result != 0 ) {
345+ final detail = _getLastError ();
346+ final msg = detail != null
347+ ? 'RAG pipeline creation failed (code $result ): $detail '
348+ : 'RAG pipeline creation failed (code $result )' ;
349+ _logger.error (msg);
350+ throw Exception (msg);
351+ }
352+
353+ _isCreated = true ;
354+ _registered = true ;
355+ _logger.debug ('RAG pipeline created (async)' );
356+ }
357+
358+ Future <void > addDocumentAsync (String text, {String ? metadataJson}) async {
359+ _ensurePipeline ();
360+ _logger.debug ('addDocumentAsync: ${text .length } chars' );
361+
362+ final result = await Isolate .run (
363+ () => _isolateAddDocument (text, metadataJson),
364+ );
365+ if (result != 0 ) {
366+ throw Exception ('Failed to add document: error $result ' );
367+ }
368+ }
369+
370+ Future <void > addDocumentsBatchAsync (
371+ List <Map <String , String >> documents,
372+ ) async {
373+ _ensurePipeline ();
374+
375+ final jsonStr = jsonEncode (documents);
376+ final result = await Isolate .run (
377+ () => _isolateAddDocumentsBatch (jsonStr),
378+ );
379+ if (result != 0 ) {
380+ throw Exception ('Failed to add documents batch: error $result ' );
381+ }
382+ }
383+
384+ Future <RAGResult > queryAsync (RAGQueryOptions options) async {
385+ _ensurePipeline ();
386+
387+ final jsonStr = jsonEncode (options.toJson ());
388+ final resultJson = await Isolate .run (
389+ () => _isolateQuery (jsonStr),
390+ );
391+
392+ final decoded = jsonDecode (resultJson) as Map <String , dynamic >;
393+ return RAGResult .fromJson (decoded);
394+ }
395+ }
396+
397+ DynamicLibrary _openBridgeLib () {
398+ if (Platform .isIOS) {
399+ return DynamicLibrary .executable ();
400+ } else if (Platform .isAndroid) {
401+ try {
402+ return DynamicLibrary .open ('libflutter_rag_bridge.so' );
403+ } catch (_) {
404+ return DynamicLibrary .open ('librac_commons.so' );
405+ }
406+ } else {
407+ return DynamicLibrary .process ();
408+ }
409+ }
410+
411+ DynamicLibrary _openCommonsLib () {
412+ if (Platform .isIOS) {
413+ return DynamicLibrary .executable ();
414+ } else if (Platform .isAndroid) {
415+ return DynamicLibrary .open ('librac_commons.so' );
416+ } else {
417+ return DynamicLibrary .process ();
418+ }
419+ }
420+
421+ int _isolateCreatePipeline (String configJson) {
422+ final commons = _openCommonsLib ();
423+ final registerFn =
424+ commons.lookupFunction <_RagRegisterNative , _RagRegisterDart >(
425+ 'rac_backend_rag_register' );
426+ registerFn ();
427+
428+ final lib = _openBridgeLib ();
429+ final fn = lib.lookupFunction< _CreatePipelineJsonNative ,
430+ _CreatePipelineJsonDart > ('flutter_rag_create_pipeline_json' );
431+
432+ final cStr = configJson.toNativeUtf8 ();
433+ try {
434+ return fn (cStr);
435+ } finally {
436+ calloc.free (cStr);
437+ }
438+ }
439+
440+ int _isolateAddDocument (String text, String ? metadataJson) {
441+ final lib = _openBridgeLib ();
442+ final fn = lib.lookupFunction <_AddDocumentNative , _AddDocumentDart >(
443+ 'flutter_rag_add_document' );
444+
445+ final cText = text.toNativeUtf8 ();
446+ final cMeta = metadataJson != null ? metadataJson.toNativeUtf8 () : nullptr;
447+
448+ try {
449+ return fn (cText, cMeta);
450+ } finally {
451+ calloc.free (cText);
452+ if (cMeta != nullptr) calloc.free (cMeta);
453+ }
454+ }
455+
456+ int _isolateAddDocumentsBatch (String documentsJson) {
457+ final lib = _openBridgeLib ();
458+ final fn = lib.lookupFunction< _AddDocumentsBatchJsonNative ,
459+ _AddDocumentsBatchJsonDart > ('flutter_rag_add_documents_batch_json' );
460+
461+ final cStr = documentsJson.toNativeUtf8 ();
462+ try {
463+ return fn (cStr);
464+ } finally {
465+ calloc.free (cStr);
466+ }
467+ }
468+
469+ String _isolateQuery (String queryJson) {
470+ final lib = _openBridgeLib ();
471+ final queryFn = lib.lookupFunction <_QueryJsonNative , _QueryJsonDart >(
472+ 'flutter_rag_query_json' );
473+ final freeFn = lib.lookupFunction <_FreeStringNative , _FreeStringDart >(
474+ 'flutter_rag_free_string' );
475+
476+ final cStr = queryJson.toNativeUtf8 ();
477+ try {
478+ final resultPtr = queryFn (cStr);
479+ final resultJson = resultPtr.toDartString ();
480+ freeFn (resultPtr);
481+ return resultJson;
482+ } finally {
483+ calloc.free (cStr);
484+ }
336485}
0 commit comments