react-native-executorch 0.5.6 → 0.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/libs/classes.jar +0 -0
- package/android/src/main/cpp/CMakeLists.txt +23 -14
- package/common/rnexecutorch/RnExecutorchInstaller.cpp +4 -21
- package/common/rnexecutorch/host_objects/ModelHostObject.h +67 -51
- package/common/rnexecutorch/models/llm/LLM.cpp +24 -1
- package/common/rnexecutorch/models/llm/LLM.h +4 -1
- package/common/rnexecutorch/threads/GlobalThreadPool.h +79 -0
- package/common/rnexecutorch/threads/HighPerformanceThreadPool.h +364 -0
- package/common/rnexecutorch/threads/utils/ThreadUtils.h +29 -0
- package/common/runner/runner.cpp +9 -3
- package/common/runner/runner.h +4 -3
- package/common/runner/text_token_generator.h +28 -10
- package/lib/Error.js +53 -0
- package/lib/ThreadPool.d.ts +10 -0
- package/lib/ThreadPool.js +28 -0
- package/lib/common/Logger.d.ts +8 -0
- package/lib/common/Logger.js +19 -0
- package/lib/constants/directories.js +2 -0
- package/lib/constants/llmDefaults.d.ts +6 -0
- package/lib/constants/llmDefaults.js +16 -0
- package/lib/constants/modelUrls.d.ts +223 -0
- package/lib/constants/modelUrls.js +322 -0
- package/lib/constants/ocr/models.d.ts +882 -0
- package/lib/constants/ocr/models.js +182 -0
- package/lib/constants/ocr/symbols.js +139 -0
- package/lib/constants/sttDefaults.d.ts +28 -0
- package/lib/constants/sttDefaults.js +68 -0
- package/lib/controllers/LLMController.d.ts +47 -0
- package/lib/controllers/LLMController.js +213 -0
- package/lib/controllers/OCRController.js +67 -0
- package/lib/controllers/SpeechToTextController.d.ts +56 -0
- package/lib/controllers/SpeechToTextController.js +349 -0
- package/lib/controllers/VerticalOCRController.js +70 -0
- package/lib/hooks/computer_vision/useClassification.d.ts +15 -0
- package/lib/hooks/computer_vision/useClassification.js +7 -0
- package/lib/hooks/computer_vision/useImageEmbeddings.d.ts +15 -0
- package/lib/hooks/computer_vision/useImageEmbeddings.js +7 -0
- package/lib/hooks/computer_vision/useImageSegmentation.d.ts +38 -0
- package/lib/hooks/computer_vision/useImageSegmentation.js +7 -0
- package/lib/hooks/computer_vision/useOCR.d.ts +20 -0
- package/lib/hooks/computer_vision/useOCR.js +41 -0
- package/lib/hooks/computer_vision/useObjectDetection.d.ts +15 -0
- package/lib/hooks/computer_vision/useObjectDetection.js +7 -0
- package/lib/hooks/computer_vision/useStyleTransfer.d.ts +15 -0
- package/lib/hooks/computer_vision/useStyleTransfer.js +7 -0
- package/lib/hooks/computer_vision/useVerticalOCR.d.ts +21 -0
- package/lib/hooks/computer_vision/useVerticalOCR.js +43 -0
- package/lib/hooks/general/useExecutorchModule.d.ts +13 -0
- package/lib/hooks/general/useExecutorchModule.js +7 -0
- package/lib/hooks/natural_language_processing/useLLM.d.ts +10 -0
- package/lib/hooks/natural_language_processing/useLLM.js +78 -0
- package/lib/hooks/natural_language_processing/useSpeechToText.d.ts +27 -0
- package/lib/hooks/natural_language_processing/useSpeechToText.js +49 -0
- package/lib/hooks/natural_language_processing/useTextEmbeddings.d.ts +16 -0
- package/lib/hooks/natural_language_processing/useTextEmbeddings.js +7 -0
- package/lib/hooks/natural_language_processing/useTokenizer.d.ts +17 -0
- package/lib/hooks/natural_language_processing/useTokenizer.js +52 -0
- package/lib/hooks/useModule.js +45 -0
- package/lib/hooks/useNonStaticModule.d.ts +20 -0
- package/lib/hooks/useNonStaticModule.js +49 -0
- package/lib/index.d.ts +48 -0
- package/lib/index.js +58 -0
- package/lib/module/controllers/LLMController.js +21 -2
- package/lib/module/controllers/LLMController.js.map +1 -1
- package/lib/module/hooks/natural_language_processing/useLLM.js +6 -2
- package/lib/module/hooks/natural_language_processing/useLLM.js.map +1 -1
- package/lib/module/modules/natural_language_processing/LLMModule.js +7 -2
- package/lib/module/modules/natural_language_processing/LLMModule.js.map +1 -1
- package/lib/module/types/llm.js.map +1 -1
- package/lib/modules/BaseModule.js +25 -0
- package/lib/modules/BaseNonStaticModule.js +14 -0
- package/lib/modules/computer_vision/ClassificationModule.d.ts +8 -0
- package/lib/modules/computer_vision/ClassificationModule.js +17 -0
- package/lib/modules/computer_vision/ImageEmbeddingsModule.d.ts +8 -0
- package/lib/modules/computer_vision/ImageEmbeddingsModule.js +17 -0
- package/lib/modules/computer_vision/ImageSegmentationModule.d.ts +11 -0
- package/lib/modules/computer_vision/ImageSegmentationModule.js +27 -0
- package/lib/modules/computer_vision/OCRModule.d.ts +14 -0
- package/lib/modules/computer_vision/OCRModule.js +17 -0
- package/lib/modules/computer_vision/ObjectDetectionModule.d.ts +9 -0
- package/lib/modules/computer_vision/ObjectDetectionModule.js +17 -0
- package/lib/modules/computer_vision/StyleTransferModule.d.ts +8 -0
- package/lib/modules/computer_vision/StyleTransferModule.js +17 -0
- package/lib/modules/computer_vision/VerticalOCRModule.d.ts +14 -0
- package/lib/modules/computer_vision/VerticalOCRModule.js +19 -0
- package/lib/modules/general/ExecutorchModule.d.ts +7 -0
- package/lib/modules/general/ExecutorchModule.js +14 -0
- package/lib/modules/natural_language_processing/LLMModule.d.ts +28 -0
- package/lib/modules/natural_language_processing/LLMModule.js +45 -0
- package/lib/modules/natural_language_processing/SpeechToTextModule.d.ts +24 -0
- package/lib/modules/natural_language_processing/SpeechToTextModule.js +36 -0
- package/lib/modules/natural_language_processing/TextEmbeddingsModule.d.ts +9 -0
- package/lib/modules/natural_language_processing/TextEmbeddingsModule.js +21 -0
- package/lib/modules/natural_language_processing/TokenizerModule.d.ts +12 -0
- package/lib/modules/natural_language_processing/TokenizerModule.js +30 -0
- package/lib/native/NativeETInstaller.js +2 -0
- package/lib/native/NativeOCR.js +2 -0
- package/lib/native/NativeVerticalOCR.js +2 -0
- package/lib/native/RnExecutorchModules.d.ts +7 -0
- package/lib/native/RnExecutorchModules.js +18 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types/common.d.ts +32 -0
- package/lib/types/common.js +25 -0
- package/lib/types/imageSegmentation.js +26 -0
- package/lib/types/llm.d.ts +46 -0
- package/lib/types/llm.js +9 -0
- package/lib/types/objectDetection.js +94 -0
- package/lib/types/ocr.js +1 -0
- package/lib/types/stt.d.ts +94 -0
- package/lib/types/stt.js +85 -0
- package/lib/typescript/controllers/LLMController.d.ts +4 -2
- package/lib/typescript/controllers/LLMController.d.ts.map +1 -1
- package/lib/typescript/hooks/natural_language_processing/useLLM.d.ts.map +1 -1
- package/lib/typescript/modules/natural_language_processing/LLMModule.d.ts +4 -2
- package/lib/typescript/modules/natural_language_processing/LLMModule.d.ts.map +1 -1
- package/lib/typescript/types/llm.d.ts +7 -1
- package/lib/typescript/types/llm.d.ts.map +1 -1
- package/lib/utils/ResourceFetcher.d.ts +24 -0
- package/lib/utils/ResourceFetcher.js +305 -0
- package/lib/utils/ResourceFetcherUtils.d.ts +54 -0
- package/lib/utils/ResourceFetcherUtils.js +127 -0
- package/lib/utils/llm.d.ts +6 -0
- package/lib/utils/llm.js +72 -0
- package/lib/utils/stt.js +21 -0
- package/package.json +3 -1
- package/react-native-executorch.podspec +12 -31
- package/src/controllers/LLMController.ts +29 -5
- package/src/hooks/natural_language_processing/useLLM.ts +15 -1
- package/src/modules/natural_language_processing/LLMModule.ts +14 -2
- package/src/types/llm.ts +8 -0
- package/third-party/android/libs/cpuinfo/arm64-v8a/libcpuinfo.so +0 -0
- package/third-party/android/libs/executorch/arm64-v8a/libexecutorch.so +0 -0
- package/third-party/android/libs/executorch/x86_64/libexecutorch.so +0 -0
- package/third-party/android/libs/pthreadpool/arm64-v8a/libpthreadpool.so +0 -0
- package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libsentencepiece.a +0 -0
- package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libtokenizers_c.a +0 -0
- package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libtokenizers_cpp.a +0 -0
- package/third-party/android/libs/tokenizers-cpp/x86_64/libsentencepiece.a +0 -0
- package/third-party/android/libs/tokenizers-cpp/x86_64/libtokenizers_c.a +0 -0
- package/third-party/android/libs/tokenizers-cpp/x86_64/libtokenizers_cpp.a +0 -0
- package/third-party/include/c10/macros/Export.h +2 -86
- package/third-party/include/c10/macros/Macros.h +28 -5
- package/third-party/include/c10/util/BFloat16-inl.h +1 -4
- package/third-party/include/c10/util/BFloat16.h +5 -8
- package/third-party/include/c10/util/Half.h +5 -0
- package/third-party/include/c10/util/bit_cast.h +1 -1
- package/third-party/include/c10/util/complex.h +639 -0
- package/third-party/include/c10/util/complex_math.h +399 -0
- package/third-party/include/c10/util/complex_utils.h +41 -0
- package/third-party/include/c10/util/irange.h +2 -2
- package/third-party/include/c10/util/overflows.h +95 -0
- package/third-party/include/executorch/ExecuTorchError.h +75 -0
- package/third-party/include/executorch/ExecuTorchModule.h +115 -11
- package/third-party/include/executorch/ExecuTorchTensor.h +731 -51
- package/third-party/include/executorch/ExecuTorchValue.h +61 -9
- package/third-party/include/executorch/extension/kernel_util/make_boxed_from_unboxed_functor.h +181 -0
- package/third-party/include/executorch/extension/kernel_util/meta_programming.h +108 -0
- package/third-party/include/executorch/extension/kernel_util/type_list.h +137 -0
- package/third-party/include/executorch/extension/module/bundled_module.h +131 -0
- package/third-party/include/executorch/extension/module/module.h +46 -20
- package/third-party/include/executorch/extension/threadpool/cpuinfo_utils.h +1 -3
- package/third-party/include/executorch/extension/threadpool/threadpool.h +1 -3
- package/third-party/include/executorch/extension/threadpool/threadpool_guard.h +35 -0
- package/third-party/include/executorch/runtime/backend/backend_execution_context.h +3 -3
- package/third-party/include/executorch/runtime/backend/backend_init_context.h +12 -6
- package/third-party/include/executorch/runtime/backend/backend_option_context.h +34 -0
- package/third-party/include/executorch/runtime/backend/interface.h +70 -9
- package/third-party/include/executorch/runtime/backend/options.h +206 -0
- package/third-party/include/executorch/runtime/core/evalue.h +19 -25
- package/third-party/include/executorch/runtime/core/event_tracer.h +32 -17
- package/third-party/include/executorch/runtime/core/event_tracer_hooks.h +23 -14
- package/third-party/include/executorch/runtime/core/exec_aten/exec_aten.h +32 -9
- package/third-party/include/executorch/runtime/core/exec_aten/util/dim_order_util.h +3 -2
- package/third-party/include/executorch/runtime/core/exec_aten/util/scalar_type_util.h +43 -75
- package/third-party/include/executorch/runtime/core/exec_aten/util/tensor_util.h +88 -87
- package/third-party/include/executorch/runtime/core/function_ref.h +100 -0
- package/third-party/include/executorch/runtime/core/named_data_map.h +14 -14
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Export.h +2 -86
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Macros.h +28 -5
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16-inl.h +1 -4
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16.h +5 -8
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/Half.h +5 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/bit_cast.h +1 -1
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex.h +639 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex_math.h +399 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex_utils.h +41 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/irange.h +2 -2
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/overflows.h +95 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/macros/Export.h +88 -0
- package/third-party/include/executorch/runtime/core/portable_type/complex.h +6 -29
- package/third-party/include/executorch/runtime/core/portable_type/tensor_impl.h +20 -0
- package/third-party/include/executorch/runtime/core/span.h +4 -0
- package/third-party/include/executorch/runtime/core/tag.h +19 -0
- package/third-party/include/executorch/runtime/core/tensor_layout.h +2 -2
- package/third-party/include/executorch/runtime/executor/method.h +15 -3
- package/third-party/include/executorch/runtime/executor/method_meta.h +34 -5
- package/third-party/include/executorch/runtime/executor/program.h +3 -4
- package/third-party/include/executorch/runtime/executor/pte_data_map.h +9 -8
- package/third-party/include/executorch/runtime/executor/tensor_parser.h +14 -13
- package/third-party/include/executorch/runtime/kernel/kernel_runtime_context.h +5 -5
- package/third-party/include/executorch/runtime/kernel/operator_registry.h +21 -19
- package/third-party/include/executorch/runtime/platform/compiler.h +8 -0
- package/third-party/include/executorch/runtime/platform/platform.h +126 -0
- package/third-party/include/headeronly/macros/Export.h +88 -0
- package/third-party/include/tokenizers-cpp/tokenizers_c.h +61 -0
- package/third-party/include/torch/headeronly/macros/Export.h +88 -0
- package/third-party/ios/ExecutorchLib.xcframework/Info.plist +43 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/ExecutorchLib +0 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/Info.plist +0 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/ExecutorchLib +0 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/Info.plist +0 -0
- package/third-party/ios/libs/cpuinfo/libcpuinfo.a +0 -0
- package/third-party/ios/libs/pthreadpool/physical-arm64-release/libpthreadpool.a +0 -0
- package/third-party/ios/libs/pthreadpool/simulator-arm64-debug/libpthreadpool.a +0 -0
- package/ios/libs/executorch/libbackend_coreml_ios.a +0 -0
- package/ios/libs/executorch/libbackend_coreml_simulator.a +0 -0
- package/ios/libs/executorch/libbackend_mps_ios.a +0 -0
- package/ios/libs/executorch/libbackend_mps_simulator.a +0 -0
- package/ios/libs/executorch/libbackend_xnnpack_ios.a +0 -0
- package/ios/libs/executorch/libbackend_xnnpack_simulator.a +0 -0
- package/ios/libs/executorch/libexecutorch_ios.a +0 -0
- package/ios/libs/executorch/libexecutorch_simulator.a +0 -0
- package/ios/libs/executorch/libkernels_custom_ios.a +0 -0
- package/ios/libs/executorch/libkernels_custom_simulator.a +0 -0
- package/ios/libs/executorch/libkernels_optimized_ios.a +0 -0
- package/ios/libs/executorch/libkernels_optimized_simulator.a +0 -0
- package/ios/libs/executorch/libkernels_portable_ios.a +0 -0
- package/ios/libs/executorch/libkernels_portable_simulator.a +0 -0
- package/ios/libs/executorch/libkernels_quantized_ios.a +0 -0
- package/ios/libs/executorch/libkernels_quantized_simulator.a +0 -0
- package/third-party/ios/ios.toolchain.cmake +0 -1122
- /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libsentencepiece.a +0 -0
- /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libtokenizers_c.a +0 -0
- /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libtokenizers_cpp.a +0 -0
- /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libsentencepiece.a +0 -0
- /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libtokenizers_c.a +0 -0
- /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libtokenizers_cpp.a +0 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resource Fetcher
|
|
3
|
+
*
|
|
4
|
+
* Provides an interface for downloading files (via `ResourceFetcher.fetch()`)
|
|
5
|
+
*
|
|
6
|
+
* Key functionality:
|
|
7
|
+
* - Download control: pause, resume, and cancel operations through:
|
|
8
|
+
* - Single file: `.pauseFetching()`, `.resumeFetching()`, `.cancelFetching()`
|
|
9
|
+
* - Downloaded file management:
|
|
10
|
+
* - `.getFilesTotalSize()`, `.listDownloadedFiles()`, `.listDownloadedModels()`, `.deleteResources()`
|
|
11
|
+
*
|
|
12
|
+
* Remark: The pausing/resuming/canceling works only for fetching remote resources.
|
|
13
|
+
*
|
|
14
|
+
* Most exported functions accept:
|
|
15
|
+
* - Multiple `ResourceSource` arguments, (union type of string, number or object)
|
|
16
|
+
*
|
|
17
|
+
* Method `.fetch()` takes argument as callback that reports download progress.
|
|
18
|
+
* Method`.fetch()` returns array of paths to successfully saved files or null if the download was paused or cancelled (then resume functions can return paths).
|
|
19
|
+
*
|
|
20
|
+
* Technical Implementation:
|
|
21
|
+
* - Maintains a `downloads` Map instance that tracks:
|
|
22
|
+
* - Currently downloading resources
|
|
23
|
+
* - Paused downloads
|
|
24
|
+
* - Successful downloads are automatically removed from the `downloads` Map
|
|
25
|
+
* - Uses the `ResourceSourceExtended` interface to enable pause/resume functionality:
|
|
26
|
+
* - Wraps user-provided `ResourceSource` elements
|
|
27
|
+
* - Implements linked list behavior via the `.next` attribute
|
|
28
|
+
* - Automatically processes subsequent downloads when `.next` contains a valid resource
|
|
29
|
+
*/
|
|
30
|
+
import { cacheDirectory, createDownloadResumable, moveAsync, FileSystemSessionType, writeAsStringAsync, EncodingType, deleteAsync, readDirectoryAsync, } from 'expo-file-system';
|
|
31
|
+
import { Asset } from 'expo-asset';
|
|
32
|
+
import { RNEDirectory } from '../constants/directories';
|
|
33
|
+
import { ResourceFetcherUtils, } from './ResourceFetcherUtils';
|
|
34
|
+
export class ResourceFetcher {
|
|
35
|
+
static downloads = new Map(); //map of currently downloading (or paused) files, if the download was started by .fetch() method.
|
|
36
|
+
static async fetch(callback = () => { }, ...sources) {
|
|
37
|
+
if (sources.length === 0) {
|
|
38
|
+
throw new Error('Empty list given as an argument!');
|
|
39
|
+
}
|
|
40
|
+
const { results: info, totalLength } = await ResourceFetcherUtils.getFilesSizes(sources);
|
|
41
|
+
const head = {
|
|
42
|
+
source: info[0].source,
|
|
43
|
+
sourceType: info[0].type,
|
|
44
|
+
callback: info[0].type === 4 /* SourceType.REMOTE_FILE */
|
|
45
|
+
? ResourceFetcherUtils.calculateDownloadProgress(totalLength, info[0].previousFilesTotalLength, info[0].length, callback)
|
|
46
|
+
: () => { },
|
|
47
|
+
results: [],
|
|
48
|
+
};
|
|
49
|
+
let node = head;
|
|
50
|
+
for (let idx = 1; idx < sources.length; idx++) {
|
|
51
|
+
node.next = {
|
|
52
|
+
source: info[idx].source,
|
|
53
|
+
sourceType: info[idx].type,
|
|
54
|
+
callback: info[idx].type === 4 /* SourceType.REMOTE_FILE */
|
|
55
|
+
? ResourceFetcherUtils.calculateDownloadProgress(totalLength, info[idx].previousFilesTotalLength, info[idx].length, callback)
|
|
56
|
+
: () => { },
|
|
57
|
+
results: [],
|
|
58
|
+
};
|
|
59
|
+
node = node.next;
|
|
60
|
+
}
|
|
61
|
+
return this.singleFetch(head);
|
|
62
|
+
}
|
|
63
|
+
static async singleFetch(sourceExtended) {
|
|
64
|
+
const source = sourceExtended.source;
|
|
65
|
+
switch (sourceExtended.sourceType) {
|
|
66
|
+
case 0 /* SourceType.OBJECT */: {
|
|
67
|
+
return this.returnOrStartNext(sourceExtended, await this.handleObject(source));
|
|
68
|
+
}
|
|
69
|
+
case 1 /* SourceType.LOCAL_FILE */: {
|
|
70
|
+
return this.returnOrStartNext(sourceExtended, this.handleLocalFile(source));
|
|
71
|
+
}
|
|
72
|
+
case 2 /* SourceType.RELEASE_MODE_FILE */: {
|
|
73
|
+
return this.returnOrStartNext(sourceExtended, await this.handleReleaseModeFile(sourceExtended));
|
|
74
|
+
}
|
|
75
|
+
case 3 /* SourceType.DEV_MODE_FILE */: {
|
|
76
|
+
const result = await this.handleDevModeFile(sourceExtended);
|
|
77
|
+
if (result !== null) {
|
|
78
|
+
return this.returnOrStartNext(sourceExtended, result);
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
default: {
|
|
83
|
+
//case SourceType.REMOTE_FILE
|
|
84
|
+
const result = await this.handleRemoteFile(sourceExtended);
|
|
85
|
+
if (result !== null) {
|
|
86
|
+
return this.returnOrStartNext(sourceExtended, result);
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
//if any download ends successfully this function is called - it checks whether it should trigger next download or return list of paths.
|
|
93
|
+
static returnOrStartNext(sourceExtended, result) {
|
|
94
|
+
sourceExtended.results.push(result);
|
|
95
|
+
if (sourceExtended.next) {
|
|
96
|
+
const nextSource = sourceExtended.next;
|
|
97
|
+
nextSource.results.push(...sourceExtended.results);
|
|
98
|
+
return this.singleFetch(nextSource);
|
|
99
|
+
}
|
|
100
|
+
sourceExtended.callback(1);
|
|
101
|
+
return sourceExtended.results;
|
|
102
|
+
}
|
|
103
|
+
static async pause(source) {
|
|
104
|
+
const resource = this.downloads.get(source);
|
|
105
|
+
switch (resource.status) {
|
|
106
|
+
case 1 /* DownloadStatus.PAUSED */:
|
|
107
|
+
throw new Error("The file download is currently paused. Can't pause the download of the same file twice.");
|
|
108
|
+
default: {
|
|
109
|
+
resource.status = 1 /* DownloadStatus.PAUSED */;
|
|
110
|
+
await resource.downloadResumable.pauseAsync();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
static async resume(source) {
|
|
115
|
+
const resource = this.downloads.get(source);
|
|
116
|
+
if (!resource.extendedInfo.fileUri ||
|
|
117
|
+
!resource.extendedInfo.cacheFileUri ||
|
|
118
|
+
!resource.extendedInfo.uri) {
|
|
119
|
+
throw new Error('Something went wrong. File uri info is not specified!');
|
|
120
|
+
}
|
|
121
|
+
switch (resource.status) {
|
|
122
|
+
case 0 /* DownloadStatus.ONGOING */:
|
|
123
|
+
throw new Error("The file download is currently ongoing. Can't resume the ongoing download.");
|
|
124
|
+
default: {
|
|
125
|
+
resource.status = 0 /* DownloadStatus.ONGOING */;
|
|
126
|
+
const result = await resource.downloadResumable.resumeAsync();
|
|
127
|
+
if (!this.downloads.has(source) ||
|
|
128
|
+
this.downloads.get(source).status === 1 /* DownloadStatus.PAUSED */) {
|
|
129
|
+
//if canceled or paused after earlier resuming.
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
if (!result ||
|
|
133
|
+
(result.status !== 200 /* HTTP_CODE.OK */ &&
|
|
134
|
+
result.status !== 206 /* HTTP_CODE.PARTIAL_CONTENT */)) {
|
|
135
|
+
throw new Error(`Failed to fetch resource from '${resource.extendedInfo.uri}'`);
|
|
136
|
+
}
|
|
137
|
+
await moveAsync({
|
|
138
|
+
from: resource.extendedInfo.cacheFileUri,
|
|
139
|
+
to: resource.extendedInfo.fileUri,
|
|
140
|
+
});
|
|
141
|
+
this.downloads.delete(source);
|
|
142
|
+
ResourceFetcherUtils.triggerHuggingFaceDownloadCounter(resource.extendedInfo.uri);
|
|
143
|
+
return this.returnOrStartNext(resource.extendedInfo, ResourceFetcherUtils.removeFilePrefix(resource.extendedInfo.fileUri));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
static async cancel(source) {
|
|
148
|
+
const resource = this.downloads.get(source);
|
|
149
|
+
await resource.downloadResumable.cancelAsync();
|
|
150
|
+
this.downloads.delete(source);
|
|
151
|
+
}
|
|
152
|
+
static async pauseFetching(...sources) {
|
|
153
|
+
const source = this.findActive(sources);
|
|
154
|
+
await this.pause(source);
|
|
155
|
+
}
|
|
156
|
+
static async resumeFetching(...sources) {
|
|
157
|
+
const source = this.findActive(sources);
|
|
158
|
+
await this.resume(source);
|
|
159
|
+
}
|
|
160
|
+
static async cancelFetching(...sources) {
|
|
161
|
+
const source = this.findActive(sources);
|
|
162
|
+
await this.cancel(source);
|
|
163
|
+
}
|
|
164
|
+
static findActive(sources) {
|
|
165
|
+
for (const source of sources) {
|
|
166
|
+
if (this.downloads.has(source)) {
|
|
167
|
+
return source;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
throw new Error('None of given sources are currently during downloading process.');
|
|
171
|
+
}
|
|
172
|
+
static async listDownloadedFiles() {
|
|
173
|
+
const files = await readDirectoryAsync(RNEDirectory);
|
|
174
|
+
return files.map((file) => `${RNEDirectory}${file}`);
|
|
175
|
+
}
|
|
176
|
+
static async listDownloadedModels() {
|
|
177
|
+
const files = await this.listDownloadedFiles();
|
|
178
|
+
return files.filter((file) => file.endsWith('.pte'));
|
|
179
|
+
}
|
|
180
|
+
static async deleteResources(...sources) {
|
|
181
|
+
for (const source of sources) {
|
|
182
|
+
const filename = ResourceFetcherUtils.getFilenameFromUri(source);
|
|
183
|
+
const fileUri = `${RNEDirectory}${filename}`;
|
|
184
|
+
if (await ResourceFetcherUtils.checkFileExists(fileUri)) {
|
|
185
|
+
await deleteAsync(fileUri);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
static async getFilesTotalSize(...sources) {
|
|
190
|
+
return (await ResourceFetcherUtils.getFilesSizes(sources)).totalLength;
|
|
191
|
+
}
|
|
192
|
+
static async handleObject(source) {
|
|
193
|
+
if (typeof source !== 'object') {
|
|
194
|
+
throw new Error('Source is expected to be object!');
|
|
195
|
+
}
|
|
196
|
+
const jsonString = JSON.stringify(source);
|
|
197
|
+
const digest = ResourceFetcherUtils.hashObject(jsonString);
|
|
198
|
+
const filename = `${digest}.json`;
|
|
199
|
+
const path = `${RNEDirectory}${filename}`;
|
|
200
|
+
if (await ResourceFetcherUtils.checkFileExists(path)) {
|
|
201
|
+
return ResourceFetcherUtils.removeFilePrefix(path);
|
|
202
|
+
}
|
|
203
|
+
await ResourceFetcherUtils.createDirectoryIfNoExists();
|
|
204
|
+
await writeAsStringAsync(path, jsonString, {
|
|
205
|
+
encoding: EncodingType.UTF8,
|
|
206
|
+
});
|
|
207
|
+
return ResourceFetcherUtils.removeFilePrefix(path);
|
|
208
|
+
}
|
|
209
|
+
static handleLocalFile(source) {
|
|
210
|
+
if (typeof source !== 'string') {
|
|
211
|
+
throw new Error('Source is expected to be string.');
|
|
212
|
+
}
|
|
213
|
+
return ResourceFetcherUtils.removeFilePrefix(source);
|
|
214
|
+
}
|
|
215
|
+
static async handleReleaseModeFile(sourceExtended) {
|
|
216
|
+
const source = sourceExtended.source;
|
|
217
|
+
if (typeof source !== 'number') {
|
|
218
|
+
throw new Error('Source is expected to be string.');
|
|
219
|
+
}
|
|
220
|
+
const asset = Asset.fromModule(source);
|
|
221
|
+
const uri = asset.uri;
|
|
222
|
+
const filename = ResourceFetcherUtils.getFilenameFromUri(uri);
|
|
223
|
+
const fileUri = `${RNEDirectory}${filename}`;
|
|
224
|
+
const fileUriWithType = `${fileUri}.${asset.type}`;
|
|
225
|
+
if (await ResourceFetcherUtils.checkFileExists(fileUri)) {
|
|
226
|
+
return ResourceFetcherUtils.removeFilePrefix(fileUri);
|
|
227
|
+
}
|
|
228
|
+
await ResourceFetcherUtils.createDirectoryIfNoExists();
|
|
229
|
+
await asset.downloadAsync();
|
|
230
|
+
if (!asset.localUri) {
|
|
231
|
+
throw new Error(`Asset local URI is not available for ${source}`);
|
|
232
|
+
}
|
|
233
|
+
await moveAsync({ from: asset.localUri, to: fileUriWithType });
|
|
234
|
+
return ResourceFetcherUtils.removeFilePrefix(fileUriWithType);
|
|
235
|
+
}
|
|
236
|
+
static async handleDevModeFile(sourceExtended) {
|
|
237
|
+
const source = sourceExtended.source;
|
|
238
|
+
if (typeof source !== 'number') {
|
|
239
|
+
throw new Error('Source is expected to be a number.');
|
|
240
|
+
}
|
|
241
|
+
sourceExtended.uri = Asset.fromModule(source).uri;
|
|
242
|
+
return await this.handleRemoteFile(sourceExtended);
|
|
243
|
+
}
|
|
244
|
+
static async handleRemoteFile(sourceExtended) {
|
|
245
|
+
const source = sourceExtended.source;
|
|
246
|
+
if (typeof source === 'object') {
|
|
247
|
+
throw new Error('Source is expected to be a string or a number.');
|
|
248
|
+
}
|
|
249
|
+
if (this.downloads.has(source)) {
|
|
250
|
+
const resource = this.downloads.get(source);
|
|
251
|
+
if (resource.status === 1 /* DownloadStatus.PAUSED */) {
|
|
252
|
+
// if the download is paused, `fetch` is treated like `resume`
|
|
253
|
+
this.resume(source);
|
|
254
|
+
}
|
|
255
|
+
// if the download is ongoing, throw error.
|
|
256
|
+
throw new Error('Already downloading this file.');
|
|
257
|
+
}
|
|
258
|
+
if (typeof source === 'number' && !sourceExtended.uri) {
|
|
259
|
+
throw new Error('Source Uri is expected to be available here.');
|
|
260
|
+
}
|
|
261
|
+
if (typeof source === 'string') {
|
|
262
|
+
sourceExtended.uri = source;
|
|
263
|
+
}
|
|
264
|
+
const uri = sourceExtended.uri;
|
|
265
|
+
const filename = ResourceFetcherUtils.getFilenameFromUri(uri);
|
|
266
|
+
sourceExtended.fileUri = `${RNEDirectory}${filename}`;
|
|
267
|
+
sourceExtended.cacheFileUri = `${cacheDirectory}${filename}`;
|
|
268
|
+
if (await ResourceFetcherUtils.checkFileExists(sourceExtended.fileUri)) {
|
|
269
|
+
return ResourceFetcherUtils.removeFilePrefix(sourceExtended.fileUri);
|
|
270
|
+
}
|
|
271
|
+
await ResourceFetcherUtils.createDirectoryIfNoExists();
|
|
272
|
+
const downloadResumable = createDownloadResumable(uri, sourceExtended.cacheFileUri, { sessionType: FileSystemSessionType.BACKGROUND }, ({ totalBytesWritten, totalBytesExpectedToWrite }) => {
|
|
273
|
+
if (totalBytesExpectedToWrite === -1) {
|
|
274
|
+
// If totalBytesExpectedToWrite is -1, it means the server does not provide content length.
|
|
275
|
+
sourceExtended.callback(0);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
sourceExtended.callback(totalBytesWritten / totalBytesExpectedToWrite);
|
|
279
|
+
});
|
|
280
|
+
//create value for the this.download Map
|
|
281
|
+
const downloadResource = {
|
|
282
|
+
downloadResumable: downloadResumable,
|
|
283
|
+
status: 0 /* DownloadStatus.ONGOING */,
|
|
284
|
+
extendedInfo: sourceExtended,
|
|
285
|
+
};
|
|
286
|
+
//add key-value pair to map
|
|
287
|
+
this.downloads.set(source, downloadResource);
|
|
288
|
+
const result = await downloadResumable.downloadAsync();
|
|
289
|
+
if (!this.downloads.has(source) ||
|
|
290
|
+
this.downloads.get(source).status === 1 /* DownloadStatus.PAUSED */) {
|
|
291
|
+
// if canceled or paused during the download
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
if (!result || result.status !== 200 /* HTTP_CODE.OK */) {
|
|
295
|
+
throw new Error(`Failed to fetch resource from '${source}'`);
|
|
296
|
+
}
|
|
297
|
+
await moveAsync({
|
|
298
|
+
from: sourceExtended.cacheFileUri,
|
|
299
|
+
to: sourceExtended.fileUri,
|
|
300
|
+
});
|
|
301
|
+
this.downloads.delete(source);
|
|
302
|
+
ResourceFetcherUtils.triggerHuggingFaceDownloadCounter(uri);
|
|
303
|
+
return ResourceFetcherUtils.removeFilePrefix(sourceExtended.fileUri);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
import { DownloadResumable } from 'expo-file-system';
|
|
5
|
+
import { ResourceSource } from '../types/common';
|
|
6
|
+
export declare const enum HTTP_CODE {
|
|
7
|
+
OK = 200,
|
|
8
|
+
PARTIAL_CONTENT = 206
|
|
9
|
+
}
|
|
10
|
+
export declare const enum DownloadStatus {
|
|
11
|
+
ONGOING = 0,
|
|
12
|
+
PAUSED = 1
|
|
13
|
+
}
|
|
14
|
+
export declare const enum SourceType {
|
|
15
|
+
OBJECT = 0,
|
|
16
|
+
LOCAL_FILE = 1,
|
|
17
|
+
RELEASE_MODE_FILE = 2,
|
|
18
|
+
DEV_MODE_FILE = 3,
|
|
19
|
+
REMOTE_FILE = 4
|
|
20
|
+
}
|
|
21
|
+
export interface ResourceSourceExtended {
|
|
22
|
+
source: ResourceSource;
|
|
23
|
+
sourceType: SourceType;
|
|
24
|
+
callback?: (downloadProgress: number) => void;
|
|
25
|
+
results: string[];
|
|
26
|
+
uri?: string;
|
|
27
|
+
fileUri?: string;
|
|
28
|
+
cacheFileUri?: string;
|
|
29
|
+
next?: ResourceSourceExtended;
|
|
30
|
+
}
|
|
31
|
+
export interface DownloadResource {
|
|
32
|
+
downloadResumable: DownloadResumable;
|
|
33
|
+
status: DownloadStatus;
|
|
34
|
+
extendedInfo: ResourceSourceExtended;
|
|
35
|
+
}
|
|
36
|
+
export declare namespace ResourceFetcherUtils {
|
|
37
|
+
function getType(source: ResourceSource): SourceType;
|
|
38
|
+
function getFilesSizes(sources: ResourceSource[]): Promise<{
|
|
39
|
+
results: {
|
|
40
|
+
source: ResourceSource;
|
|
41
|
+
type: SourceType;
|
|
42
|
+
length: number;
|
|
43
|
+
previousFilesTotalLength: number;
|
|
44
|
+
}[];
|
|
45
|
+
totalLength: number;
|
|
46
|
+
}>;
|
|
47
|
+
function removeFilePrefix(uri: string): string;
|
|
48
|
+
function hashObject(jsonString: string): string;
|
|
49
|
+
function calculateDownloadProgress(totalLength: number, previousFilesTotalLength: number, currentFileLength: number, setProgress: (downloadProgress: number) => void): (progress: number) => void;
|
|
50
|
+
function triggerHuggingFaceDownloadCounter(uri: string): Promise<void>;
|
|
51
|
+
function createDirectoryIfNoExists(): Promise<void>;
|
|
52
|
+
function checkFileExists(fileUri: string): Promise<boolean>;
|
|
53
|
+
function getFilenameFromUri(uri: string): string;
|
|
54
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
import { getInfoAsync, makeDirectoryAsync, } from 'expo-file-system';
|
|
5
|
+
import { RNEDirectory } from '../constants/directories';
|
|
6
|
+
import { Asset } from 'expo-asset';
|
|
7
|
+
import { Logger } from '../common/Logger';
|
|
8
|
+
export var ResourceFetcherUtils;
|
|
9
|
+
(function (ResourceFetcherUtils) {
|
|
10
|
+
function getType(source) {
|
|
11
|
+
if (typeof source === 'object') {
|
|
12
|
+
return 0 /* SourceType.OBJECT */;
|
|
13
|
+
}
|
|
14
|
+
else if (typeof source === 'number') {
|
|
15
|
+
const uri = Asset.fromModule(source).uri;
|
|
16
|
+
if (!uri.includes('://')) {
|
|
17
|
+
return 2 /* SourceType.RELEASE_MODE_FILE */;
|
|
18
|
+
}
|
|
19
|
+
return 3 /* SourceType.DEV_MODE_FILE */;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
// typeof source == 'string'
|
|
23
|
+
if (source.startsWith('file://')) {
|
|
24
|
+
return 1 /* SourceType.LOCAL_FILE */;
|
|
25
|
+
}
|
|
26
|
+
return 4 /* SourceType.REMOTE_FILE */;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
ResourceFetcherUtils.getType = getType;
|
|
30
|
+
async function getFilesSizes(sources) {
|
|
31
|
+
const results = [];
|
|
32
|
+
let totalLength = 0;
|
|
33
|
+
let previousFilesTotalLength = 0;
|
|
34
|
+
for (const source of sources) {
|
|
35
|
+
const type = await ResourceFetcherUtils.getType(source);
|
|
36
|
+
let length = 0;
|
|
37
|
+
if (type === 4 /* SourceType.REMOTE_FILE */ && typeof source === 'string') {
|
|
38
|
+
try {
|
|
39
|
+
const response = await fetch(source, { method: 'HEAD' });
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
Logger.warn(`Failed to fetch HEAD for ${source}: ${response.status}`);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const contentLength = response.headers.get('content-length');
|
|
45
|
+
if (!contentLength) {
|
|
46
|
+
Logger.warn(`No content-length header for ${source}`);
|
|
47
|
+
}
|
|
48
|
+
length = contentLength ? parseInt(contentLength, 10) : 0;
|
|
49
|
+
previousFilesTotalLength = totalLength;
|
|
50
|
+
totalLength += length;
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
Logger.warn(`Error fetching HEAD for ${source}:`, error);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
results.push({ source, type, length, previousFilesTotalLength });
|
|
58
|
+
}
|
|
59
|
+
return { results, totalLength };
|
|
60
|
+
}
|
|
61
|
+
ResourceFetcherUtils.getFilesSizes = getFilesSizes;
|
|
62
|
+
function removeFilePrefix(uri) {
|
|
63
|
+
return uri.startsWith('file://') ? uri.slice(7) : uri;
|
|
64
|
+
}
|
|
65
|
+
ResourceFetcherUtils.removeFilePrefix = removeFilePrefix;
|
|
66
|
+
function hashObject(jsonString) {
|
|
67
|
+
let hash = 0;
|
|
68
|
+
for (let i = 0; i < jsonString.length; i++) {
|
|
69
|
+
// eslint-disable-next-line no-bitwise
|
|
70
|
+
hash = (hash << 5) - hash + jsonString.charCodeAt(i);
|
|
71
|
+
// eslint-disable-next-line no-bitwise
|
|
72
|
+
hash |= 0;
|
|
73
|
+
}
|
|
74
|
+
// eslint-disable-next-line no-bitwise
|
|
75
|
+
return (hash >>> 0).toString();
|
|
76
|
+
}
|
|
77
|
+
ResourceFetcherUtils.hashObject = hashObject;
|
|
78
|
+
function calculateDownloadProgress(totalLength, previousFilesTotalLength, currentFileLength, setProgress) {
|
|
79
|
+
return (progress) => {
|
|
80
|
+
if (progress === 1 &&
|
|
81
|
+
previousFilesTotalLength === totalLength - currentFileLength) {
|
|
82
|
+
setProgress(1);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
// Avoid division by zero
|
|
86
|
+
if (totalLength === 0) {
|
|
87
|
+
setProgress(0);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const baseProgress = previousFilesTotalLength / totalLength;
|
|
91
|
+
const scaledProgress = progress * (currentFileLength / totalLength);
|
|
92
|
+
const updatedProgress = baseProgress + scaledProgress;
|
|
93
|
+
setProgress(updatedProgress);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
ResourceFetcherUtils.calculateDownloadProgress = calculateDownloadProgress;
|
|
97
|
+
/*
|
|
98
|
+
* Increments the Hugging Face download counter if the URI points to a Software Mansion Hugging Face repo.
|
|
99
|
+
* More information: https://huggingface.co/docs/hub/models-download-stats
|
|
100
|
+
*/
|
|
101
|
+
async function triggerHuggingFaceDownloadCounter(uri) {
|
|
102
|
+
const url = new URL(uri);
|
|
103
|
+
if (url.host === 'huggingface.co' &&
|
|
104
|
+
url.pathname.startsWith('/software-mansion/')) {
|
|
105
|
+
const baseUrl = `${url.protocol}//${url.host}${url.pathname.split('resolve')[0]}`;
|
|
106
|
+
fetch(`${baseUrl}resolve/main/config.json`, { method: 'HEAD' });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
ResourceFetcherUtils.triggerHuggingFaceDownloadCounter = triggerHuggingFaceDownloadCounter;
|
|
110
|
+
async function createDirectoryIfNoExists() {
|
|
111
|
+
if (!(await checkFileExists(RNEDirectory))) {
|
|
112
|
+
await makeDirectoryAsync(RNEDirectory, { intermediates: true });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
ResourceFetcherUtils.createDirectoryIfNoExists = createDirectoryIfNoExists;
|
|
116
|
+
async function checkFileExists(fileUri) {
|
|
117
|
+
const fileInfo = await getInfoAsync(fileUri);
|
|
118
|
+
return fileInfo.exists;
|
|
119
|
+
}
|
|
120
|
+
ResourceFetcherUtils.checkFileExists = checkFileExists;
|
|
121
|
+
function getFilenameFromUri(uri) {
|
|
122
|
+
let cleanUri = uri.replace(/^https?:\/\//, '');
|
|
123
|
+
cleanUri = cleanUri.split('#')?.[0] ?? cleanUri;
|
|
124
|
+
return cleanUri.replace(/[^a-zA-Z0-9._-]/g, '_');
|
|
125
|
+
}
|
|
126
|
+
ResourceFetcherUtils.getFilenameFromUri = getFilenameFromUri;
|
|
127
|
+
})(ResourceFetcherUtils || (ResourceFetcherUtils = {}));
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ToolCall } from '../types/llm';
|
|
2
|
+
import { Schema } from 'jsonschema';
|
|
3
|
+
import * as zCore from 'zod/v4/core';
|
|
4
|
+
export declare const parseToolCall: (message: string) => ToolCall[];
|
|
5
|
+
export declare const getStructuredOutputPrompt: <T extends zCore.$ZodType>(responseSchema: T | Schema) => string;
|
|
6
|
+
export declare const fixAndValidateStructuredOutput: <T extends zCore.$ZodType>(output: string, responseSchema: T | Schema) => zCore.output<T>;
|
package/lib/utils/llm.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import * as z from 'zod/v4';
|
|
2
|
+
import { Validator } from 'jsonschema';
|
|
3
|
+
import { jsonrepair } from 'jsonrepair';
|
|
4
|
+
import { DEFAULT_STRUCTURED_OUTPUT_PROMPT } from '../constants/llmDefaults';
|
|
5
|
+
import * as zCore from 'zod/v4/core';
|
|
6
|
+
import { Logger } from '../common/Logger';
|
|
7
|
+
export const parseToolCall = (message) => {
|
|
8
|
+
try {
|
|
9
|
+
const unparsedToolCalls = message.match('\\[(.|\\s)*\\]');
|
|
10
|
+
if (!unparsedToolCalls) {
|
|
11
|
+
throw Error('Regex did not match array.');
|
|
12
|
+
}
|
|
13
|
+
const parsedMessage = JSON.parse(unparsedToolCalls[0]);
|
|
14
|
+
const results = [];
|
|
15
|
+
for (const tool of parsedMessage) {
|
|
16
|
+
if ('name' in tool &&
|
|
17
|
+
typeof tool.name === 'string' &&
|
|
18
|
+
'arguments' in tool &&
|
|
19
|
+
tool.arguments !== null &&
|
|
20
|
+
typeof tool.arguments === 'object') {
|
|
21
|
+
results.push({
|
|
22
|
+
toolName: tool.name,
|
|
23
|
+
arguments: tool.arguments,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return results;
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
Logger.error(e);
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const filterObjectKeys = (obj, keysToRemove) => {
|
|
35
|
+
const entries = Object.entries(obj);
|
|
36
|
+
const filteredEntries = entries.filter(([key, _]) => !keysToRemove.includes(key));
|
|
37
|
+
return Object.fromEntries(filteredEntries);
|
|
38
|
+
};
|
|
39
|
+
export const getStructuredOutputPrompt = (responseSchema) => {
|
|
40
|
+
const schemaObject = responseSchema instanceof zCore.$ZodType
|
|
41
|
+
? filterObjectKeys(z.toJSONSchema(responseSchema), [
|
|
42
|
+
'$schema',
|
|
43
|
+
'additionalProperties',
|
|
44
|
+
])
|
|
45
|
+
: responseSchema;
|
|
46
|
+
const schemaString = JSON.stringify(schemaObject);
|
|
47
|
+
return DEFAULT_STRUCTURED_OUTPUT_PROMPT(schemaString);
|
|
48
|
+
};
|
|
49
|
+
const extractBetweenBrackets = (text) => {
|
|
50
|
+
const startIndex = text.search(/[\\{\\[]/); // First occurrence of either { or [
|
|
51
|
+
const openingBracket = text[startIndex];
|
|
52
|
+
const closingBracket = openingBracket === '{' ? '}' : ']';
|
|
53
|
+
if (!openingBracket)
|
|
54
|
+
throw Error("Couldn't find JSON in text");
|
|
55
|
+
return text.slice(text.indexOf(openingBracket), text.lastIndexOf(closingBracket) + 1);
|
|
56
|
+
};
|
|
57
|
+
// this is a bit hacky typing
|
|
58
|
+
export const fixAndValidateStructuredOutput = (output, responseSchema) => {
|
|
59
|
+
const extractedOutput = extractBetweenBrackets(output);
|
|
60
|
+
const repairedOutput = jsonrepair(extractedOutput);
|
|
61
|
+
const outputJSON = JSON.parse(repairedOutput);
|
|
62
|
+
if (responseSchema instanceof zCore.$ZodType) {
|
|
63
|
+
return z.parse(responseSchema, outputJSON);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
const validator = new Validator();
|
|
67
|
+
validator.validate(outputJSON, responseSchema, {
|
|
68
|
+
throwAll: true,
|
|
69
|
+
});
|
|
70
|
+
return outputJSON;
|
|
71
|
+
}
|
|
72
|
+
};
|
package/lib/utils/stt.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const longCommonInfPref = (seq1, seq2, hammingDistThreshold) => {
|
|
2
|
+
let maxInd = 0;
|
|
3
|
+
let maxLength = 0;
|
|
4
|
+
for (let i = 0; i < seq1.length; i++) {
|
|
5
|
+
let j = 0;
|
|
6
|
+
let hammingDist = 0;
|
|
7
|
+
while (j < seq2.length &&
|
|
8
|
+
i + j < seq1.length &&
|
|
9
|
+
(seq1[i + j] === seq2[j] || hammingDist < hammingDistThreshold)) {
|
|
10
|
+
if (seq1[i + j] !== seq2[j]) {
|
|
11
|
+
hammingDist++;
|
|
12
|
+
}
|
|
13
|
+
j++;
|
|
14
|
+
}
|
|
15
|
+
if (j >= maxLength) {
|
|
16
|
+
maxLength = j;
|
|
17
|
+
maxInd = i;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return maxInd;
|
|
21
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-executorch",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.8",
|
|
4
4
|
"description": "An easy way to run AI models in React Native with ExecuTorch",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./lib/module/index.js",
|
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
"*.podspec",
|
|
18
18
|
"third-party/include",
|
|
19
19
|
"third-party",
|
|
20
|
+
"!third-party/ios/ExecutorchLib",
|
|
21
|
+
"!third-party/ios/libs/executorch",
|
|
20
22
|
"!ios/build",
|
|
21
23
|
"!android/build",
|
|
22
24
|
"!android/gradle",
|