Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:ffi/ffi.dart';

import 'package:runanywhere/foundation/logging/sdk_logger.dart';
import 'package:runanywhere/native/ffi_types.dart';
import 'package:runanywhere/native/native_functions.dart';
import 'package:runanywhere/native/platform_loader.dart';

/// LLM component bridge for C++ interop.
Expand Down Expand Up @@ -78,13 +79,9 @@ class DartBridgeLLM {
}

try {
final lib = PlatformLoader.loadCommons();
final create = lib.lookupFunction<Int32 Function(Pointer<RacHandle>),
int Function(Pointer<RacHandle>)>('rac_llm_component_create');

final handlePtr = calloc<RacHandle>();
try {
final result = create(handlePtr);
final result = NativeFunctions.llmCreate(handlePtr);

if (result != RAC_SUCCESS) {
throw StateError(
Expand All @@ -111,11 +108,7 @@ class DartBridgeLLM {
if (_handle == null) return false;

try {
final lib = PlatformLoader.loadCommons();
final isLoadedFn = lib.lookupFunction<Int32 Function(RacHandle),
int Function(RacHandle)>('rac_llm_component_is_loaded');

return isLoadedFn(_handle!) == RAC_TRUE;
return NativeFunctions.llmIsLoaded(_handle!) == RAC_TRUE;
} catch (e) {
_logger.debug('isLoaded check failed: $e');
return false;
Expand All @@ -130,11 +123,7 @@ class DartBridgeLLM {
if (_handle == null) return false;

try {
final lib = PlatformLoader.loadCommons();
final supportsStreamingFn = lib.lookupFunction<Int32 Function(RacHandle),
int Function(RacHandle)>('rac_llm_component_supports_streaming');

return supportsStreamingFn(_handle!) == RAC_TRUE;
return NativeFunctions.llmSupportsStreaming(_handle!) == RAC_TRUE;
} catch (e) {
return false;
}
Expand All @@ -161,16 +150,8 @@ class DartBridgeLLM {
final namePtr = modelName.toNativeUtf8();

try {
final lib = PlatformLoader.loadCommons();
final loadModelFn = lib.lookupFunction<
Int32 Function(
RacHandle, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>),
int Function(RacHandle, Pointer<Utf8>, Pointer<Utf8>,
Pointer<Utf8>)>('rac_llm_component_load_model');

_logger.debug(
'Calling rac_llm_component_load_model with handle: $_handle, path: $modelPath');
final result = loadModelFn(handle, pathPtr, idPtr, namePtr);
_logger.debug('Calling rac_llm_component_load_model with handle=$handle');
final result = NativeFunctions.llmLoadModel(handle, pathPtr, idPtr, namePtr);
_logger.debug(
'rac_llm_component_load_model returned: $result (${RacResultCode.getMessage(result)})');

Expand All @@ -194,11 +175,7 @@ class DartBridgeLLM {
if (_handle == null) return;

try {
final lib = PlatformLoader.loadCommons();
final cleanupFn = lib.lookupFunction<Int32 Function(RacHandle),
int Function(RacHandle)>('rac_llm_component_cleanup');

cleanupFn(_handle!);
NativeFunctions.llmCleanup(_handle!);
_loadedModelId = null;
_logger.info('LLM model unloaded');
} catch (e) {
Expand All @@ -211,11 +188,7 @@ class DartBridgeLLM {
if (_handle == null) return;

try {
final lib = PlatformLoader.loadCommons();
final cancelFn = lib.lookupFunction<Int32 Function(RacHandle),
int Function(RacHandle)>('rac_llm_component_cancel');

cancelFn(_handle!);
NativeFunctions.llmCancel(_handle!);
_logger.debug('LLM generation cancelled');
} catch (e) {
_logger.error('Failed to cancel generation: $e');
Expand Down Expand Up @@ -373,11 +346,7 @@ class DartBridgeLLM {
void destroy() {
if (_handle != null) {
try {
final lib = PlatformLoader.loadCommons();
final destroyFn = lib.lookupFunction<Void Function(RacHandle),
void Function(RacHandle)>('rac_llm_component_destroy');

destroyFn(_handle!);
NativeFunctions.llmDestroy(_handle!);
_handle = null;
_loadedModelId = null;
_logger.debug('LLM component destroyed');
Expand Down Expand Up @@ -486,7 +455,7 @@ void _streamingIsolateEntry(_StreamingIsolateParams params) {
// Set systemPrompt if provided
if (params.systemPrompt != null && params.systemPrompt!.isNotEmpty) {
systemPromptPtr = params.systemPrompt!.toNativeUtf8();
optionsPtr.ref.systemPrompt = systemPromptPtr!;
optionsPtr.ref.systemPrompt = systemPromptPtr;
} else {
optionsPtr.ref.systemPrompt = nullptr;
}
Expand Down Expand Up @@ -553,7 +522,7 @@ void _streamingIsolateEntry(_StreamingIsolateParams params) {
calloc.free(promptPtr);
calloc.free(optionsPtr);
if (systemPromptPtr != null) {
calloc.free(systemPromptPtr!);
calloc.free(systemPromptPtr);
}
_isolateSendPort = null;
}
Expand Down Expand Up @@ -622,7 +591,7 @@ _IsolateGenerationResult _generateInIsolate(
// Set systemPrompt if provided
if (systemPrompt != null && systemPrompt.isNotEmpty) {
systemPromptPtr = systemPrompt.toNativeUtf8();
optionsPtr.ref.systemPrompt = systemPromptPtr!;
optionsPtr.ref.systemPrompt = systemPromptPtr;
} else {
optionsPtr.ref.systemPrompt = nullptr;
}
Expand Down Expand Up @@ -658,7 +627,7 @@ _IsolateGenerationResult _generateInIsolate(
calloc.free(optionsPtr);
calloc.free(resultPtr);
if (systemPromptPtr != null) {
calloc.free(systemPromptPtr!);
calloc.free(systemPromptPtr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:ffi/ffi.dart';

import 'package:runanywhere/foundation/logging/sdk_logger.dart';
import 'package:runanywhere/native/ffi_types.dart';
import 'package:runanywhere/native/native_functions.dart';
import 'package:runanywhere/native/platform_loader.dart';

/// STT component bridge for C++ interop.
Expand Down Expand Up @@ -48,13 +49,9 @@ class DartBridgeSTT {
}

try {
final lib = PlatformLoader.loadCommons();
final create = lib.lookupFunction<Int32 Function(Pointer<RacHandle>),
int Function(Pointer<RacHandle>)>('rac_stt_component_create');

final handlePtr = calloc<RacHandle>();
try {
final result = create(handlePtr);
final result = NativeFunctions.sttCreate(handlePtr);

if (result != RAC_SUCCESS) {
throw StateError(
Expand All @@ -81,11 +78,7 @@ class DartBridgeSTT {
if (_handle == null) return false;

try {
final lib = PlatformLoader.loadCommons();
final isLoadedFn = lib.lookupFunction<Int32 Function(RacHandle),
int Function(RacHandle)>('rac_stt_component_is_loaded');

return isLoadedFn(_handle!) == RAC_TRUE;
return NativeFunctions.sttIsLoaded(_handle!) == RAC_TRUE;
} catch (e) {
_logger.debug('isLoaded check failed: $e');
return false;
Expand All @@ -100,11 +93,7 @@ class DartBridgeSTT {
if (_handle == null) return false;

try {
final lib = PlatformLoader.loadCommons();
final supportsStreamingFn = lib.lookupFunction<Int32 Function(RacHandle),
int Function(RacHandle)>('rac_stt_component_supports_streaming');

return supportsStreamingFn(_handle!) == RAC_TRUE;
return NativeFunctions.sttSupportsStreaming(_handle!) == RAC_TRUE;
} catch (e) {
return false;
}
Expand All @@ -131,14 +120,7 @@ class DartBridgeSTT {
final namePtr = modelName.toNativeUtf8();

try {
final lib = PlatformLoader.loadCommons();
final loadModelFn = lib.lookupFunction<
Int32 Function(
RacHandle, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>),
int Function(RacHandle, Pointer<Utf8>, Pointer<Utf8>,
Pointer<Utf8>)>('rac_stt_component_load_model');

final result = loadModelFn(handle, pathPtr, idPtr, namePtr);
final result = NativeFunctions.sttLoadModel(handle, pathPtr, idPtr, namePtr);

if (result != RAC_SUCCESS) {
throw StateError(
Expand All @@ -160,11 +142,7 @@ class DartBridgeSTT {
if (_handle == null) return;

try {
final lib = PlatformLoader.loadCommons();
final cleanupFn = lib.lookupFunction<Int32 Function(RacHandle),
int Function(RacHandle)>('rac_stt_component_cleanup');

cleanupFn(_handle!);
NativeFunctions.sttCleanup(_handle!);
_loadedModelId = null;
_logger.info('STT model unloaded');
} catch (e) {
Expand Down Expand Up @@ -357,11 +335,7 @@ class DartBridgeSTT {
void destroy() {
if (_handle != null) {
try {
final lib = PlatformLoader.loadCommons();
final destroyFn = lib.lookupFunction<Void Function(RacHandle),
void Function(RacHandle)>('rac_stt_component_destroy');

destroyFn(_handle!);
NativeFunctions.sttDestroy(_handle!);
_handle = null;
_loadedModelId = null;
_logger.debug('STT component destroyed');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:ffi/ffi.dart';

import 'package:runanywhere/foundation/logging/sdk_logger.dart';
import 'package:runanywhere/native/ffi_types.dart';
import 'package:runanywhere/native/native_functions.dart';
import 'package:runanywhere/native/platform_loader.dart';

/// TTS component bridge for C++ interop.
Expand Down Expand Up @@ -48,13 +49,9 @@ class DartBridgeTTS {
}

try {
final lib = PlatformLoader.loadCommons();
final create = lib.lookupFunction<Int32 Function(Pointer<RacHandle>),
int Function(Pointer<RacHandle>)>('rac_tts_component_create');

final handlePtr = calloc<RacHandle>();
try {
final result = create(handlePtr);
final result = NativeFunctions.ttsCreate(handlePtr);

if (result != RAC_SUCCESS) {
throw StateError(
Expand All @@ -81,11 +78,7 @@ class DartBridgeTTS {
if (_handle == null) return false;

try {
final lib = PlatformLoader.loadCommons();
final isLoadedFn = lib.lookupFunction<Int32 Function(RacHandle),
int Function(RacHandle)>('rac_tts_component_is_loaded');

return isLoadedFn(_handle!) == RAC_TRUE;
return NativeFunctions.ttsIsLoaded(_handle!) == RAC_TRUE;
} catch (e) {
_logger.debug('isLoaded check failed: $e');
return false;
Expand Down Expand Up @@ -116,14 +109,7 @@ class DartBridgeTTS {
final namePtr = voiceName.toNativeUtf8();

try {
final lib = PlatformLoader.loadCommons();
final loadVoiceFn = lib.lookupFunction<
Int32 Function(
RacHandle, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>),
int Function(RacHandle, Pointer<Utf8>, Pointer<Utf8>,
Pointer<Utf8>)>('rac_tts_component_load_voice');

final result = loadVoiceFn(handle, pathPtr, idPtr, namePtr);
final result = NativeFunctions.ttsLoadVoice(handle, pathPtr, idPtr, namePtr);

if (result != RAC_SUCCESS) {
throw StateError(
Expand All @@ -145,11 +131,7 @@ class DartBridgeTTS {
if (_handle == null) return;

try {
final lib = PlatformLoader.loadCommons();
final cleanupFn = lib.lookupFunction<Int32 Function(RacHandle),
int Function(RacHandle)>('rac_tts_component_cleanup');

cleanupFn(_handle!);
NativeFunctions.ttsCleanup(_handle!);
_loadedVoiceId = null;
_logger.info('TTS voice unloaded');
} catch (e) {
Expand All @@ -162,11 +144,7 @@ class DartBridgeTTS {
if (_handle == null) return;

try {
final lib = PlatformLoader.loadCommons();
final stopFn = lib.lookupFunction<Int32 Function(RacHandle),
int Function(RacHandle)>('rac_tts_component_stop');

stopFn(_handle!);
NativeFunctions.ttsStop(_handle!);
_logger.debug('TTS synthesis stopped');
} catch (e) {
_logger.error('Failed to stop TTS: $e');
Expand Down Expand Up @@ -335,11 +313,7 @@ class DartBridgeTTS {
void destroy() {
if (_handle != null) {
try {
final lib = PlatformLoader.loadCommons();
final destroyFn = lib.lookupFunction<Void Function(RacHandle),
void Function(RacHandle)>('rac_tts_component_destroy');

destroyFn(_handle!);
NativeFunctions.ttsDestroy(_handle!);
_handle = null;
_loadedVoiceId = null;
_logger.debug('TTS component destroyed');
Expand Down
Loading