@openclaw/llama-cpp-provider 2026.11.3 → 2026.11.5-beta.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/dist/.setup/{auth-config-Dx2WDi-G.mjs → auth-config-BJOlAPuO.mjs} +1 -1
- package/dist/.setup/{discovery-osk0TXQN.mjs → discovery-CghcMQy6.mjs} +4 -6
- package/dist/.setup/{embedding-provider.runtime-djNMas6G.mjs → embedding-provider.runtime-Bb0UVBwq.mjs} +46 -37
- package/dist/.setup/{managed-provider-config-BuFqNTx1.mjs → managed-provider-config-Dn-MKZjS.mjs} +1 -1
- package/dist/.setup/{managed-server-Bjr1c5yX.mjs → managed-server-CZGhj7j8.mjs} +396 -295
- package/dist/.setup/{media-catalog-BwFQGYtY.mjs → media-catalog-DgRc3mpW.mjs} +7 -212
- package/dist/.setup/{media-provider.runtime-MqV-UJ1m.mjs → media-provider.runtime-Cd4dOeAN.mjs} +2 -2
- package/dist/.setup/{media-setup-BQ6-EW_y.mjs → media-setup-CYVQ5us2.mjs} +9 -9
- package/dist/.setup/{provider-DVEnxK8Q.mjs → provider-CwnlpLK5.mjs} +4 -6
- package/dist/.setup/{provider-discovery-SxEYhgMV.mjs → provider-discovery-D3_7fprf.mjs} +2 -2
- package/dist/.setup/{setup-DmjzzdfH.mjs → setup-Cd7vqUzv.mjs} +123 -132
- package/dist/.setup/{setup-D9_0z5GM.mjs → setup-DDuIGmUb.mjs} +8 -10
- package/dist/.setup/{stream-CSBkXmdm.mjs → stream-lxVe6M15.mjs} +39 -11
- package/dist/.source-module-urls/2a292bf2cc9d15ab4011c4fb7ee888280524a76675c662fb0329d5dc914f3fba.json +1 -0
- package/dist/index.js +113 -118
- package/dist/provider-discovery.js +1 -2
- package/dist/provider-policy-api.js +1 -1
- package/package.json +5 -5
- package/dist/.setup/{defaults-DX4XN2j1.mjs → defaults-CZr5jpH7.mjs} +1 -1
|
@@ -1,23 +1,232 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as resolveLlamaCppCatalogArtifact,
|
|
1
|
+
import { D as resolveLlamaCppModelSource, E as resolveLlamaCppModelCacheDir, S as resolveHomePath, a as DEFAULT_LLAMA_CPP_EMBEDDING_MODEL_ID, c as DEFAULT_LLAMA_CPP_EMBEDDING_MODEL_SIZE_BYTES, h as LLAMA_CPP_DEFAULT_PORT, n as DEFAULT_LLAMA_CPP_EMBEDDING_CACHE_FILE, o as DEFAULT_LLAMA_CPP_EMBEDDING_MODEL_REVISION, r as DEFAULT_LLAMA_CPP_EMBEDDING_CONTEXT_SIZE, s as DEFAULT_LLAMA_CPP_EMBEDDING_MODEL_SHA256, w as resolveLlamaCppDataDir, x as resolveCachedLlamaCppModelPath } from "./defaults-CZr5jpH7.mjs";
|
|
2
|
+
import { a as resolveLlamaCppCatalogArtifact, r as resolveLlamaCppMediaArtifact } from "./media-catalog-DgRc3mpW.mjs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { asOptionalRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
5
5
|
import { readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http";
|
|
6
6
|
import { fetchWithSsrFGuard, ssrfPolicyFromHttpBaseUrlAllowedOrigin } from "openclaw/plugin-sdk/ssrf-runtime";
|
|
7
7
|
import { fetchConfiguredLocalOriginWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime-internal";
|
|
8
8
|
import { createHash, randomUUID } from "node:crypto";
|
|
9
|
-
import
|
|
9
|
+
import fsp from "node:fs/promises";
|
|
10
10
|
import net from "node:net";
|
|
11
11
|
import { createAsyncLock } from "openclaw/plugin-sdk/async-lock-runtime";
|
|
12
|
+
import { createSubsystemLogger } from "openclaw/plugin-sdk/logging-core";
|
|
12
13
|
import { execFile } from "node:child_process";
|
|
13
|
-
import fs
|
|
14
|
+
import fs, { createWriteStream } from "node:fs";
|
|
15
|
+
import { pipeline } from "node:stream/promises";
|
|
14
16
|
import { sha256File } from "@openclaw/fs-safe/durability";
|
|
17
|
+
import { ARCHIVE_LIMIT_ERROR_CODE, ArchiveLimitError, extractArchive } from "openclaw/plugin-sdk/archive";
|
|
15
18
|
import { pruneMapToMaxSize } from "openclaw/plugin-sdk/collection-runtime";
|
|
16
19
|
import { toErrorObject } from "openclaw/plugin-sdk/error-runtime";
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
import { racePromiseWithAbortSignal } from "openclaw/plugin-sdk/time-runtime";
|
|
21
|
+
//#region extensions/llama-cpp/src/llama-server-assets.ts
|
|
22
|
+
const LLAMA_SERVER_RELEASE = "b10809";
|
|
23
|
+
const LLAMA_SERVER_COMMIT = "5266f24da75dc449bd56cbed7addb9c8e4a6a73e";
|
|
24
|
+
const MEBIBYTE$1 = 1048576;
|
|
25
|
+
const CUDA_ARCHIVE_LIMITS = {
|
|
26
|
+
maxArchiveBytes: 400 * MEBIBYTE$1,
|
|
27
|
+
maxExtractedBytes: 600 * MEBIBYTE$1,
|
|
28
|
+
maxEntryBytes: 521 * MEBIBYTE$1
|
|
29
|
+
};
|
|
30
|
+
const WINDOWS_X64_VC_RUNTIME = {
|
|
31
|
+
archive: "vc-redist",
|
|
32
|
+
name: "VC_redist.x64.exe",
|
|
33
|
+
url: "https://download.visualstudio.microsoft.com/download/pr/ebdab8e5-1d7b-4d9f-a11b-cbb1720c3b12/843068991DAAA1F73AD9F6239BCE4D0F6A07A51F18C37EA2A867E9BECA71295C/VC_redist.x64.exe",
|
|
34
|
+
sha256: "843068991daaa1f73ad9f6239bce4d0f6a07a51f18c37ea2a867e9beca71295c",
|
|
35
|
+
size: 18731856,
|
|
36
|
+
containerOffset: 63e4,
|
|
37
|
+
containerSize: 18091661,
|
|
38
|
+
nestedCabinet: "a4",
|
|
39
|
+
files: [
|
|
40
|
+
{
|
|
41
|
+
source: "msvcp140.dll_amd64",
|
|
42
|
+
target: "msvcp140.dll"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
source: "vcruntime140.dll_amd64",
|
|
46
|
+
target: "vcruntime140.dll"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
source: "vcruntime140_1.dll_amd64",
|
|
50
|
+
target: "vcruntime140_1.dll"
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
};
|
|
54
|
+
const WINDOWS_ARM64_VC_RUNTIME = {
|
|
55
|
+
archive: "vc-redist",
|
|
56
|
+
name: "VC_redist.arm64.exe",
|
|
57
|
+
url: "https://download.visualstudio.microsoft.com/download/pr/ece44298-3977-4f73-ab91-c13fe79cfea8/B70EF586669A620A0A30A1156969C05C6A3831DC8F8BC992DA75779D2A92F944/VC_redist.arm64.exe",
|
|
58
|
+
sha256: "b70ef586669a620a0a30a1156969c05c6a3831dc8f8bc992da75779d2a92f944",
|
|
59
|
+
size: 11870816,
|
|
60
|
+
containerOffset: 684112,
|
|
61
|
+
containerSize: 11176508,
|
|
62
|
+
nestedCabinet: "a1",
|
|
63
|
+
files: [{
|
|
64
|
+
source: "msvcp140.dll_arm64",
|
|
65
|
+
target: "msvcp140.dll"
|
|
66
|
+
}, {
|
|
67
|
+
source: "vcruntime140.dll_arm64",
|
|
68
|
+
target: "vcruntime140.dll"
|
|
69
|
+
}]
|
|
70
|
+
};
|
|
71
|
+
const MACOS_ALIASES = [
|
|
72
|
+
["libggml-rpc.0.23.0.dylib", ["libggml-rpc.0.dylib", "libggml-rpc.dylib"]],
|
|
73
|
+
["libllama.0.4.0.dylib", ["libllama.0.dylib", "libllama.dylib"]],
|
|
74
|
+
["libmtmd.0.4.0.dylib", ["libmtmd.0.dylib", "libmtmd.dylib"]],
|
|
75
|
+
["libggml.0.23.0.dylib", ["libggml.0.dylib", "libggml.dylib"]],
|
|
76
|
+
["libggml-base.0.23.0.dylib", ["libggml-base.0.dylib", "libggml-base.dylib"]],
|
|
77
|
+
["libggml-blas.0.23.0.dylib", ["libggml-blas.0.dylib", "libggml-blas.dylib"]],
|
|
78
|
+
["libllama-common.0.4.0.dylib", ["libllama-common.0.dylib", "libllama-common.dylib"]],
|
|
79
|
+
["libggml-cpu.0.23.0.dylib", ["libggml-cpu.0.dylib", "libggml-cpu.dylib"]]
|
|
80
|
+
];
|
|
81
|
+
const MACOS_METAL_ALIASES = [...MACOS_ALIASES, ["libggml-metal.0.23.0.dylib", ["libggml-metal.0.dylib", "libggml-metal.dylib"]]];
|
|
82
|
+
const LINUX_ALIASES = [
|
|
83
|
+
["libllama.so.0.4.0", ["libllama.so.0", "libllama.so"]],
|
|
84
|
+
["libggml.so.0.23.0", ["libggml.so.0", "libggml.so"]],
|
|
85
|
+
["libmtmd.so.0.4.0", ["libmtmd.so.0", "libmtmd.so"]],
|
|
86
|
+
["libggml-base.so.0.23.0", ["libggml-base.so.0", "libggml-base.so"]],
|
|
87
|
+
["libllama-common.so.0.4.0", ["libllama-common.so.0", "libllama-common.so"]]
|
|
88
|
+
];
|
|
89
|
+
const LLAMA_SERVER_ASSETS = [
|
|
90
|
+
{
|
|
91
|
+
platform: "darwin",
|
|
92
|
+
arch: "arm64",
|
|
93
|
+
backend: "metal",
|
|
94
|
+
archive: "tar.gz",
|
|
95
|
+
archiveRoot: `llama-${LLAMA_SERVER_RELEASE}`,
|
|
96
|
+
name: `llama-${LLAMA_SERVER_RELEASE}-bin-macos-arm64.tar.gz`,
|
|
97
|
+
sha256: "7d692df9e1e386e62f1c12b843903218041e6cd74c9415aa39a7ed3176f9eaa2",
|
|
98
|
+
sizeBytes: 11123196,
|
|
99
|
+
executable: "llama-server",
|
|
100
|
+
regularFileAliases: MACOS_METAL_ALIASES
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
platform: "darwin",
|
|
104
|
+
arch: "x64",
|
|
105
|
+
backend: "cpu",
|
|
106
|
+
archive: "tar.gz",
|
|
107
|
+
archiveRoot: `llama-${LLAMA_SERVER_RELEASE}`,
|
|
108
|
+
name: `llama-${LLAMA_SERVER_RELEASE}-bin-macos-x64.tar.gz`,
|
|
109
|
+
sha256: "13b34aa8a5d87341a21065a83f54a8167e1aaa6fe0d66065de01632a1ed64be6",
|
|
110
|
+
sizeBytes: 11175330,
|
|
111
|
+
executable: "llama-server",
|
|
112
|
+
regularFileAliases: MACOS_ALIASES
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
platform: "linux",
|
|
116
|
+
arch: "arm64",
|
|
117
|
+
backend: "cpu",
|
|
118
|
+
archive: "tar.gz",
|
|
119
|
+
archiveRoot: `llama-${LLAMA_SERVER_RELEASE}`,
|
|
120
|
+
name: `llama-${LLAMA_SERVER_RELEASE}-bin-ubuntu-arm64.tar.gz`,
|
|
121
|
+
sha256: "f2b7333971e1b7b42e9268bfdbfa30f5f56e2897156084d2251385df94aec358",
|
|
122
|
+
sizeBytes: 13380118,
|
|
123
|
+
executable: "llama-server",
|
|
124
|
+
regularFileAliases: LINUX_ALIASES
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
platform: "linux",
|
|
128
|
+
arch: "x64",
|
|
129
|
+
backend: "cpu",
|
|
130
|
+
archive: "tar.gz",
|
|
131
|
+
archiveRoot: `llama-${LLAMA_SERVER_RELEASE}`,
|
|
132
|
+
name: `llama-${LLAMA_SERVER_RELEASE}-bin-ubuntu-x64.tar.gz`,
|
|
133
|
+
sha256: "5e34434ddc6d03cd1584f403201aff0d4bd1a5793a72ff7e286532dfd1e4b941",
|
|
134
|
+
sizeBytes: 16734586,
|
|
135
|
+
executable: "llama-server",
|
|
136
|
+
regularFileAliases: LINUX_ALIASES
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
platform: "win32",
|
|
140
|
+
arch: "x64",
|
|
141
|
+
backend: "cuda",
|
|
142
|
+
archive: "zip",
|
|
143
|
+
archiveRoot: ".",
|
|
144
|
+
name: `llama-${LLAMA_SERVER_RELEASE}-bin-win-cuda-12.4-x64.zip`,
|
|
145
|
+
sha256: "c77bfcd9ed8d91e8721a2d6a290b907fddd4fa5412a47b21c6fa1709116b85f9",
|
|
146
|
+
sizeBytes: 253938543,
|
|
147
|
+
executable: "llama-server.exe",
|
|
148
|
+
regularFileAliases: [],
|
|
149
|
+
limits: CUDA_ARCHIVE_LIMITS,
|
|
150
|
+
dependencies: [{
|
|
151
|
+
archive: "zip",
|
|
152
|
+
archiveRoot: ".",
|
|
153
|
+
name: "cudart-llama-bin-win-cuda-12.4-x64.zip",
|
|
154
|
+
sha256: "8c79a9b226de4b3cacfd1f83d24f962d0773be79f1e7b75c6af4ded7e32ae1d6",
|
|
155
|
+
sizeBytes: 391443627,
|
|
156
|
+
regularFileAliases: [],
|
|
157
|
+
files: [
|
|
158
|
+
"cublas64_12.dll",
|
|
159
|
+
"cublasLt64_12.dll",
|
|
160
|
+
"cudart64_12.dll"
|
|
161
|
+
],
|
|
162
|
+
limits: {
|
|
163
|
+
...CUDA_ARCHIVE_LIMITS,
|
|
164
|
+
maxEntries: 3
|
|
165
|
+
}
|
|
166
|
+
}, WINDOWS_X64_VC_RUNTIME]
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
platform: "win32",
|
|
170
|
+
arch: "arm64",
|
|
171
|
+
backend: "cpu",
|
|
172
|
+
archive: "zip",
|
|
173
|
+
archiveRoot: ".",
|
|
174
|
+
name: `llama-${LLAMA_SERVER_RELEASE}-bin-win-cpu-arm64.zip`,
|
|
175
|
+
sha256: "c1058fe5764a687275c8d20d6bbc1454e787cdbb8ebb8c37a2f959f2b144dc77",
|
|
176
|
+
sizeBytes: 11974499,
|
|
177
|
+
executable: "llama-server.exe",
|
|
178
|
+
regularFileAliases: [],
|
|
179
|
+
dependencies: [WINDOWS_ARM64_VC_RUNTIME]
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
platform: "win32",
|
|
183
|
+
arch: "x64",
|
|
184
|
+
backend: "cpu",
|
|
185
|
+
archive: "zip",
|
|
186
|
+
archiveRoot: ".",
|
|
187
|
+
name: `llama-${LLAMA_SERVER_RELEASE}-bin-win-cpu-x64.zip`,
|
|
188
|
+
sha256: "9df3158ed228a641a4b127942d7f459f24c9e13f04682659d05c00c80099b6b5",
|
|
189
|
+
sizeBytes: 18407457,
|
|
190
|
+
executable: "llama-server.exe",
|
|
191
|
+
regularFileAliases: [],
|
|
192
|
+
dependencies: [WINDOWS_X64_VC_RUNTIME]
|
|
193
|
+
}
|
|
194
|
+
];
|
|
195
|
+
function selectLlamaServerAsset(platform = process.platform, arch = process.arch, acceleration) {
|
|
196
|
+
const backend = acceleration?.kind ?? (platform === "darwin" && arch === "arm64" ? "metal" : "cpu");
|
|
197
|
+
if (backend === "cuda" && acceleration?.kind === "cuda") {
|
|
198
|
+
if (platform !== "win32" || arch !== "x64") throw new Error(`No verified CUDA llama-server ${LLAMA_SERVER_RELEASE} build is available for ${platform}/${arch}. Install a CUDA-enabled llama-server manually and configure its absolute path, or explicitly choose CPU setup.`);
|
|
199
|
+
if (!(acceleration.devices.length > 0 && acceleration.devices.every((device) => {
|
|
200
|
+
const version = /^(\d+)\.(\d+)(?:\.\d+)?$/u.exec(device.driverVersion);
|
|
201
|
+
return version && (Number(version[1]) > 551 || Number(version[1]) === 551 && Number(version[2]) >= 78) && (device.computeCapability === void 0 || device.computeCapability >= 5);
|
|
202
|
+
}))) throw new Error("The verified CUDA 12.4 build requires NVIDIA driver 551.78 or newer and compute capability 5.0 or newer. Update the NVIDIA driver, configure a compatible llama-server manually, or explicitly choose CPU setup.");
|
|
203
|
+
}
|
|
204
|
+
const asset = LLAMA_SERVER_ASSETS.find((candidate) => candidate.platform === platform && candidate.arch === arch && candidate.backend === backend);
|
|
205
|
+
if (!asset) throw new Error(`No verified llama-server ${LLAMA_SERVER_RELEASE} build is available for ${platform}/${arch}. Install a compatible llama-server manually, then rerun llama.cpp setup with its absolute path.`);
|
|
206
|
+
return asset;
|
|
207
|
+
}
|
|
208
|
+
function resolveManagedLlamaServerPaths(asset = selectLlamaServerAsset()) {
|
|
209
|
+
const installDir = path.join(resolveLlamaCppDataDir(), LLAMA_SERVER_RELEASE, `${asset.platform}-${asset.arch}${asset.backend === "cuda" ? "-cuda-12.4" : ""}`);
|
|
210
|
+
return {
|
|
211
|
+
installDir,
|
|
212
|
+
command: path.join(installDir, asset.executable),
|
|
213
|
+
presetPath: path.join(resolveLlamaCppDataDir(), "models.ini")
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function findManagedLlamaServerAsset(command, platform = process.platform, arch = process.arch) {
|
|
217
|
+
return LLAMA_SERVER_ASSETS.find((asset) => asset.platform === platform && asset.arch === arch && resolveManagedLlamaServerPaths(asset).command === command);
|
|
218
|
+
}
|
|
219
|
+
//#endregion
|
|
220
|
+
//#region extensions/llama-cpp/src/runtime-error.ts
|
|
221
|
+
function formatLlamaServerRuntimeError(error) {
|
|
222
|
+
const cause = error instanceof Error ? error : new Error(String(error));
|
|
223
|
+
const library = cause.message.match(/error while loading shared libraries: ([\w.+-]+):/u)?.[1];
|
|
224
|
+
if (!library) return cause;
|
|
225
|
+
const runtime = /^lib(?:ssl|crypto)\.so\.3$/u.test(library) ? "OpenSSL 3 (libssl3t64 or libssl3 on Debian/Ubuntu)" : library.startsWith("libgomp.so.") ? "OpenMP (libgomp1 on Debian/Ubuntu or libgomp on Fedora)" : library;
|
|
226
|
+
return new Error(`Missing llama-server shared library ${library}. Install the ${runtime} runtime using your system package manager, then rerun llama.cpp setup. Detail: ${cause.message}`, { cause });
|
|
227
|
+
}
|
|
228
|
+
//#endregion
|
|
229
|
+
//#region extensions/llama-cpp/src/llama-server-install.ts
|
|
21
230
|
const MEBIBYTE = 1048576;
|
|
22
231
|
const LLAMA_ARCHIVE_LIMITS = {
|
|
23
232
|
maxArchiveBytes: 256 * MEBIBYTE,
|
|
@@ -27,37 +236,24 @@ const LLAMA_ARCHIVE_LIMITS = {
|
|
|
27
236
|
maxMetaEntryBytes: MEBIBYTE
|
|
28
237
|
};
|
|
29
238
|
const MAX_ALIAS_BYTES = 512 * MEBIBYTE;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
239
|
+
const CAB_HEADER_SIZE = 12;
|
|
240
|
+
const EXPAND_TIMEOUT_MS = 6e4;
|
|
241
|
+
async function assertRegularFile(filePath, label, source = "llama-server archive") {
|
|
242
|
+
const stat = await fsp.lstat(filePath).catch(() => void 0);
|
|
243
|
+
if (!stat?.isFile() || stat.nlink > 1) throw new Error(`${source} does not contain regular ${label}`);
|
|
33
244
|
return stat;
|
|
34
245
|
}
|
|
35
|
-
async function
|
|
36
|
-
|
|
37
|
-
let copiedBytes = 0;
|
|
38
|
-
for (const [source, aliases] of params.asset.regularFileAliases) {
|
|
39
|
-
const sourcePath = path.join(params.rootDir, source);
|
|
40
|
-
const sourceStat = await assertRegularFile$1(sourcePath, `alias source ${source}`);
|
|
41
|
-
for (const alias of aliases) {
|
|
42
|
-
copiedBytes += sourceStat.size;
|
|
43
|
-
if (copiedBytes > MAX_ALIAS_BYTES) throw new ArchiveLimitError(ARCHIVE_LIMIT_ERROR_CODE.EXTRACTED_SIZE_EXCEEDS_LIMIT);
|
|
44
|
-
const aliasPath = path.join(params.rootDir, alias);
|
|
45
|
-
await fs.copyFile(sourcePath, aliasPath, constants.COPYFILE_EXCL);
|
|
46
|
-
await fs.chmod(aliasPath, sourceStat.mode & 511);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
async function extractVerifiedArchive(params) {
|
|
51
|
-
const isTar = params.asset.archive === "tar.gz";
|
|
246
|
+
async function extractVerifiedArchive(archivePath, destDir, asset, requiredFiles) {
|
|
247
|
+
const isTar = asset.archive === "tar.gz";
|
|
52
248
|
const limits = {
|
|
53
249
|
...LLAMA_ARCHIVE_LIMITS,
|
|
54
|
-
...
|
|
250
|
+
...asset.limits
|
|
55
251
|
};
|
|
56
|
-
const aliasCount =
|
|
252
|
+
const aliasCount = asset.regularFileAliases.reduce((count, [, aliases]) => count + aliases.length, 0);
|
|
57
253
|
if (aliasCount > limits.maxEntries) throw new ArchiveLimitError(ARCHIVE_LIMIT_ERROR_CODE.ENTRY_COUNT_EXCEEDS_LIMIT);
|
|
58
254
|
await extractArchive({
|
|
59
|
-
archivePath
|
|
60
|
-
destDir
|
|
255
|
+
archivePath,
|
|
256
|
+
destDir,
|
|
61
257
|
kind: isTar ? "tar" : "zip",
|
|
62
258
|
timeoutMs: 0,
|
|
63
259
|
limits: {
|
|
@@ -68,116 +264,28 @@ async function extractVerifiedArchive(params) {
|
|
|
68
264
|
entryFilter: (entry) => entry.kind === "symlink" ? "skip" : "extract",
|
|
69
265
|
onFiltered: "skip-entry"
|
|
70
266
|
});
|
|
71
|
-
const rootDir = path.join(
|
|
72
|
-
await
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
});
|
|
85
|
-
return path.join(rootDir, params.asset.executable);
|
|
86
|
-
}
|
|
87
|
-
async function extractLlamaServerDependencyArchive(params) {
|
|
88
|
-
return await extractVerifiedArchive({
|
|
89
|
-
...params,
|
|
90
|
-
requiredFiles: params.asset.files
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
//#endregion
|
|
94
|
-
//#region extensions/llama-cpp/src/llama-server-vc-runtime.ts
|
|
95
|
-
const CAB_HEADER_SIZE = 12;
|
|
96
|
-
const EXPAND_TIMEOUT_MS = 6e4;
|
|
97
|
-
async function extractPinnedContainer(params) {
|
|
98
|
-
const source = await fs.open(params.bundlePath, "r");
|
|
99
|
-
try {
|
|
100
|
-
const stat = await source.stat();
|
|
101
|
-
if (!stat.isFile() || stat.size !== params.asset.size) throw new Error(`Visual C++ runtime bundle size mismatch: expected ${params.asset.size}, got ${stat.size}`);
|
|
102
|
-
if (params.asset.containerOffset < 0 || params.asset.containerSize < CAB_HEADER_SIZE || params.asset.containerOffset + params.asset.containerSize > stat.size) throw new Error("invalid Visual C++ runtime container bounds");
|
|
103
|
-
const header = Buffer.alloc(CAB_HEADER_SIZE);
|
|
104
|
-
if ((await source.read(header, 0, header.length, params.asset.containerOffset)).bytesRead !== header.length || header.subarray(0, 4).toString("ascii") !== "MSCF" || header.readUInt32LE(8) !== params.asset.containerSize) throw new Error("Visual C++ runtime bundle does not contain the pinned cabinet");
|
|
105
|
-
await pipeline(source.createReadStream({
|
|
106
|
-
start: params.asset.containerOffset,
|
|
107
|
-
end: params.asset.containerOffset + params.asset.containerSize - 1,
|
|
108
|
-
autoClose: false
|
|
109
|
-
}), createWriteStream(params.containerPath, {
|
|
110
|
-
flags: "wx",
|
|
111
|
-
mode: 384
|
|
112
|
-
}), { signal: params.signal });
|
|
113
|
-
} finally {
|
|
114
|
-
await source.close();
|
|
267
|
+
const rootDir = path.join(destDir, asset.archiveRoot);
|
|
268
|
+
for (const file of requiredFiles) await assertRegularFile(path.join(rootDir, file), `file ${file}`);
|
|
269
|
+
let copiedBytes = 0;
|
|
270
|
+
for (const [source, aliases] of asset.regularFileAliases) {
|
|
271
|
+
const sourcePath = path.join(rootDir, source);
|
|
272
|
+
const sourceStat = await assertRegularFile(sourcePath, `alias source ${source}`);
|
|
273
|
+
for (const alias of aliases) {
|
|
274
|
+
copiedBytes += sourceStat.size;
|
|
275
|
+
if (copiedBytes > MAX_ALIAS_BYTES) throw new ArchiveLimitError(ARCHIVE_LIMIT_ERROR_CODE.EXTRACTED_SIZE_EXCEEDS_LIMIT);
|
|
276
|
+
const aliasPath = path.join(rootDir, alias);
|
|
277
|
+
await fsp.copyFile(sourcePath, aliasPath, fs.constants.COPYFILE_EXCL);
|
|
278
|
+
await fsp.chmod(aliasPath, sourceStat.mode & 511);
|
|
279
|
+
}
|
|
115
280
|
}
|
|
116
|
-
|
|
117
|
-
function resolveExpandCommand() {
|
|
118
|
-
const systemRoot = process.env.SystemRoot;
|
|
119
|
-
if (!systemRoot || !path.win32.isAbsolute(systemRoot)) throw new Error("SystemRoot is unavailable; cannot extract the Visual C++ runtime");
|
|
120
|
-
return path.win32.join(systemRoot, "System32", "expand.exe");
|
|
281
|
+
return rootDir;
|
|
121
282
|
}
|
|
122
283
|
async function runExpand(command, args, signal) {
|
|
123
|
-
await
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
signal,
|
|
127
|
-
windowsHide: true
|
|
128
|
-
}, (error, _stdout, stderr) => {
|
|
129
|
-
if (error) {
|
|
130
|
-
const detail = stderr.trim();
|
|
131
|
-
reject(new Error(`Failed to extract the Visual C++ runtime${detail ? `: ${detail}` : ""}`, { cause: error }));
|
|
132
|
-
} else resolve();
|
|
133
|
-
});
|
|
284
|
+
await runServerCommand(command, args, signal, EXPAND_TIMEOUT_MS, (error, stderr) => {
|
|
285
|
+
const detail = stderr.trim();
|
|
286
|
+
return new Error(`Failed to extract the Visual C++ runtime${detail ? `: ${detail}` : ""}`, { cause: error });
|
|
134
287
|
});
|
|
135
288
|
}
|
|
136
|
-
async function assertRegularFile(filePath, label) {
|
|
137
|
-
const stat = await fs.lstat(filePath).catch(() => void 0);
|
|
138
|
-
if (!stat?.isFile() || stat.nlink > 1) throw new Error(`Visual C++ runtime does not contain regular file ${label}`);
|
|
139
|
-
}
|
|
140
|
-
/** Extracts the pinned Microsoft redistributable DLLs without installing machine-wide state. */
|
|
141
|
-
async function extractWindowsVcRuntime(params) {
|
|
142
|
-
const { nestedCabinet, files } = params.asset;
|
|
143
|
-
const expandCommand = resolveExpandCommand();
|
|
144
|
-
const containerPath = path.join(params.destDir, "bundle.cab");
|
|
145
|
-
const nestedDir = path.join(params.destDir, "nested");
|
|
146
|
-
const filesDir = path.join(params.destDir, "files");
|
|
147
|
-
await Promise.all([
|
|
148
|
-
fs.mkdir(params.destDir, { recursive: true }),
|
|
149
|
-
fs.mkdir(nestedDir, { recursive: true }),
|
|
150
|
-
fs.mkdir(filesDir, { recursive: true })
|
|
151
|
-
]);
|
|
152
|
-
await extractPinnedContainer({
|
|
153
|
-
asset: params.asset,
|
|
154
|
-
bundlePath: params.bundlePath,
|
|
155
|
-
containerPath,
|
|
156
|
-
signal: params.signal
|
|
157
|
-
});
|
|
158
|
-
await runExpand(expandCommand, [
|
|
159
|
-
`-F:${nestedCabinet}`,
|
|
160
|
-
containerPath,
|
|
161
|
-
nestedDir
|
|
162
|
-
], params.signal);
|
|
163
|
-
const nestedPath = path.join(nestedDir, nestedCabinet);
|
|
164
|
-
await assertRegularFile(nestedPath, nestedCabinet);
|
|
165
|
-
for (const file of files) {
|
|
166
|
-
params.signal?.throwIfAborted();
|
|
167
|
-
await runExpand(expandCommand, [
|
|
168
|
-
`-F:${file.source}`,
|
|
169
|
-
nestedPath,
|
|
170
|
-
filesDir
|
|
171
|
-
], params.signal);
|
|
172
|
-
const sourcePath = path.join(filesDir, file.source);
|
|
173
|
-
await assertRegularFile(sourcePath, file.source);
|
|
174
|
-
const targetPath = path.join(params.destDir, file.target);
|
|
175
|
-
await fs.copyFile(sourcePath, targetPath, constants.COPYFILE_EXCL);
|
|
176
|
-
}
|
|
177
|
-
return params.destDir;
|
|
178
|
-
}
|
|
179
|
-
//#endregion
|
|
180
|
-
//#region extensions/llama-cpp/src/llama-server-install.ts
|
|
181
289
|
const DOWNLOAD_TIMEOUT_MS = 18e5;
|
|
182
290
|
const PROBE_TIMEOUT_MS = 15e3;
|
|
183
291
|
const VERSION_TIMEOUT_MS = 12e4;
|
|
@@ -235,16 +343,16 @@ function rememberVerifiedFile(filePath, stat, sha256) {
|
|
|
235
343
|
async function sha256File$1(filePath, signal) {
|
|
236
344
|
signal?.throwIfAborted();
|
|
237
345
|
try {
|
|
238
|
-
const identity = fileIdentity(await
|
|
346
|
+
const identity = fileIdentity(await fsp.stat(filePath, { bigint: true }));
|
|
239
347
|
const verified = verifiedFiles.get(filePath);
|
|
240
348
|
if (verified?.identity === identity) return verified.sha256;
|
|
241
349
|
verifiedFiles.delete(filePath);
|
|
242
|
-
const handle = await
|
|
350
|
+
const handle = await fsp.open(filePath, "r");
|
|
243
351
|
try {
|
|
244
352
|
const before = fileIdentity(await handle.stat({ bigint: true }));
|
|
245
353
|
const { digest: sha256 } = await sha256File(handle, { signal });
|
|
246
354
|
const after = await handle.stat({ bigint: true });
|
|
247
|
-
if (before !== fileIdentity(after) || before !== fileIdentity(await
|
|
355
|
+
if (before !== fileIdentity(after) || before !== fileIdentity(await fsp.stat(filePath, { bigint: true }))) throw new Error(`File changed during integrity verification: ${filePath}. Retry setup.`);
|
|
248
356
|
rememberVerifiedFile(filePath, after, sha256);
|
|
249
357
|
return sha256;
|
|
250
358
|
} finally {
|
|
@@ -265,7 +373,7 @@ function readResponseSha256(response) {
|
|
|
265
373
|
}
|
|
266
374
|
async function downloadVerifiedFile(params) {
|
|
267
375
|
const partialPath = `${params.destination}.partial-${randomUUID()}`;
|
|
268
|
-
await
|
|
376
|
+
await fsp.mkdir(path.dirname(params.destination), { recursive: true });
|
|
269
377
|
const { fetchWithSsrFGuard, ssrfPolicyFromHttpBaseUrlAllowedOrigin } = await import("openclaw/plugin-sdk/ssrf-runtime");
|
|
270
378
|
try {
|
|
271
379
|
const { response, release } = await fetchWithSsrFGuard({
|
|
@@ -282,7 +390,7 @@ async function downloadVerifiedFile(params) {
|
|
|
282
390
|
if (!expectedSha256 && params.requireServerDigest) throw new Error("the download server did not provide a SHA-256 digest; download the GGUF manually and configure its local path");
|
|
283
391
|
const contentLength = Number(response.headers.get("content-length"));
|
|
284
392
|
const totalSize = params.expectedSize ?? (Number.isFinite(contentLength) && contentLength > 0 ? contentLength : 0);
|
|
285
|
-
const handle = await
|
|
393
|
+
const handle = await fsp.open(partialPath, "wx", 384);
|
|
286
394
|
const hash = createHash("sha256");
|
|
287
395
|
let downloadedSize = 0;
|
|
288
396
|
let previousSize = 0;
|
|
@@ -312,7 +420,7 @@ async function downloadVerifiedFile(params) {
|
|
|
312
420
|
if (expectedSha256 && actualSha256 !== expectedSha256.toLowerCase()) throw new Error(`download SHA-256 mismatch: expected ${expectedSha256}, got ${actualSha256}`);
|
|
313
421
|
const completed = await handle.stat({ bigint: true });
|
|
314
422
|
params.signal?.throwIfAborted();
|
|
315
|
-
await
|
|
423
|
+
await fsp.rename(partialPath, params.destination);
|
|
316
424
|
const published = await handle.stat({ bigint: true });
|
|
317
425
|
if (completed.size === published.size && completed.mtimeNs === published.mtimeNs) rememberVerifiedFile(params.destination, published, actualSha256);
|
|
318
426
|
} finally {
|
|
@@ -322,34 +430,28 @@ async function downloadVerifiedFile(params) {
|
|
|
322
430
|
await release();
|
|
323
431
|
}
|
|
324
432
|
} finally {
|
|
325
|
-
await
|
|
433
|
+
await fsp.rm(partialPath, { force: true }).catch(() => void 0);
|
|
326
434
|
}
|
|
327
435
|
}
|
|
328
|
-
async function runServerCommand(command, args, signal, timeoutMs = PROBE_TIMEOUT_MS) {
|
|
436
|
+
async function runServerCommand(command, args, signal, timeoutMs = PROBE_TIMEOUT_MS, formatError = formatRuntimeDependencyError) {
|
|
329
437
|
return await new Promise((resolve, reject) => {
|
|
330
438
|
execFile(command, args, {
|
|
331
439
|
timeout: timeoutMs,
|
|
332
440
|
signal,
|
|
333
441
|
windowsHide: true
|
|
334
442
|
}, (error, stdout, stderr) => {
|
|
335
|
-
if (error) reject(
|
|
443
|
+
if (error) reject(signal?.aborted ? toErrorObject(signal.reason, "Installation cancelled") : formatError(error, stderr));
|
|
336
444
|
else resolve(`${stdout}${stderr}`.trim());
|
|
337
445
|
});
|
|
338
446
|
});
|
|
339
447
|
}
|
|
340
448
|
function formatRuntimeDependencyError(error) {
|
|
341
449
|
const detail = error instanceof Error ? error.message : String(error);
|
|
342
|
-
if (process.platform === "linux") return new Error(`The verified llama-server build could not start. Install the OpenMP runtime (for example libgomp1 on Debian/Ubuntu or libgomp on Fedora), then rerun llama.cpp setup. Detail: ${detail}`, { cause: error });
|
|
343
450
|
if (process.platform === "win32") return new Error(`The verified llama-server build could not start with its app-local Visual C++ runtime. Rerun llama.cpp setup to reinstall it, or configure a compatible llama-server manually. Detail: ${detail}`, { cause: error });
|
|
344
|
-
return
|
|
451
|
+
return formatLlamaServerRuntimeError(error);
|
|
345
452
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
return await runServerCommand(command, ["--version"], signal, VERSION_TIMEOUT_MS);
|
|
349
|
-
} catch (error) {
|
|
350
|
-
signal?.throwIfAborted();
|
|
351
|
-
throw formatRuntimeDependencyError(error);
|
|
352
|
-
}
|
|
453
|
+
function queryServerVersion(command, signal) {
|
|
454
|
+
return runServerCommand(command, ["--version"], signal, VERSION_TIMEOUT_MS);
|
|
353
455
|
}
|
|
354
456
|
function validateServerVersionOutput(command, version) {
|
|
355
457
|
const match = (version.split(/\r?\n/u, 1)[0]?.trim() ?? "").match(/^version: .+ \(build (\d+), commit ([a-f\d]{9})\)$/u);
|
|
@@ -367,32 +469,6 @@ async function validateInstalledServer(command, asset, signal) {
|
|
|
367
469
|
validateServerVersionOutput(command, await queryServerVersion(command, signal));
|
|
368
470
|
await validateCudaServer(command, asset, signal);
|
|
369
471
|
}
|
|
370
|
-
async function stageLlamaServerDependency(params) {
|
|
371
|
-
const { dependency, extractDir, extractedRoot, index, options } = params;
|
|
372
|
-
options.signal?.throwIfAborted();
|
|
373
|
-
const dependencyArchive = path.join(extractDir, dependency.name);
|
|
374
|
-
await downloadVerifiedFile({
|
|
375
|
-
url: assetUrl(dependency),
|
|
376
|
-
destination: dependencyArchive,
|
|
377
|
-
expectedSha256: dependency.sha256,
|
|
378
|
-
expectedSize: dependency.archive === "vc-redist" ? dependency.size : void 0,
|
|
379
|
-
signal: options.signal,
|
|
380
|
-
onProgress: options.onProgress
|
|
381
|
-
});
|
|
382
|
-
const dependencyExtractDir = path.join(extractDir, `dependency-${index}`);
|
|
383
|
-
await fs.mkdir(dependencyExtractDir);
|
|
384
|
-
const dependencyRoot = dependency.archive === "vc-redist" ? await extractWindowsVcRuntime({
|
|
385
|
-
bundlePath: dependencyArchive,
|
|
386
|
-
destDir: dependencyExtractDir,
|
|
387
|
-
asset: dependency,
|
|
388
|
-
signal: options.signal
|
|
389
|
-
}) : await extractLlamaServerDependencyArchive({
|
|
390
|
-
archivePath: dependencyArchive,
|
|
391
|
-
destDir: dependencyExtractDir,
|
|
392
|
-
asset: dependency
|
|
393
|
-
});
|
|
394
|
-
for (const file of dependencyFiles(dependency)) await fs.copyFile(path.join(dependencyRoot, file), path.join(extractedRoot, file), fs$1.constants.COPYFILE_EXCL);
|
|
395
|
-
}
|
|
396
472
|
function formatVcRuntimeFallbackError(startupError, fallbackError) {
|
|
397
473
|
const startupDetail = startupError instanceof Error ? startupError.message : String(startupError);
|
|
398
474
|
const fallbackDetail = fallbackError instanceof Error ? fallbackError.message : String(fallbackError);
|
|
@@ -402,7 +478,7 @@ async function installLlamaServer(asset, options) {
|
|
|
402
478
|
options.signal?.throwIfAborted();
|
|
403
479
|
assertSupportedLinuxRuntime(asset);
|
|
404
480
|
const { installDir, command } = resolveManagedLlamaServerPaths(asset);
|
|
405
|
-
if (await
|
|
481
|
+
if (await fsp.stat(command).then((stat) => stat.isFile()).catch(() => false)) {
|
|
406
482
|
try {
|
|
407
483
|
await validateInstalledServer(command, asset, options.signal);
|
|
408
484
|
} catch (error) {
|
|
@@ -415,7 +491,7 @@ async function installLlamaServer(asset, options) {
|
|
|
415
491
|
const dataDir = resolveLlamaCppDataDir();
|
|
416
492
|
const archivePath = path.join(dataDir, `.download-${randomUUID()}-${asset.name}`);
|
|
417
493
|
const extractDir = path.join(dataDir, `.extract-${randomUUID()}`);
|
|
418
|
-
await
|
|
494
|
+
await fsp.mkdir(dataDir, { recursive: true });
|
|
419
495
|
try {
|
|
420
496
|
await downloadVerifiedFile({
|
|
421
497
|
url: assetUrl(asset),
|
|
@@ -425,28 +501,82 @@ async function installLlamaServer(asset, options) {
|
|
|
425
501
|
onProgress: options.onProgress
|
|
426
502
|
});
|
|
427
503
|
const serverExtractDir = path.join(extractDir, "server");
|
|
428
|
-
await
|
|
429
|
-
const extractedCommand = await
|
|
430
|
-
archivePath,
|
|
431
|
-
destDir: serverExtractDir,
|
|
432
|
-
asset
|
|
433
|
-
});
|
|
504
|
+
await fsp.mkdir(serverExtractDir, { recursive: true });
|
|
505
|
+
const extractedCommand = path.join(await extractVerifiedArchive(archivePath, serverExtractDir, asset, [asset.executable]), asset.executable);
|
|
434
506
|
const extractedRoot = path.dirname(extractedCommand);
|
|
507
|
+
const stageDependency = async (dependency, index) => {
|
|
508
|
+
options.signal?.throwIfAborted();
|
|
509
|
+
const dependencyArchive = path.join(extractDir, dependency.name);
|
|
510
|
+
await downloadVerifiedFile({
|
|
511
|
+
url: assetUrl(dependency),
|
|
512
|
+
destination: dependencyArchive,
|
|
513
|
+
expectedSha256: dependency.sha256,
|
|
514
|
+
expectedSize: dependency.archive === "vc-redist" ? dependency.size : void 0,
|
|
515
|
+
signal: options.signal,
|
|
516
|
+
onProgress: options.onProgress
|
|
517
|
+
});
|
|
518
|
+
const dependencyExtractDir = path.join(extractDir, `dependency-${index}`);
|
|
519
|
+
await fsp.mkdir(dependencyExtractDir);
|
|
520
|
+
let dependencyRoot;
|
|
521
|
+
if (dependency.archive === "vc-redist") {
|
|
522
|
+
const { nestedCabinet, files } = dependency;
|
|
523
|
+
const systemRoot = process.env.SystemRoot;
|
|
524
|
+
if (!systemRoot || !path.win32.isAbsolute(systemRoot)) throw new Error("SystemRoot is unavailable; cannot extract the Visual C++ runtime");
|
|
525
|
+
const expandCommand = path.win32.join(systemRoot, "System32", "expand.exe");
|
|
526
|
+
const containerPath = path.join(dependencyExtractDir, "bundle.cab");
|
|
527
|
+
const nestedDir = path.join(dependencyExtractDir, "nested");
|
|
528
|
+
const filesDir = path.join(dependencyExtractDir, "files");
|
|
529
|
+
await Promise.all([fsp.mkdir(nestedDir, { recursive: true }), fsp.mkdir(filesDir, { recursive: true })]);
|
|
530
|
+
const source = await fsp.open(dependencyArchive, "r");
|
|
531
|
+
try {
|
|
532
|
+
const stat = await source.stat();
|
|
533
|
+
if (!stat.isFile() || stat.size !== dependency.size) throw new Error(`Visual C++ runtime bundle size mismatch: expected ${dependency.size}, got ${stat.size}`);
|
|
534
|
+
if (dependency.containerOffset < 0 || dependency.containerSize < CAB_HEADER_SIZE || dependency.containerOffset + dependency.containerSize > stat.size) throw new Error("invalid Visual C++ runtime container bounds");
|
|
535
|
+
const header = Buffer.alloc(CAB_HEADER_SIZE);
|
|
536
|
+
if ((await source.read(header, 0, header.length, dependency.containerOffset)).bytesRead !== header.length || header.subarray(0, 4).toString("ascii") !== "MSCF" || header.readUInt32LE(8) !== dependency.containerSize) throw new Error("Visual C++ runtime bundle does not contain the pinned cabinet");
|
|
537
|
+
await pipeline(source.createReadStream({
|
|
538
|
+
start: dependency.containerOffset,
|
|
539
|
+
end: dependency.containerOffset + dependency.containerSize - 1,
|
|
540
|
+
autoClose: false
|
|
541
|
+
}), createWriteStream(containerPath, {
|
|
542
|
+
flags: "wx",
|
|
543
|
+
mode: 384
|
|
544
|
+
}), { signal: options.signal });
|
|
545
|
+
} finally {
|
|
546
|
+
await source.close();
|
|
547
|
+
}
|
|
548
|
+
await runExpand(expandCommand, [
|
|
549
|
+
`-F:${nestedCabinet}`,
|
|
550
|
+
containerPath,
|
|
551
|
+
nestedDir
|
|
552
|
+
], options.signal);
|
|
553
|
+
const nestedPath = path.join(nestedDir, nestedCabinet);
|
|
554
|
+
await assertRegularFile(nestedPath, `file ${nestedCabinet}`, "Visual C++ runtime");
|
|
555
|
+
for (const file of files) {
|
|
556
|
+
options.signal?.throwIfAborted();
|
|
557
|
+
await runExpand(expandCommand, [
|
|
558
|
+
`-F:${file.source}`,
|
|
559
|
+
nestedPath,
|
|
560
|
+
filesDir
|
|
561
|
+
], options.signal);
|
|
562
|
+
const sourcePath = path.join(filesDir, file.source);
|
|
563
|
+
await assertRegularFile(sourcePath, `file ${file.source}`, "Visual C++ runtime");
|
|
564
|
+
await fsp.copyFile(sourcePath, path.join(dependencyExtractDir, file.target), fs.constants.COPYFILE_EXCL);
|
|
565
|
+
}
|
|
566
|
+
dependencyRoot = dependencyExtractDir;
|
|
567
|
+
} else dependencyRoot = await extractVerifiedArchive(dependencyArchive, dependencyExtractDir, dependency, dependency.files);
|
|
568
|
+
const files = dependency.archive === "vc-redist" ? dependency.files.map((file) => file.target) : dependency.files;
|
|
569
|
+
for (const file of files) await fsp.copyFile(path.join(dependencyRoot, file), path.join(extractedRoot, file), fs.constants.COPYFILE_EXCL);
|
|
570
|
+
};
|
|
435
571
|
const dependencies = asset.dependencies ?? [];
|
|
436
572
|
const windowsRuntimeDependencies = [];
|
|
437
573
|
for (const [index, dependency] of dependencies.entries()) if (dependency.archive === "vc-redist") windowsRuntimeDependencies.push({
|
|
438
574
|
dependency,
|
|
439
575
|
index
|
|
440
576
|
});
|
|
441
|
-
else await
|
|
442
|
-
dependency,
|
|
443
|
-
index,
|
|
444
|
-
extractDir,
|
|
445
|
-
extractedRoot,
|
|
446
|
-
options
|
|
447
|
-
});
|
|
577
|
+
else await stageDependency(dependency, index);
|
|
448
578
|
options.signal?.throwIfAborted();
|
|
449
|
-
await
|
|
579
|
+
await fsp.chmod(extractedCommand, 493);
|
|
450
580
|
if (windowsRuntimeDependencies.length > 0) {
|
|
451
581
|
let version;
|
|
452
582
|
try {
|
|
@@ -454,13 +584,7 @@ async function installLlamaServer(asset, options) {
|
|
|
454
584
|
} catch (startupError) {
|
|
455
585
|
options.signal?.throwIfAborted();
|
|
456
586
|
try {
|
|
457
|
-
for (const { dependency, index } of windowsRuntimeDependencies) await
|
|
458
|
-
dependency,
|
|
459
|
-
index,
|
|
460
|
-
extractDir,
|
|
461
|
-
extractedRoot,
|
|
462
|
-
options
|
|
463
|
-
});
|
|
587
|
+
for (const { dependency, index } of windowsRuntimeDependencies) await stageDependency(dependency, index);
|
|
464
588
|
} catch (stagingError) {
|
|
465
589
|
options.signal?.throwIfAborted();
|
|
466
590
|
throw formatVcRuntimeFallbackError(startupError, stagingError);
|
|
@@ -475,25 +599,21 @@ async function installLlamaServer(asset, options) {
|
|
|
475
599
|
validateServerVersionOutput(extractedCommand, version);
|
|
476
600
|
await validateCudaServer(extractedCommand, asset, options.signal);
|
|
477
601
|
} else await validateInstalledServer(extractedCommand, asset, options.signal);
|
|
478
|
-
await
|
|
602
|
+
await fsp.mkdir(path.dirname(installDir), { recursive: true });
|
|
479
603
|
options.signal?.throwIfAborted();
|
|
480
|
-
await
|
|
604
|
+
await fsp.rm(installDir, {
|
|
481
605
|
recursive: true,
|
|
482
606
|
force: true
|
|
483
607
|
});
|
|
484
|
-
await
|
|
485
|
-
await validateInstalledServer(command, asset, options.signal);
|
|
608
|
+
await fsp.rename(extractedRoot, installDir);
|
|
486
609
|
return command;
|
|
487
610
|
} finally {
|
|
488
|
-
await Promise.all([
|
|
611
|
+
await Promise.all([fsp.rm(archivePath, { force: true }), fsp.rm(extractDir, {
|
|
489
612
|
recursive: true,
|
|
490
613
|
force: true
|
|
491
614
|
})]);
|
|
492
615
|
}
|
|
493
616
|
}
|
|
494
|
-
function dependencyFiles(dependency) {
|
|
495
|
-
return dependency.archive === "vc-redist" ? dependency.files.map((file) => file.target) : dependency.files;
|
|
496
|
-
}
|
|
497
617
|
async function ensureLlamaServerInstalled(options = {}) {
|
|
498
618
|
options.signal?.throwIfAborted();
|
|
499
619
|
const asset = options.asset ?? selectLlamaServerAsset();
|
|
@@ -503,16 +623,7 @@ async function ensureLlamaServerInstalled(options = {}) {
|
|
|
503
623
|
if (installationPromises.get(key) === pending) installationPromises.delete(key);
|
|
504
624
|
});
|
|
505
625
|
installationPromises.set(key, pending);
|
|
506
|
-
return {
|
|
507
|
-
command: previous && options.signal ? await new Promise((resolve, reject) => {
|
|
508
|
-
const signal = options.signal;
|
|
509
|
-
const onAbort = () => reject(toErrorObject(signal?.reason, "Installation cancelled"));
|
|
510
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
511
|
-
pending.then(resolve, reject).finally(() => signal?.removeEventListener("abort", onAbort));
|
|
512
|
-
if (signal?.aborted) onAbort();
|
|
513
|
-
}) : await pending,
|
|
514
|
-
asset
|
|
515
|
-
};
|
|
626
|
+
return { command: await racePromiseWithAbortSignal(pending, previous ? options.signal : void 0, (signal) => toErrorObject(signal.reason, "Installation cancelled")) };
|
|
516
627
|
}
|
|
517
628
|
//#endregion
|
|
518
629
|
//#region extensions/llama-cpp/src/llama-server-preset.ts
|
|
@@ -649,9 +760,17 @@ function buildLlamaServerPreset(existing, params) {
|
|
|
649
760
|
return header + (header && !/[\r\n]$/u.test(header) ? newline : "") + orderedSections.map((section) => section.replace(/[\r\n]+$/u, "")).join(newline + newline) + newline;
|
|
650
761
|
}
|
|
651
762
|
//#endregion
|
|
652
|
-
//#region extensions/llama-cpp/src/managed-server
|
|
763
|
+
//#region extensions/llama-cpp/src/managed-server.ts
|
|
764
|
+
const modelPromises = /* @__PURE__ */ new Map();
|
|
653
765
|
const log = createSubsystemLogger("llama-cpp");
|
|
654
766
|
const recoveries = /* @__PURE__ */ new Map();
|
|
767
|
+
const resolvedModelArtifacts = /* @__PURE__ */ new Map();
|
|
768
|
+
const presetState = {
|
|
769
|
+
appliedRevisions: /* @__PURE__ */ new Map(),
|
|
770
|
+
desiredRevisions: /* @__PURE__ */ new Map()
|
|
771
|
+
};
|
|
772
|
+
const runPresetTransition = createAsyncLock();
|
|
773
|
+
const LLAMA_CPP_PRESET_RELOAD_TIMEOUT_MS = 15e3;
|
|
655
774
|
function readUniqueArgument(argv, name) {
|
|
656
775
|
const values = argv.flatMap((argument, index) => {
|
|
657
776
|
if (argument === name) {
|
|
@@ -662,55 +781,6 @@ function readUniqueArgument(argv, name) {
|
|
|
662
781
|
});
|
|
663
782
|
return values.length === 1 ? values[0] : void 0;
|
|
664
783
|
}
|
|
665
|
-
/** A new host process recovers once; in-process restarts retain that host's children. */
|
|
666
|
-
async function recoverManagedLlamaServer(params) {
|
|
667
|
-
params.signal?.throwIfAborted();
|
|
668
|
-
if (!findManagedLlamaServerAsset(params.command)) return;
|
|
669
|
-
const args = params.args ?? [];
|
|
670
|
-
const preset = readUniqueArgument(args, "--models-preset");
|
|
671
|
-
const host = readUniqueArgument(args, "--host");
|
|
672
|
-
if (!preset || !host || readUniqueArgument(args, "--port") !== String(params.port)) return;
|
|
673
|
-
const required = /* @__PURE__ */ new Map([
|
|
674
|
-
["--port", String(params.port)],
|
|
675
|
-
["--models-preset", preset],
|
|
676
|
-
["--host", host]
|
|
677
|
-
]);
|
|
678
|
-
const cwd = !path.isAbsolute(preset) ? path.resolve(params.cwd ?? process.cwd()) : void 0;
|
|
679
|
-
const key = JSON.stringify([
|
|
680
|
-
params.command,
|
|
681
|
-
[...required],
|
|
682
|
-
cwd
|
|
683
|
-
]);
|
|
684
|
-
let recovery = recoveries.get(key);
|
|
685
|
-
if (!recovery) {
|
|
686
|
-
recovery = (async () => {
|
|
687
|
-
const { reapOrphanedProcesses } = await import("openclaw/plugin-sdk/process-runtime");
|
|
688
|
-
await reapOrphanedProcesses({
|
|
689
|
-
command: params.command,
|
|
690
|
-
cwd,
|
|
691
|
-
matchesArguments: (argv) => [...required].every(([name, value]) => readUniqueArgument(argv, name) === value),
|
|
692
|
-
signal: params.signal,
|
|
693
|
-
onReap: (pid) => log.info(`reaping orphaned managed llama-server: pid=${pid} port=${params.port}`)
|
|
694
|
-
});
|
|
695
|
-
})().catch((error) => {
|
|
696
|
-
recoveries.delete(key);
|
|
697
|
-
throw error;
|
|
698
|
-
});
|
|
699
|
-
recoveries.set(key, recovery);
|
|
700
|
-
}
|
|
701
|
-
await recovery;
|
|
702
|
-
params.signal?.throwIfAborted();
|
|
703
|
-
}
|
|
704
|
-
//#endregion
|
|
705
|
-
//#region extensions/llama-cpp/src/managed-server.ts
|
|
706
|
-
const modelPromises = /* @__PURE__ */ new Map();
|
|
707
|
-
const resolvedModelArtifacts = /* @__PURE__ */ new Map();
|
|
708
|
-
const presetState = {
|
|
709
|
-
appliedRevisions: /* @__PURE__ */ new Map(),
|
|
710
|
-
desiredRevisions: /* @__PURE__ */ new Map()
|
|
711
|
-
};
|
|
712
|
-
const runPresetTransition = createAsyncLock();
|
|
713
|
-
const LLAMA_CPP_PRESET_RELOAD_TIMEOUT_MS = 15e3;
|
|
714
784
|
function parseHuggingFaceSource(source) {
|
|
715
785
|
const [pathPart, revisionPart] = source.replace(/^(?:hf|huggingface):(?:\/\/)?/iu, "").split("#", 2);
|
|
716
786
|
const [user, repositoryWithTag, ...fileParts] = (pathPart ?? "").split("/");
|
|
@@ -805,7 +875,7 @@ function defaultArtifact(source) {
|
|
|
805
875
|
};
|
|
806
876
|
}
|
|
807
877
|
async function assertGguf(filePath) {
|
|
808
|
-
const handle = await
|
|
878
|
+
const handle = await fsp.open(filePath, "r").catch((error) => {
|
|
809
879
|
if ((error instanceof Error && "code" in error ? error.code : void 0) === "ENOENT") throw new Error(`Model file is missing: ${filePath}. Run interactive llama.cpp setup or correct params.modelPath.`, { cause: error });
|
|
810
880
|
throw error;
|
|
811
881
|
});
|
|
@@ -871,8 +941,8 @@ async function ensureLlamaCppModel(params) {
|
|
|
871
941
|
const artifact = await resolveCachedModelArtifact(localSource, params.cacheDir, params.signal);
|
|
872
942
|
const destination = path.join(params.cacheDir, artifact.fileName);
|
|
873
943
|
const load = modelPromises.get(destination) ?? (async () => {
|
|
874
|
-
const exists = await
|
|
875
|
-
if (exists && artifact.expectedSha256 && (artifact.expectedSize === void 0 || (await
|
|
944
|
+
const exists = await fsp.stat(destination).then((stat) => stat.isFile()).catch(() => false);
|
|
945
|
+
if (exists && artifact.expectedSha256 && (artifact.expectedSize === void 0 || (await fsp.stat(destination)).size === artifact.expectedSize) && await sha256File$1(destination, params.signal) === artifact.expectedSha256) {
|
|
876
946
|
await assertGguf(destination);
|
|
877
947
|
return destination;
|
|
878
948
|
}
|
|
@@ -904,18 +974,18 @@ async function ensureLlamaCppModel(params) {
|
|
|
904
974
|
}
|
|
905
975
|
}
|
|
906
976
|
async function writePreset(presetPath, contents) {
|
|
907
|
-
await
|
|
977
|
+
await fsp.mkdir(path.dirname(presetPath), { recursive: true });
|
|
908
978
|
const temporary = `${presetPath}.tmp-${randomUUID()}`;
|
|
909
979
|
try {
|
|
910
|
-
await
|
|
911
|
-
await
|
|
980
|
+
await fsp.writeFile(temporary, contents, { mode: 384 });
|
|
981
|
+
await fsp.rename(temporary, presetPath);
|
|
912
982
|
} finally {
|
|
913
|
-
await
|
|
983
|
+
await fsp.rm(temporary, { force: true });
|
|
914
984
|
}
|
|
915
985
|
}
|
|
916
986
|
async function updatePreset(presetPath, params) {
|
|
917
987
|
await runPresetTransition(async () => {
|
|
918
|
-
const existing = await
|
|
988
|
+
const existing = await fsp.readFile(presetPath, "utf8").catch((error) => {
|
|
919
989
|
if (asOptionalRecord(error)?.code === "ENOENT") return;
|
|
920
990
|
throw error;
|
|
921
991
|
});
|
|
@@ -967,7 +1037,7 @@ async function prepareManagedLlamaServer(params) {
|
|
|
967
1037
|
let command = params.asset ? void 0 : params.localService?.command;
|
|
968
1038
|
const asset = command === void 0 ? params.asset : findManagedLlamaServerAsset(command);
|
|
969
1039
|
if (command !== void 0 && asset) try {
|
|
970
|
-
await
|
|
1040
|
+
await fsp.stat(command);
|
|
971
1041
|
} catch (error) {
|
|
972
1042
|
if (asOptionalRecord(error)?.code !== "ENOENT") throw error;
|
|
973
1043
|
command = void 0;
|
|
@@ -987,13 +1057,48 @@ async function prepareManagedLlamaServer(params) {
|
|
|
987
1057
|
healthUrl: (!params.isolated ? params.localService?.healthUrl : void 0) ?? `${rootUrl}/health`
|
|
988
1058
|
};
|
|
989
1059
|
const configuredPreset = resolveLlamaCppPresetPath(params.localService);
|
|
990
|
-
if (params.localService && !params.isolated)
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
1060
|
+
if (params.localService && !params.isolated) {
|
|
1061
|
+
const { cwd: configuredCwd, args: configuredArgs } = params.localService;
|
|
1062
|
+
const signal = params.signal;
|
|
1063
|
+
signal?.throwIfAborted();
|
|
1064
|
+
if (findManagedLlamaServerAsset(command)) {
|
|
1065
|
+
const args = configuredArgs ?? [];
|
|
1066
|
+
const preset = readUniqueArgument(args, "--models-preset");
|
|
1067
|
+
const host = readUniqueArgument(args, "--host");
|
|
1068
|
+
if (preset && host && readUniqueArgument(args, "--port") === String(port)) {
|
|
1069
|
+
const required = /* @__PURE__ */ new Map([
|
|
1070
|
+
["--port", String(port)],
|
|
1071
|
+
["--models-preset", preset],
|
|
1072
|
+
["--host", host]
|
|
1073
|
+
]);
|
|
1074
|
+
const cwd = !path.isAbsolute(preset) ? path.resolve(configuredCwd ?? process.cwd()) : void 0;
|
|
1075
|
+
const key = JSON.stringify([
|
|
1076
|
+
command,
|
|
1077
|
+
[...required],
|
|
1078
|
+
cwd
|
|
1079
|
+
]);
|
|
1080
|
+
let recovery = recoveries.get(key);
|
|
1081
|
+
if (!recovery) {
|
|
1082
|
+
recovery = (async () => {
|
|
1083
|
+
const { reapOrphanedProcesses } = await import("openclaw/plugin-sdk/process-runtime");
|
|
1084
|
+
await reapOrphanedProcesses({
|
|
1085
|
+
command,
|
|
1086
|
+
cwd,
|
|
1087
|
+
matchesArguments: (argv) => [...required].every(([name, value]) => readUniqueArgument(argv, name) === value),
|
|
1088
|
+
signal,
|
|
1089
|
+
onReap: (pid) => log.info(`reaping orphaned managed llama-server: pid=${pid} port=${port}`)
|
|
1090
|
+
});
|
|
1091
|
+
})().catch((error) => {
|
|
1092
|
+
recoveries.delete(key);
|
|
1093
|
+
throw error;
|
|
1094
|
+
});
|
|
1095
|
+
recoveries.set(key, recovery);
|
|
1096
|
+
}
|
|
1097
|
+
await recovery;
|
|
1098
|
+
signal?.throwIfAborted();
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
997
1102
|
if (params.localService && !configuredPreset && !params.isolated) {
|
|
998
1103
|
await runPresetTransition(async () => {
|
|
999
1104
|
presetState.desiredRevisions.delete(reconcileOrigin);
|
|
@@ -1006,7 +1111,7 @@ async function prepareManagedLlamaServer(params) {
|
|
|
1006
1111
|
}
|
|
1007
1112
|
const defaultPreset = configuredPreset ? path.resolve(params.localService?.cwd ?? process.cwd(), configuredPreset) : resolveManagedLlamaServerPaths(params.asset).presetPath;
|
|
1008
1113
|
const presetPath = params.isolated ? path.join(path.dirname(defaultPreset), `models-${randomUUID()}.ini`) : defaultPreset;
|
|
1009
|
-
const initialContents = params.isolated && params.mediaModels ? await
|
|
1114
|
+
const initialContents = params.isolated && params.mediaModels ? await fsp.readFile(defaultPreset, "utf8").catch((error) => {
|
|
1010
1115
|
if (!configuredPreset && asOptionalRecord(error)?.code === "ENOENT") return;
|
|
1011
1116
|
throw error;
|
|
1012
1117
|
}) : void 0;
|
|
@@ -1030,7 +1135,7 @@ async function prepareManagedLlamaServer(params) {
|
|
|
1030
1135
|
});
|
|
1031
1136
|
params.signal?.throwIfAborted();
|
|
1032
1137
|
} catch (error) {
|
|
1033
|
-
if (params.isolated) await
|
|
1138
|
+
if (params.isolated) await fsp.rm(presetPath, { force: true });
|
|
1034
1139
|
throw error;
|
|
1035
1140
|
}
|
|
1036
1141
|
return {
|
|
@@ -1073,10 +1178,6 @@ async function ensureManagedLlamaServerForChat(params) {
|
|
|
1073
1178
|
model: params.model,
|
|
1074
1179
|
provider: params.provider
|
|
1075
1180
|
});
|
|
1076
|
-
if (!chatModelPath && resolveLlamaCppModelSource(params.model) === "hf:unsloth/gemma-4-E4B-it-GGUF/gemma-4-E4B-it-Q4_K_M.gguf") {
|
|
1077
|
-
const legacy = path.join(resolveLegacyLlamaCppModelCacheDir(), DEFAULT_LLAMA_CPP_MODEL_CACHE_FILE);
|
|
1078
|
-
if (await fs.stat(legacy).then((stat) => stat.isFile()).catch(() => false)) chatModelPath = legacy;
|
|
1079
|
-
}
|
|
1080
1181
|
chatModelPath = await ensureLlamaCppModel({
|
|
1081
1182
|
source: chatModelPath ?? resolveLlamaCppModelSource(params.model),
|
|
1082
1183
|
cacheDir,
|
|
@@ -1112,4 +1213,4 @@ async function ensureManagedLlamaServerForChat(params) {
|
|
|
1112
1213
|
});
|
|
1113
1214
|
}
|
|
1114
1215
|
//#endregion
|
|
1115
|
-
export { ensureLlamaCppModel, ensureManagedLlamaServerForChat, UnsupportedLlamaServerHostError as n, prepareManagedLlamaServer, ensureLlamaServerInstalled as r, reconcileManagedLlamaServer, resolveLlamaCppModelDownloadSize, resolveLlamaCppPresetPath, assertMediaPresetLimits as t };
|
|
1216
|
+
export { resolveManagedLlamaServerPaths as a, ensureLlamaCppModel, ensureManagedLlamaServerForChat, formatLlamaServerRuntimeError as i, UnsupportedLlamaServerHostError as n, selectLlamaServerAsset as o, prepareManagedLlamaServer, ensureLlamaServerInstalled as r, reconcileManagedLlamaServer, resolveLlamaCppModelDownloadSize, resolveLlamaCppPresetPath, assertMediaPresetLimits as t };
|