llama-cpp-pro 0.2.2
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/CHANGELOG.md +295 -0
- package/LICENSE +21 -0
- package/LlamaCpp.podspec +33 -0
- package/LlamaCppCapacitor.podspec +33 -0
- package/Package.swift +29 -0
- package/README.md +93 -0
- package/android/build.gradle +88 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/CMakeLists-arm64.txt +138 -0
- package/android/src/main/CMakeLists-x86_64.txt +141 -0
- package/android/src/main/CMakeLists.txt +138 -0
- package/android/src/main/java/ai/annadata/plugin/capacitor/LlamaCpp.java +1340 -0
- package/android/src/main/java/ai/annadata/plugin/capacitor/LlamaCppPlugin.java +814 -0
- package/android/src/main/java/ai/annadata/plugin/capacitor/ModelAdmissionController.java +214 -0
- package/android/src/main/jni-chat-session.cpp +261 -0
- package/android/src/main/jni-lora.cpp +197 -0
- package/android/src/main/jni-multimodal.cpp +167 -0
- package/android/src/main/jni-tts.cpp +290 -0
- package/android/src/main/jni-utils.h +148 -0
- package/android/src/main/jni.cpp +1808 -0
- package/android/src/main/jniLibs/arm64-v8a/libllama-cpp-arm64.so +0 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/build-native.sh +280 -0
- package/cmake/desktop-metal-embed.cmake +30 -0
- package/cmake/desktop-sources.cmake +100 -0
- package/cmake/ggml-backends.cmake +44 -0
- package/cpp/LICENSE +21 -0
- package/cpp/README.md +15 -0
- package/cpp/anyascii.c +22223 -0
- package/cpp/anyascii.h +42 -0
- package/cpp/cap-completion.cpp +942 -0
- package/cpp/cap-completion.h +127 -0
- package/cpp/cap-embedding.cpp +193 -0
- package/cpp/cap-embedding.h +35 -0
- package/cpp/cap-ios-bridge.cpp +1810 -0
- package/cpp/cap-ios-bridge.h +61 -0
- package/cpp/cap-llama.cpp +412 -0
- package/cpp/cap-llama.h +161 -0
- package/cpp/cap-mtmd.hpp +602 -0
- package/cpp/cap-native-server.cpp +1095 -0
- package/cpp/cap-native-server.h +40 -0
- package/cpp/cap-tts.cpp +591 -0
- package/cpp/cap-tts.h +59 -0
- package/cpp/cap-wasm-fs.cpp +156 -0
- package/cpp/cap-wasm-jspi.cpp +19 -0
- package/cpp/cap-wasm-jspi.h +30 -0
- package/cpp/cap-wasm-vfs.cpp +22 -0
- package/cpp/chat-parser.cpp +393 -0
- package/cpp/chat-parser.h +120 -0
- package/cpp/chat.cpp +2315 -0
- package/cpp/chat.h +221 -0
- package/cpp/common.cpp +1664 -0
- package/cpp/common.h +744 -0
- package/cpp/ggml-alloc.c +1028 -0
- package/cpp/ggml-alloc.h +76 -0
- package/cpp/ggml-backend-impl.h +255 -0
- package/cpp/ggml-backend-reg.cpp +600 -0
- package/cpp/ggml-backend.cpp +2121 -0
- package/cpp/ggml-backend.h +354 -0
- package/cpp/ggml-common.h +1878 -0
- package/cpp/ggml-cpp.h +39 -0
- package/cpp/ggml-cpu/amx/amx.cpp +221 -0
- package/cpp/ggml-cpu/amx/amx.h +8 -0
- package/cpp/ggml-cpu/amx/common.h +91 -0
- package/cpp/ggml-cpu/amx/mmq.cpp +2512 -0
- package/cpp/ggml-cpu/amx/mmq.h +10 -0
- package/cpp/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
- package/cpp/ggml-cpu/arch/arm/quants.c +3650 -0
- package/cpp/ggml-cpu/arch/arm/repack.cpp +1891 -0
- package/cpp/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
- package/cpp/ggml-cpu/arch/x86/quants.c +3820 -0
- package/cpp/ggml-cpu/arch/x86/repack.cpp +6307 -0
- package/cpp/ggml-cpu/arch-fallback.h +215 -0
- package/cpp/ggml-cpu/binary-ops.cpp +158 -0
- package/cpp/ggml-cpu/binary-ops.h +16 -0
- package/cpp/ggml-cpu/common.h +73 -0
- package/cpp/ggml-cpu/ggml-cpu-impl.h +559 -0
- package/cpp/ggml-cpu/ggml-cpu.c +3578 -0
- package/cpp/ggml-cpu/ggml-cpu.cpp +672 -0
- package/cpp/ggml-cpu/ops.cpp +10587 -0
- package/cpp/ggml-cpu/ops.h +114 -0
- package/cpp/ggml-cpu/quants.c +1193 -0
- package/cpp/ggml-cpu/quants.h +97 -0
- package/cpp/ggml-cpu/repack.cpp +1982 -0
- package/cpp/ggml-cpu/repack.h +120 -0
- package/cpp/ggml-cpu/simd-mappings.h +1184 -0
- package/cpp/ggml-cpu/traits.cpp +36 -0
- package/cpp/ggml-cpu/traits.h +38 -0
- package/cpp/ggml-cpu/unary-ops.cpp +186 -0
- package/cpp/ggml-cpu/unary-ops.h +28 -0
- package/cpp/ggml-cpu/vec.cpp +348 -0
- package/cpp/ggml-cpu/vec.h +1121 -0
- package/cpp/ggml-cpu.h +145 -0
- package/cpp/ggml-impl.h +622 -0
- package/cpp/ggml-metal-impl.h +688 -0
- package/cpp/ggml-metal.h +66 -0
- package/cpp/ggml-metal.m +6833 -0
- package/cpp/ggml-metal.metal +10754 -0
- package/cpp/ggml-opt.cpp +1093 -0
- package/cpp/ggml-opt.h +256 -0
- package/cpp/ggml-quants.c +5324 -0
- package/cpp/ggml-quants.h +106 -0
- package/cpp/ggml-threading.cpp +12 -0
- package/cpp/ggml-threading.h +14 -0
- package/cpp/ggml.c +7108 -0
- package/cpp/ggml.h +2492 -0
- package/cpp/gguf.cpp +1358 -0
- package/cpp/gguf.h +202 -0
- package/cpp/json-partial.cpp +256 -0
- package/cpp/json-partial.h +38 -0
- package/cpp/json-schema-to-grammar.cpp +985 -0
- package/cpp/json-schema-to-grammar.h +21 -0
- package/cpp/llama-adapter.cpp +388 -0
- package/cpp/llama-adapter.h +76 -0
- package/cpp/llama-arch.cpp +2355 -0
- package/cpp/llama-arch.h +499 -0
- package/cpp/llama-batch.cpp +875 -0
- package/cpp/llama-batch.h +160 -0
- package/cpp/llama-chat.cpp +783 -0
- package/cpp/llama-chat.h +65 -0
- package/cpp/llama-context.cpp +2788 -0
- package/cpp/llama-context.h +306 -0
- package/cpp/llama-cparams.cpp +5 -0
- package/cpp/llama-cparams.h +41 -0
- package/cpp/llama-cpp.h +30 -0
- package/cpp/llama-grammar.cpp +1229 -0
- package/cpp/llama-grammar.h +173 -0
- package/cpp/llama-graph.cpp +1891 -0
- package/cpp/llama-graph.h +810 -0
- package/cpp/llama-hparams.cpp +180 -0
- package/cpp/llama-hparams.h +233 -0
- package/cpp/llama-impl.cpp +167 -0
- package/cpp/llama-impl.h +61 -0
- package/cpp/llama-io.cpp +15 -0
- package/cpp/llama-io.h +35 -0
- package/cpp/llama-kv-cache-iswa.cpp +318 -0
- package/cpp/llama-kv-cache-iswa.h +135 -0
- package/cpp/llama-kv-cache.cpp +2059 -0
- package/cpp/llama-kv-cache.h +374 -0
- package/cpp/llama-kv-cells.h +491 -0
- package/cpp/llama-memory-hybrid.cpp +258 -0
- package/cpp/llama-memory-hybrid.h +137 -0
- package/cpp/llama-memory-recurrent.cpp +1146 -0
- package/cpp/llama-memory-recurrent.h +179 -0
- package/cpp/llama-memory.cpp +59 -0
- package/cpp/llama-memory.h +119 -0
- package/cpp/llama-mmap.cpp +609 -0
- package/cpp/llama-mmap.h +68 -0
- package/cpp/llama-model-loader.cpp +1166 -0
- package/cpp/llama-model-loader.h +170 -0
- package/cpp/llama-model-saver.cpp +282 -0
- package/cpp/llama-model-saver.h +37 -0
- package/cpp/llama-model.cpp +19061 -0
- package/cpp/llama-model.h +491 -0
- package/cpp/llama-sampling.cpp +2575 -0
- package/cpp/llama-sampling.h +32 -0
- package/cpp/llama-vocab.cpp +3792 -0
- package/cpp/llama-vocab.h +176 -0
- package/cpp/llama.cpp +358 -0
- package/cpp/llama.h +1373 -0
- package/cpp/log.cpp +428 -0
- package/cpp/log.h +103 -0
- package/cpp/minja/chat-template.hpp +550 -0
- package/cpp/minja/minja.hpp +3009 -0
- package/cpp/nlohmann/json.hpp +25526 -0
- package/cpp/nlohmann/json_fwd.hpp +187 -0
- package/cpp/regex-partial.cpp +204 -0
- package/cpp/regex-partial.h +56 -0
- package/cpp/sampling.cpp +579 -0
- package/cpp/sampling.h +107 -0
- package/cpp/tools/mtmd/clip-impl.h +473 -0
- package/cpp/tools/mtmd/clip.cpp +4322 -0
- package/cpp/tools/mtmd/clip.h +106 -0
- package/cpp/tools/mtmd/miniaudio/miniaudio.h +93468 -0
- package/cpp/tools/mtmd/mtmd-audio.cpp +769 -0
- package/cpp/tools/mtmd/mtmd-audio.h +47 -0
- package/cpp/tools/mtmd/mtmd-helper.cpp +460 -0
- package/cpp/tools/mtmd/mtmd-helper.h +95 -0
- package/cpp/tools/mtmd/mtmd.cpp +1066 -0
- package/cpp/tools/mtmd/mtmd.h +302 -0
- package/cpp/tools/mtmd/stb/stb_image.h +7988 -0
- package/cpp/unicode-data.cpp +7034 -0
- package/cpp/unicode-data.h +20 -0
- package/cpp/unicode.cpp +1061 -0
- package/cpp/unicode.h +68 -0
- package/cpp/vendor/cpp-httplib/httplib.cpp +16509 -0
- package/cpp/vendor/cpp-httplib/httplib.h +3883 -0
- package/desktop/electron-builder.config.cjs +157 -0
- package/desktop/entitlements.mac.plist +12 -0
- package/desktop/package.json +11 -0
- package/desktop/resolve-package-root.cjs +88 -0
- package/desktop/src/main/backend-selector.cjs +148 -0
- package/desktop/src/main/gpu-probe.cjs +142 -0
- package/desktop/src/main/index.cjs +58 -0
- package/desktop/src/main/ipc-handlers.cjs +212 -0
- package/desktop/src/main/model-store.cjs +50 -0
- package/desktop/src/main/preload.cjs +39 -0
- package/desktop/src/main/sidecar-client.cjs +76 -0
- package/desktop/src/main/sidecar-manager.cjs +364 -0
- package/dist/docs.json +14357 -0
- package/dist/esm/definitions.d.ts +731 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/desktop.d.ts +37 -0
- package/dist/esm/desktop.js +133 -0
- package/dist/esm/desktop.js.map +1 -0
- package/dist/esm/index.d.ts +200 -0
- package/dist/esm/index.js +612 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/isomorphic/desktop.runtime.d.ts +37 -0
- package/dist/esm/isomorphic/desktop.runtime.js +36 -0
- package/dist/esm/isomorphic/desktop.runtime.js.map +1 -0
- package/dist/esm/isomorphic/errors.d.ts +6 -0
- package/dist/esm/isomorphic/errors.js +9 -0
- package/dist/esm/isomorphic/errors.js.map +1 -0
- package/dist/esm/isomorphic/model.admission.d.ts +17 -0
- package/dist/esm/isomorphic/model.admission.js +27 -0
- package/dist/esm/isomorphic/model.admission.js.map +1 -0
- package/dist/esm/isomorphic/model.scheduler.d.ts +31 -0
- package/dist/esm/isomorphic/model.scheduler.js +95 -0
- package/dist/esm/isomorphic/model.scheduler.js.map +1 -0
- package/dist/esm/isomorphic/provider.desktop.d.ts +40 -0
- package/dist/esm/isomorphic/provider.desktop.js +411 -0
- package/dist/esm/isomorphic/provider.desktop.js.map +1 -0
- package/dist/esm/isomorphic/provider.factory.d.ts +2 -0
- package/dist/esm/isomorphic/provider.factory.js +16 -0
- package/dist/esm/isomorphic/provider.factory.js.map +1 -0
- package/dist/esm/isomorphic/provider.interface.d.ts +76 -0
- package/dist/esm/isomorphic/provider.interface.js +2 -0
- package/dist/esm/isomorphic/provider.interface.js.map +1 -0
- package/dist/esm/isomorphic/provider.native.d.ts +18 -0
- package/dist/esm/isomorphic/provider.native.js +173 -0
- package/dist/esm/isomorphic/provider.native.js.map +1 -0
- package/dist/esm/isomorphic/provider.web.d.ts +88 -0
- package/dist/esm/isomorphic/provider.web.js +573 -0
- package/dist/esm/isomorphic/provider.web.js.map +1 -0
- package/dist/esm/isomorphic/sidecar-sse.d.ts +14 -0
- package/dist/esm/isomorphic/sidecar-sse.js +76 -0
- package/dist/esm/isomorphic/sidecar-sse.js.map +1 -0
- package/dist/esm/isomorphic/wasmMemoryCalibration.d.ts +26 -0
- package/dist/esm/isomorphic/wasmMemoryCalibration.js +60 -0
- package/dist/esm/isomorphic/wasmMemoryCalibration.js.map +1 -0
- package/dist/esm/isomorphic/wasmMemoryPolicy.d.ts +55 -0
- package/dist/esm/isomorphic/wasmMemoryPolicy.js +93 -0
- package/dist/esm/isomorphic/wasmMemoryPolicy.js.map +1 -0
- package/dist/esm/storage/manifest.d.ts +13 -0
- package/dist/esm/storage/manifest.js +87 -0
- package/dist/esm/storage/manifest.js.map +1 -0
- package/dist/esm/storage/opfs.store.d.ts +31 -0
- package/dist/esm/storage/opfs.store.js +241 -0
- package/dist/esm/storage/opfs.store.js.map +1 -0
- package/dist/esm/web.d.ts +206 -0
- package/dist/esm/web.js +521 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/esm/workers/async-file.d.ts +13 -0
- package/dist/esm/workers/async-file.js +12 -0
- package/dist/esm/workers/async-file.js.map +1 -0
- package/dist/esm/workers/heapfs.d.ts +55 -0
- package/dist/esm/workers/heapfs.js +115 -0
- package/dist/esm/workers/heapfs.js.map +1 -0
- package/dist/esm/workers/wasm.engine.d.ts +86 -0
- package/dist/esm/workers/wasm.engine.js +504 -0
- package/dist/esm/workers/wasm.engine.js.map +1 -0
- package/dist/esm/workers/worker.protocol.d.ts +160 -0
- package/dist/esm/workers/worker.protocol.js +2 -0
- package/dist/esm/workers/worker.protocol.js.map +1 -0
- package/dist/plugin.cjs +3179 -0
- package/dist/plugin.cjs.map +1 -0
- package/dist/plugin.js +3181 -0
- package/dist/plugin.js.map +1 -0
- package/dist/wasm/llama_engine.d.ts +74 -0
- package/dist/wasm/llama_engine.js +1829 -0
- package/dist/wasm/llama_engine.wasm +0 -0
- package/dist/wasm/llama_engine_emscripten.mjs +2 -0
- package/dist/wasm/package.json +16 -0
- package/dist/workers/llm.worker.js +1226 -0
- package/dist/workers/llm.worker.js.map +7 -0
- package/extraResources/llama-wasm/llama_engine.d.ts +74 -0
- package/extraResources/llama-wasm/llama_engine.js +1829 -0
- package/extraResources/llama-wasm/llama_engine.wasm +0 -0
- package/extraResources/llama-wasm/llama_engine_emscripten.mjs +2 -0
- package/extraResources/llama-wasm/package.json +16 -0
- package/extraResources/sidecar/README.md +11 -0
- package/extraResources/sidecar/darwin-arm64 +0 -0
- package/extraResources/sidecar/darwin-x64 +0 -0
- package/ios/CMakeLists-arm64.txt +161 -0
- package/ios/CMakeLists-x86_64.txt +188 -0
- package/ios/CMakeLists.txt +161 -0
- package/ios/Frameworks/llama-cpp.framework/Info.plist +28 -0
- package/ios/Frameworks/llama-cpp.framework/llama-cpp +0 -0
- package/ios/Sources/LlamaCppCapacitor/LlamaCpp.swift +1365 -0
- package/ios/Sources/LlamaCppCapacitor/LlamaCppPlugin.swift +694 -0
- package/ios/Sources/LlamaCppCapacitor/LlamaNativeBridge.swift +349 -0
- package/ios/Sources/LlamaCppCapacitor/ModelAdmissionController.swift +177 -0
- package/ios/embed-metal-shaders.sh +24 -0
- package/ios/metal-embed.cmake +29 -0
- package/package.json +281 -0
- package/scripts/build-js-preserve-wasm.cjs +53 -0
- package/scripts/build-sidecar-linux.sh +6 -0
- package/scripts/build-sidecar-win.bat +19 -0
- package/scripts/build-sidecar.sh +184 -0
- package/scripts/embed-llama-ios-app-framework.sh +63 -0
- package/scripts/ensure-desktop-sidecar-bundle.cjs +103 -0
- package/scripts/ensure-llama-ios-xcframework.sh +108 -0
- package/scripts/fix-esm-extensions.cjs +45 -0
- package/scripts/prepare-js-dist.cjs +28 -0
- package/scripts/stage-desktop-resources.cjs +116 -0
- package/sidecar/CMakeLists.txt +192 -0
- package/sidecar/cap-sidecar-main.cpp +68 -0
- package/types/llama-cpp-pro.d.ts +441 -0
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sidecar process lifecycle manager for llama-cpp desktop.
|
|
3
|
+
* Supports NVIDIA (CUDA), AMD (ROCm), Intel (OpenVINO), Apple (Metal), Vulkan, and CPU.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const os = require('os');
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const { spawn } = require('child_process');
|
|
10
|
+
const { createSidecarClient } = require('./sidecar-client.cjs');
|
|
11
|
+
const { backendToVariant } = require('./backend-selector.cjs');
|
|
12
|
+
|
|
13
|
+
const MAX_RESTART_ATTEMPTS = 2;
|
|
14
|
+
const HEALTH_INTERVAL_MS = 500;
|
|
15
|
+
const HEALTH_MAX_ATTEMPTS = 30;
|
|
16
|
+
const SHUTDOWN_GRACE_MS = 500;
|
|
17
|
+
|
|
18
|
+
function getRandomPort() {
|
|
19
|
+
return 18080 + Math.floor(Math.random() * 2000);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function vulkanStderrImpliesGpuFailure(buf) {
|
|
23
|
+
if (!buf || buf.length < 12) return false;
|
|
24
|
+
return (
|
|
25
|
+
/VK_ERROR_[A-Z0-9_]+/i.test(buf)
|
|
26
|
+
|| (/ggml_vulkan/i.test(buf) && /error|fail/i.test(buf))
|
|
27
|
+
|| /Vulkan.*(fail|error)/i.test(buf)
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function rocmStderrImpliesGpuFailure(buf) {
|
|
32
|
+
if (!buf || buf.length < 12) return false;
|
|
33
|
+
return (
|
|
34
|
+
/rocblas\s*error/i.test(buf)
|
|
35
|
+
|| /tensilelibrary/i.test(buf)
|
|
36
|
+
|| /ROCm error/i.test(buf)
|
|
37
|
+
|| /hipblas/i.test(buf)
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Resolve sidecar binary for platform, arch, and GPU backend variant.
|
|
43
|
+
*/
|
|
44
|
+
function resolveBinaryPath(deps) {
|
|
45
|
+
const platform = (deps && deps.platform) || process.platform;
|
|
46
|
+
const arch = (deps && deps.arch) || process.arch;
|
|
47
|
+
const gpuBackend = (deps && deps.gpuBackend) || null;
|
|
48
|
+
const variant = (deps && deps.variant) || (gpuBackend ? backendToVariant(gpuBackend, platform) : null);
|
|
49
|
+
const resourcesPath = (deps && deps.resourcesPath) || process.resourcesPath;
|
|
50
|
+
const _fs = (deps && deps.fs) || fs;
|
|
51
|
+
|
|
52
|
+
const ext = platform === 'win32' ? '.exe' : '';
|
|
53
|
+
const names = [];
|
|
54
|
+
|
|
55
|
+
if (variant) {
|
|
56
|
+
names.push(`${platform}-${arch}-${variant}${ext}`);
|
|
57
|
+
}
|
|
58
|
+
if (gpuBackend === 'rocm') {
|
|
59
|
+
names.push(`${platform}-${arch}-rocm${ext}`);
|
|
60
|
+
}
|
|
61
|
+
if (gpuBackend === 'cuda') {
|
|
62
|
+
names.push(`${platform}-${arch}-cuda${ext}`);
|
|
63
|
+
}
|
|
64
|
+
if (gpuBackend === 'vulkan') {
|
|
65
|
+
names.push(`${platform}-${arch}-vulkan${ext}`);
|
|
66
|
+
}
|
|
67
|
+
names.push(`${platform}-${arch}${ext}`);
|
|
68
|
+
|
|
69
|
+
const searchDirs = [];
|
|
70
|
+
if (resourcesPath) {
|
|
71
|
+
searchDirs.push(path.join(resourcesPath, 'sidecar'));
|
|
72
|
+
}
|
|
73
|
+
searchDirs.push(
|
|
74
|
+
path.join(process.cwd(), 'extraResources', 'sidecar'),
|
|
75
|
+
path.join(process.cwd(), 'sidecar', 'bin'),
|
|
76
|
+
path.join(process.cwd(), 'sidecar', 'build', 'bin'),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
for (const dir of searchDirs) {
|
|
80
|
+
for (const name of names) {
|
|
81
|
+
const p = path.join(dir, name);
|
|
82
|
+
try {
|
|
83
|
+
_fs.accessSync(p);
|
|
84
|
+
return p;
|
|
85
|
+
} catch (_) { /* next */ }
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return path.join(searchDirs[0], names[0]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function resolveBackendPluginDir(binaryPath, deps) {
|
|
93
|
+
const platform = (deps && deps.platform) || process.platform;
|
|
94
|
+
const arch = (deps && deps.arch) || process.arch;
|
|
95
|
+
const resourcesPath = (deps && deps.resourcesPath) || process.resourcesPath;
|
|
96
|
+
const _fs = (deps && deps.fs) || fs;
|
|
97
|
+
|
|
98
|
+
const candidates = [
|
|
99
|
+
path.join(path.dirname(binaryPath), 'ggml-plugins', `${platform}-${arch}`),
|
|
100
|
+
path.join(path.dirname(binaryPath), 'runtime-libs', `${platform}-${arch}`),
|
|
101
|
+
path.dirname(binaryPath),
|
|
102
|
+
];
|
|
103
|
+
if (resourcesPath) {
|
|
104
|
+
candidates.unshift(
|
|
105
|
+
path.join(resourcesPath, 'sidecar', 'ggml-plugins', `${platform}-${arch}`),
|
|
106
|
+
path.join(resourcesPath, 'sidecar', 'runtime-libs', `${platform}-${arch}`),
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
for (const c of candidates) {
|
|
111
|
+
try {
|
|
112
|
+
if (_fs.existsSync(c)) return c;
|
|
113
|
+
} catch (_) { /* skip */ }
|
|
114
|
+
}
|
|
115
|
+
return path.dirname(binaryPath);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function buildSpawnEnv(binaryPath, backendDir, forceCpu, deps) {
|
|
119
|
+
const platform = (deps && deps.platform) || process.platform;
|
|
120
|
+
const env = { ...process.env };
|
|
121
|
+
|
|
122
|
+
if (backendDir) {
|
|
123
|
+
if (platform === 'win32') {
|
|
124
|
+
env.PATH = `${backendDir};${env.PATH || ''}`;
|
|
125
|
+
} else {
|
|
126
|
+
env.LD_LIBRARY_PATH = `${backendDir}:${env.LD_LIBRARY_PATH || ''}`;
|
|
127
|
+
env.DYLD_LIBRARY_PATH = `${backendDir}:${env.DYLD_LIBRARY_PATH || ''}`;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (forceCpu) {
|
|
132
|
+
env.LLAMA_NO_GPU = '1';
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return env;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function createSidecarManager(deps) {
|
|
139
|
+
const _fs = (deps && deps.fs) || fs;
|
|
140
|
+
const _spawn = (deps && deps.spawn) || spawn;
|
|
141
|
+
const _createClient = (deps && deps.createClient) || createSidecarClient;
|
|
142
|
+
const _resolveBinaryPath = (deps && deps.resolveBinaryPath) || resolveBinaryPath;
|
|
143
|
+
const _getRandomPort = (deps && deps.getRandomPort) || getRandomPort;
|
|
144
|
+
const _log = (deps && deps.log) || console;
|
|
145
|
+
|
|
146
|
+
let status = {
|
|
147
|
+
running: false,
|
|
148
|
+
port: null,
|
|
149
|
+
pid: null,
|
|
150
|
+
backend: null,
|
|
151
|
+
variant: null,
|
|
152
|
+
binaryPath: null,
|
|
153
|
+
restartCount: 0,
|
|
154
|
+
permanentWasmFallback: false,
|
|
155
|
+
forceCpu: false,
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
let childProcess = null;
|
|
159
|
+
let client = null;
|
|
160
|
+
let intentionalStop = false;
|
|
161
|
+
let accelStderr = '';
|
|
162
|
+
const listeners = [];
|
|
163
|
+
|
|
164
|
+
function emit() {
|
|
165
|
+
const snap = { ...status };
|
|
166
|
+
for (const fn of listeners) {
|
|
167
|
+
try { fn(snap); } catch (_) { /* ignore */ }
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function buildSpawnArgs(modelPath, port, opts) {
|
|
172
|
+
const args = [
|
|
173
|
+
'--host', '127.0.0.1',
|
|
174
|
+
'--port', String(port),
|
|
175
|
+
];
|
|
176
|
+
if (modelPath) {
|
|
177
|
+
args.push('--model', modelPath);
|
|
178
|
+
if (opts && opts.modelId) {
|
|
179
|
+
args.push('--model-id', String(opts.modelId));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (opts && opts.n_ctx) {
|
|
183
|
+
args.push('--ctx-size', String(opts.n_ctx));
|
|
184
|
+
}
|
|
185
|
+
if (opts && opts.n_threads) {
|
|
186
|
+
args.push('--threads', String(opts.n_threads));
|
|
187
|
+
}
|
|
188
|
+
const ngl = opts && opts.n_gpu_layers;
|
|
189
|
+
if (status.forceCpu || opts?.forceCpu) {
|
|
190
|
+
args.push('--no-gpu');
|
|
191
|
+
} else if (ngl != null && ngl >= 0) {
|
|
192
|
+
args.push('--n-gpu-layers', String(ngl));
|
|
193
|
+
} else if (status.backend && status.backend !== 'cpu') {
|
|
194
|
+
args.push('--n-gpu-layers', '99');
|
|
195
|
+
}
|
|
196
|
+
const backendDir = resolveBackendPluginDir(status.binaryPath || '', deps);
|
|
197
|
+
if (backendDir) {
|
|
198
|
+
args.push('--backend-dir', backendDir);
|
|
199
|
+
}
|
|
200
|
+
return args;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async function waitForHealth(port) {
|
|
204
|
+
const c = _createClient(port);
|
|
205
|
+
for (let i = 0; i < HEALTH_MAX_ATTEMPTS; i++) {
|
|
206
|
+
try {
|
|
207
|
+
const h = await c.health();
|
|
208
|
+
if (h && h.status === 'ok') return true;
|
|
209
|
+
} catch (_) { /* retry */ }
|
|
210
|
+
await new Promise((r) => setTimeout(r, HEALTH_INTERVAL_MS));
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async function start(options) {
|
|
216
|
+
if (status.permanentWasmFallback) {
|
|
217
|
+
return { ok: false, reason: 'permanent-wasm-fallback' };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const selection = options && options.selection;
|
|
221
|
+
const modelPath = options && options.modelPath;
|
|
222
|
+
|
|
223
|
+
if (status.running && status.port) {
|
|
224
|
+
return { ok: true, port: status.port };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const gpuBackend = selection && selection.gpuBackend;
|
|
228
|
+
const variant = (selection && selection.variant) || null;
|
|
229
|
+
status.variant = variant;
|
|
230
|
+
status.backend = gpuBackend || (selection && selection.type) || 'cpu';
|
|
231
|
+
status.binaryPath = _resolveBinaryPath({ ...deps, variant, gpuBackend });
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
_fs.accessSync(status.binaryPath);
|
|
235
|
+
} catch (_) {
|
|
236
|
+
_log.warn(`[sidecar-manager] Binary not found: ${status.binaryPath}`);
|
|
237
|
+
status.permanentWasmFallback = true;
|
|
238
|
+
emit();
|
|
239
|
+
return { ok: false, reason: 'binary-missing', path: status.binaryPath };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const port = options?.port || _getRandomPort();
|
|
243
|
+
const backendDir = resolveBackendPluginDir(status.binaryPath, deps);
|
|
244
|
+
const args = buildSpawnArgs(modelPath, port, options);
|
|
245
|
+
const env = buildSpawnEnv(status.binaryPath, backendDir, status.forceCpu, deps);
|
|
246
|
+
|
|
247
|
+
intentionalStop = false;
|
|
248
|
+
accelStderr = '';
|
|
249
|
+
|
|
250
|
+
childProcess = _spawn(status.binaryPath, args, {
|
|
251
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
252
|
+
windowsHide: true,
|
|
253
|
+
env,
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
status.pid = childProcess.pid;
|
|
257
|
+
status.port = port;
|
|
258
|
+
|
|
259
|
+
childProcess.stderr.on('data', (chunk) => {
|
|
260
|
+
const text = String(chunk);
|
|
261
|
+
accelStderr = (accelStderr + text).slice(-65536);
|
|
262
|
+
if (process.env.LLAMA_SIDECAR_VERBOSE === '1') {
|
|
263
|
+
_log.info('[sidecar]', text.trim());
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
childProcess.on('exit', (code) => {
|
|
268
|
+
if (intentionalStop) {
|
|
269
|
+
status.running = false;
|
|
270
|
+
status.pid = null;
|
|
271
|
+
emit();
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const gpuFail =
|
|
276
|
+
vulkanStderrImpliesGpuFailure(accelStderr)
|
|
277
|
+
|| rocmStderrImpliesGpuFailure(accelStderr);
|
|
278
|
+
|
|
279
|
+
if (gpuFail && !status.forceCpu && options?.retryCpu !== false) {
|
|
280
|
+
_log.warn('[sidecar-manager] GPU init failed — retrying CPU-only');
|
|
281
|
+
status.forceCpu = true;
|
|
282
|
+
status.restartCount += 1;
|
|
283
|
+
status.running = false;
|
|
284
|
+
emit();
|
|
285
|
+
start({ ...options, forceCpu: true, retryCpu: false }).catch(() => {});
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (status.restartCount < MAX_RESTART_ATTEMPTS) {
|
|
290
|
+
status.restartCount += 1;
|
|
291
|
+
_log.warn(`[sidecar-manager] Unexpected exit (${code}), restart ${status.restartCount}`);
|
|
292
|
+
status.running = false;
|
|
293
|
+
emit();
|
|
294
|
+
start(options).catch(() => {});
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
status.permanentWasmFallback = true;
|
|
299
|
+
_log.warn('[sidecar-manager] Max restarts exceeded — WASM fallback');
|
|
300
|
+
status.running = false;
|
|
301
|
+
status.pid = null;
|
|
302
|
+
emit();
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
const healthy = await waitForHealth(port);
|
|
306
|
+
if (!healthy) {
|
|
307
|
+
await stop();
|
|
308
|
+
if (!status.forceCpu && options?.retryCpu !== false) {
|
|
309
|
+
status.forceCpu = true;
|
|
310
|
+
return start({ ...options, forceCpu: true, retryCpu: false });
|
|
311
|
+
}
|
|
312
|
+
status.permanentWasmFallback = true;
|
|
313
|
+
emit();
|
|
314
|
+
return { ok: false, reason: 'health-check-failed' };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
client = _createClient(port);
|
|
318
|
+
status.running = true;
|
|
319
|
+
emit();
|
|
320
|
+
return { ok: true, port };
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
async function stop() {
|
|
324
|
+
intentionalStop = true;
|
|
325
|
+
if (childProcess && !childProcess.killed) {
|
|
326
|
+
childProcess.kill('SIGTERM');
|
|
327
|
+
await new Promise((r) => setTimeout(r, SHUTDOWN_GRACE_MS));
|
|
328
|
+
if (!childProcess.killed) childProcess.kill('SIGKILL');
|
|
329
|
+
}
|
|
330
|
+
childProcess = null;
|
|
331
|
+
client = null;
|
|
332
|
+
status.running = false;
|
|
333
|
+
status.port = null;
|
|
334
|
+
status.pid = null;
|
|
335
|
+
emit();
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function getClient() {
|
|
339
|
+
return client;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function getStatus() {
|
|
343
|
+
return { ...status };
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function onStatusChange(fn) {
|
|
347
|
+
listeners.push(fn);
|
|
348
|
+
return () => {
|
|
349
|
+
const i = listeners.indexOf(fn);
|
|
350
|
+
if (i >= 0) listeners.splice(i, 1);
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
return {
|
|
355
|
+
start, stop, getClient, getStatus, onStatusChange, resolveBinaryPath,
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
module.exports = {
|
|
360
|
+
createSidecarManager,
|
|
361
|
+
resolveBinaryPath,
|
|
362
|
+
resolveBackendPluginDir,
|
|
363
|
+
getRandomPort,
|
|
364
|
+
};
|