@koed-labs/server 0.0.0-stage → 0.11.1

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.
Files changed (67) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +64 -2
  3. package/bin/koed.js +7 -0
  4. package/package.json +26 -4
  5. package/third-party-notices.json +92 -0
  6. package/vendor/chunk-2XKSVKQA.js +49 -0
  7. package/vendor/chunk-4OO6VAU3.js +259 -0
  8. package/vendor/chunk-53ZFCDHP.js +332 -0
  9. package/vendor/chunk-6JRWGOQG.js +236 -0
  10. package/vendor/chunk-6KS67EQE.js +75 -0
  11. package/vendor/chunk-7EGSY3E4.js +8 -0
  12. package/vendor/chunk-7FSHVVTZ.js +128 -0
  13. package/vendor/chunk-7NOQSX4T.js +14 -0
  14. package/vendor/chunk-7NXKPDF3.js +1586 -0
  15. package/vendor/chunk-ALJUSOF7.js +175 -0
  16. package/vendor/chunk-AVQCUFYQ.js +16 -0
  17. package/vendor/chunk-BGENSONI.js +18 -0
  18. package/vendor/chunk-BTS4ZTF3.js +43 -0
  19. package/vendor/chunk-BUIM2M4R.js +22 -0
  20. package/vendor/chunk-C4V72GXC.js +194 -0
  21. package/vendor/chunk-C5SNDPAP.js +2472 -0
  22. package/vendor/chunk-CHMH4LH5.js +2497 -0
  23. package/vendor/chunk-EA4QDDQA.js +58 -0
  24. package/vendor/chunk-EDPNYIX5.js +5448 -0
  25. package/vendor/chunk-FN3HUUO2.js +1974 -0
  26. package/vendor/chunk-FSNLU46W.js +14548 -0
  27. package/vendor/chunk-FYYRFX4U.js +4423 -0
  28. package/vendor/chunk-GFASBPV2.js +42 -0
  29. package/vendor/chunk-HC76426R.js +48 -0
  30. package/vendor/chunk-IY2HRXOZ.js +99 -0
  31. package/vendor/chunk-JBGD4JLR.js +3779 -0
  32. package/vendor/chunk-JYK6UL6D.js +261 -0
  33. package/vendor/chunk-LRP66XEY.js +13907 -0
  34. package/vendor/chunk-NOU7RHYZ.js +26 -0
  35. package/vendor/chunk-NVTMIAAF.js +113 -0
  36. package/vendor/chunk-O7DW2VAV.js +14 -0
  37. package/vendor/chunk-OCILJ737.js +299 -0
  38. package/vendor/chunk-PL3LWC5C.js +754 -0
  39. package/vendor/chunk-QRKHHWJJ.js +104 -0
  40. package/vendor/chunk-QT24SAW6.js +110 -0
  41. package/vendor/chunk-RCR4XPE3.js +26020 -0
  42. package/vendor/chunk-SHLRINKO.js +73 -0
  43. package/vendor/chunk-SNKWETM5.js +35 -0
  44. package/vendor/chunk-SRKIBFOE.js +242 -0
  45. package/vendor/chunk-T6UB5PNX.js +124 -0
  46. package/vendor/chunk-TG26CZGQ.js +23 -0
  47. package/vendor/chunk-TISYUUEJ.js +188 -0
  48. package/vendor/chunk-TSAYTRX3.js +89 -0
  49. package/vendor/chunk-TVYEB2GN.js +343 -0
  50. package/vendor/chunk-UB6JCVQ3.js +461 -0
  51. package/vendor/chunk-V67IQB7H.js +153 -0
  52. package/vendor/chunk-VGIYSDNU.js +522 -0
  53. package/vendor/chunk-W2HNI6AA.js +75 -0
  54. package/vendor/chunk-W2OJZZ6X.js +20 -0
  55. package/vendor/chunk-WDFERJG3.js +2208 -0
  56. package/vendor/chunk-WPIEF4LY.js +1647 -0
  57. package/vendor/chunk-XIZVLPHZ.js +18 -0
  58. package/vendor/chunk-YZLQZU5E.js +46 -0
  59. package/vendor/chunk-Z5WCAUA6.js +148 -0
  60. package/vendor/chunk-ZIOKV2X6.js +1589 -0
  61. package/vendor/chunk-ZOHNXUDY.js +12501 -0
  62. package/vendor/chunk-ZUDSZGVO.js +11 -0
  63. package/vendor/cli.js +7 -0
  64. package/vendor/component-manifest-catalog.js +9 -0
  65. package/vendor/control-plane.js +28 -0
  66. package/vendor/package.json +1 -0
  67. package/vendor/privacy-service-bootstrap.js +6 -0
@@ -0,0 +1,261 @@
1
+ import { createRequire as createKoedRequire } from "node:module"; const require = createKoedRequire(import.meta.url);
2
+
3
+ // packages/koed-server/dist/local-models-runtime.js
4
+ import { createHash } from "node:crypto";
5
+ import { constants, lstatSync, createWriteStream, mkdirSync, renameSync, rmSync, statSync } from "node:fs";
6
+ import { open } from "node:fs/promises";
7
+ import { Readable, Transform } from "node:stream";
8
+ import { pipeline } from "node:stream/promises";
9
+ import { basename, dirname, resolve } from "node:path";
10
+ var MODEL_DEFINITIONS = {
11
+ embedding: {
12
+ key: "qwen3-0.6b",
13
+ filename: "Qwen3-Embedding-0.6B-Q8_0.gguf",
14
+ urlEnv: "KOED_EMBEDDING_MODEL_URL",
15
+ sha256Env: "KOED_EMBEDDING_MODEL_SHA256",
16
+ pathEnv: "KOED_EMBEDDING_MODEL_PATH",
17
+ defaultUrl: "https://huggingface.co/Qwen/Qwen3-Embedding-0.6B-GGUF/resolve/main/Qwen3-Embedding-0.6B-Q8_0.gguf",
18
+ defaultSha256: "06507c7b42688469c4e7298b0a1e16deff06caf291cf0a5b278c308249c3e439"
19
+ }
20
+ };
21
+ var localModelKinds = ["embedding"];
22
+ var trimEnv = (environment, name) => {
23
+ const value = environment[name]?.trim();
24
+ return value ? value : void 0;
25
+ };
26
+ var dependencyMode = (environment) => {
27
+ const value = trimEnv(environment, "KOED_DEPENDENCY_MODE");
28
+ if (value === "bundled-local" || value === "external") {
29
+ return value;
30
+ }
31
+ return void 0;
32
+ };
33
+ var modelDownloadBlockedAction = (manifest) => `External dependency mode does not download model assets. Switch to bundled-local mode or set ${manifest.pathEnv} to an existing model file.`;
34
+ var normalizeSha256 = (value) => {
35
+ const normalized = value?.trim().toLowerCase();
36
+ if (!normalized) {
37
+ return void 0;
38
+ }
39
+ if (!/^[a-f0-9]{64}$/.test(normalized)) {
40
+ throw new Error("Model SHA-256 must be 64 lowercase or uppercase hex characters.");
41
+ }
42
+ return normalized;
43
+ };
44
+ var resolveLocalModelManifest = (paths, kind = "embedding", environment = process.env) => {
45
+ const definition = MODEL_DEFINITIONS[kind];
46
+ const explicitPath = trimEnv(environment, definition.pathEnv);
47
+ return {
48
+ kind,
49
+ key: definition.key,
50
+ filename: definition.filename,
51
+ modelPath: explicitPath ? resolve(explicitPath) : resolve(paths.modelsDir, definition.filename),
52
+ urlEnv: definition.urlEnv,
53
+ sha256Env: definition.sha256Env,
54
+ pathEnv: definition.pathEnv,
55
+ url: trimEnv(environment, definition.urlEnv) ?? definition.defaultUrl,
56
+ sha256: normalizeSha256(trimEnv(environment, definition.sha256Env) ?? definition.defaultSha256),
57
+ defaultUrl: definition.defaultUrl,
58
+ defaultSha256: definition.defaultSha256
59
+ };
60
+ };
61
+ var sha256File = async (path, signal) => {
62
+ const hash = createHash("sha256");
63
+ const file = await open(path, constants.O_RDONLY | constants.O_NONBLOCK);
64
+ try {
65
+ if (!(await file.stat()).isFile())
66
+ throw new Error("The model must be a regular file.");
67
+ await pipeline(file.createReadStream(), hash, { signal });
68
+ } finally {
69
+ await file.close();
70
+ }
71
+ return hash.digest("hex");
72
+ };
73
+ var collectLocalModelStatus = async (paths, kind = "embedding", environment = process.env, signal) => {
74
+ const manifest = resolveLocalModelManifest(paths, kind, environment);
75
+ const mode = dependencyMode(environment);
76
+ const file = statSync(manifest.modelPath, { throwIfNoEntry: false });
77
+ if (!file) {
78
+ const dangling = lstatSync(manifest.modelPath, {
79
+ throwIfNoEntry: false
80
+ })?.isSymbolicLink();
81
+ return {
82
+ state: "missing",
83
+ message: dangling ? `${manifest.key} model link at ${manifest.modelPath} points to a missing file.` : `${manifest.key} model is not installed at ${manifest.modelPath}.`,
84
+ action: mode === "external" ? modelDownloadBlockedAction(manifest) : `Run koed models install --kind ${kind}. Override ${manifest.urlEnv} and ${manifest.sha256Env} only when using a custom model artifact.`,
85
+ modelPath: manifest.modelPath,
86
+ manifest
87
+ };
88
+ }
89
+ if (!file.isFile())
90
+ throw new Error("The installed model must be a regular file.");
91
+ const sizeBytes = file.size;
92
+ if (!manifest.sha256) {
93
+ return {
94
+ state: "not_configured",
95
+ message: `${manifest.key} model exists, but ${manifest.sha256Env} is not configured for verification.`,
96
+ action: `Set ${manifest.sha256Env} to verify installed model checksum.`,
97
+ modelPath: manifest.modelPath,
98
+ sizeBytes,
99
+ manifest
100
+ };
101
+ }
102
+ const actual = await sha256File(manifest.modelPath, signal);
103
+ if (actual !== manifest.sha256) {
104
+ return {
105
+ state: "checksum_mismatch",
106
+ message: `${manifest.key} model checksum mismatch.`,
107
+ action: `Remove ${manifest.modelPath}, verify ${manifest.sha256Env}, then run koed models install --kind ${kind}.`,
108
+ modelPath: manifest.modelPath,
109
+ sizeBytes,
110
+ sha256: actual,
111
+ manifest
112
+ };
113
+ }
114
+ return {
115
+ state: "installed",
116
+ message: `${manifest.key} model is installed and checksum verified.`,
117
+ modelPath: manifest.modelPath,
118
+ sizeBytes,
119
+ sha256: actual,
120
+ manifest
121
+ };
122
+ };
123
+ var installLocalModel = async (paths, kind = "embedding", environment = process.env, { fetch: fetcher = globalThis.fetch.bind(globalThis), onProgress } = {}) => {
124
+ const manifest = resolveLocalModelManifest(paths, kind, environment);
125
+ const mode = dependencyMode(environment);
126
+ if (mode === "external") {
127
+ return {
128
+ ok: false,
129
+ state: "not_configured",
130
+ message: `${manifest.key} model downloads are disabled in external dependency mode.`,
131
+ action: modelDownloadBlockedAction(manifest),
132
+ modelPath: manifest.modelPath,
133
+ manifest
134
+ };
135
+ }
136
+ if (!manifest.url) {
137
+ return {
138
+ ok: false,
139
+ state: "not_configured",
140
+ message: `${manifest.urlEnv} is required to install ${manifest.key}.`,
141
+ action: `Set ${manifest.urlEnv} to HTTPS model artifact URL.`,
142
+ modelPath: manifest.modelPath,
143
+ manifest
144
+ };
145
+ }
146
+ if (!manifest.sha256) {
147
+ return {
148
+ ok: false,
149
+ state: "not_configured",
150
+ message: `${manifest.sha256Env} is required before installing ${manifest.key}.`,
151
+ action: `Set ${manifest.sha256Env} to expected SHA-256 checksum.`,
152
+ modelPath: manifest.modelPath,
153
+ manifest
154
+ };
155
+ }
156
+ try {
157
+ if (new URL(manifest.url).protocol !== "https:") {
158
+ return {
159
+ ok: false,
160
+ state: "not_configured",
161
+ message: `${manifest.urlEnv} must be an HTTPS URL.`,
162
+ action: `Set ${manifest.urlEnv} to a trusted HTTPS model artifact URL.`,
163
+ modelPath: manifest.modelPath,
164
+ manifest
165
+ };
166
+ }
167
+ } catch {
168
+ return {
169
+ ok: false,
170
+ state: "not_configured",
171
+ message: `${manifest.urlEnv} must be a valid HTTPS URL.`,
172
+ action: `Set ${manifest.urlEnv} to a trusted HTTPS model artifact URL.`,
173
+ modelPath: manifest.modelPath,
174
+ manifest
175
+ };
176
+ }
177
+ mkdirSync(dirname(manifest.modelPath), { recursive: true, mode: 448 });
178
+ const tempPath = resolve(paths.cacheDir, `${basename(manifest.modelPath)}.${process.pid}.download`);
179
+ mkdirSync(paths.cacheDir, { recursive: true, mode: 448 });
180
+ rmSync(tempPath, { force: true });
181
+ const response = await fetcher(manifest.url);
182
+ if (!response.ok || !response.body) {
183
+ return {
184
+ ok: false,
185
+ state: "missing",
186
+ message: `Could not download ${manifest.key}: HTTP ${response.status}.`,
187
+ action: `Check ${manifest.urlEnv}, network access, and artifact permissions.`,
188
+ modelPath: manifest.modelPath,
189
+ manifest
190
+ };
191
+ }
192
+ const contentLength = Number.parseInt(response.headers.get("content-length") ?? "", 10);
193
+ const totalBytes = Number.isSafeInteger(contentLength) && contentLength >= 0 ? contentLength : null;
194
+ let completedBytes = 0;
195
+ let lastReportedBytes = 0;
196
+ let lastReportedAt = 0;
197
+ onProgress?.({
198
+ completedBytes: 0,
199
+ phase: "downloading",
200
+ totalBytes
201
+ });
202
+ const progress = new Transform({
203
+ transform(chunk, _encoding, callback) {
204
+ completedBytes += chunk.byteLength;
205
+ const now = Date.now();
206
+ if (completedBytes === totalBytes || completedBytes - lastReportedBytes >= 1048576 || now - lastReportedAt >= 250) {
207
+ lastReportedBytes = completedBytes;
208
+ lastReportedAt = now;
209
+ onProgress?.({
210
+ completedBytes,
211
+ phase: "downloading",
212
+ totalBytes
213
+ });
214
+ }
215
+ callback(null, chunk);
216
+ }
217
+ });
218
+ await pipeline(Readable.fromWeb(response.body), progress, createWriteStream(tempPath, { mode: 384 }));
219
+ onProgress?.({
220
+ completedBytes,
221
+ phase: "verifying",
222
+ totalBytes
223
+ });
224
+ const actual = await sha256File(tempPath);
225
+ if (actual !== manifest.sha256) {
226
+ rmSync(tempPath, { force: true });
227
+ return {
228
+ ok: false,
229
+ state: "checksum_mismatch",
230
+ message: `${manifest.key} downloaded checksum mismatch.`,
231
+ action: `Verify ${manifest.sha256Env} and ${manifest.urlEnv}.`,
232
+ modelPath: manifest.modelPath,
233
+ sha256: actual,
234
+ manifest
235
+ };
236
+ }
237
+ renameSync(tempPath, manifest.modelPath);
238
+ const sizeBytes = statSync(manifest.modelPath).size;
239
+ const status = {
240
+ state: "installed",
241
+ message: `${manifest.key} model is installed and checksum verified.`,
242
+ modelPath: manifest.modelPath,
243
+ sizeBytes,
244
+ sha256: actual,
245
+ manifest
246
+ };
247
+ onProgress?.({
248
+ completedBytes: sizeBytes,
249
+ phase: "complete",
250
+ totalBytes: sizeBytes
251
+ });
252
+ return { ok: true, ...status };
253
+ };
254
+
255
+ export {
256
+ localModelKinds,
257
+ resolveLocalModelManifest,
258
+ sha256File,
259
+ collectLocalModelStatus,
260
+ installLocalModel
261
+ };