kifaru 1.0.194 → 1.0.195
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/kifaru-gateway.js +57 -2
- package/kifaru-local-run-worker.js +49177 -0
- package/kifaru-pi.js +4 -1
- package/package.json +3 -2
package/kifaru-gateway.js
CHANGED
|
@@ -54207,7 +54207,7 @@ var init_lib2 = __esm(() => {
|
|
|
54207
54207
|
});
|
|
54208
54208
|
|
|
54209
54209
|
// src/index.ts
|
|
54210
|
-
import
|
|
54210
|
+
import fs15 from "fs";
|
|
54211
54211
|
|
|
54212
54212
|
// src/config.ts
|
|
54213
54213
|
init_zod();
|
|
@@ -74295,13 +74295,66 @@ function workbenchSocketHandlers(service) {
|
|
|
74295
74295
|
};
|
|
74296
74296
|
}
|
|
74297
74297
|
|
|
74298
|
+
// src/local-run-supervisor.ts
|
|
74299
|
+
import { spawn } from "child_process";
|
|
74300
|
+
import fs14 from "fs";
|
|
74301
|
+
import path14 from "path";
|
|
74302
|
+
function localRunConcurrency(value = process.env.KIFARU_LOCAL_RUN_CONCURRENCY) {
|
|
74303
|
+
const parsed = Number(value ?? "3");
|
|
74304
|
+
return Number.isInteger(parsed) && parsed > 0 ? Math.min(parsed, 8) : 3;
|
|
74305
|
+
}
|
|
74306
|
+
function localRunWorkerEntry(fromDir = import.meta.dir) {
|
|
74307
|
+
const bundled = path14.join(fromDir, "kifaru-local-run-worker.js");
|
|
74308
|
+
if (fs14.existsSync(bundled))
|
|
74309
|
+
return bundled;
|
|
74310
|
+
const source = path14.join(fromDir, "local-run-worker.ts");
|
|
74311
|
+
return fs14.existsSync(source) ? source : null;
|
|
74312
|
+
}
|
|
74313
|
+
function startLocalRunWorkers(options = {}) {
|
|
74314
|
+
const env = options.env ?? process.env;
|
|
74315
|
+
if (env.KIFARU_LOCAL_RUNS !== "1")
|
|
74316
|
+
return () => {};
|
|
74317
|
+
const entry = localRunWorkerEntry(options.fromDir);
|
|
74318
|
+
if (!entry)
|
|
74319
|
+
throw new Error("Local headless worker entry is missing from this Kifaru installation");
|
|
74320
|
+
const children = new Set;
|
|
74321
|
+
let stopping = false;
|
|
74322
|
+
const count = localRunConcurrency(env.KIFARU_LOCAL_RUN_CONCURRENCY);
|
|
74323
|
+
const launch = (slot) => {
|
|
74324
|
+
if (stopping)
|
|
74325
|
+
return;
|
|
74326
|
+
const child = spawn(process.execPath, [entry], { env, stdio: "inherit" });
|
|
74327
|
+
children.add(child);
|
|
74328
|
+
child.on("exit", (code) => {
|
|
74329
|
+
children.delete(child);
|
|
74330
|
+
if (!stopping) {
|
|
74331
|
+
if (code !== 0)
|
|
74332
|
+
console.error(`[local-run] worker ${slot} exited (${code ?? "signal"}); restarting`);
|
|
74333
|
+
setTimeout(() => launch(slot), code === 0 ? 100 : 1000);
|
|
74334
|
+
}
|
|
74335
|
+
});
|
|
74336
|
+
child.on("error", (error92) => console.error(`[local-run] worker ${slot} failed:`, error92));
|
|
74337
|
+
};
|
|
74338
|
+
for (let slot = 1;slot <= count; slot++)
|
|
74339
|
+
launch(slot);
|
|
74340
|
+
console.log(`[local-run] ${count} worker slots ready`);
|
|
74341
|
+
return () => {
|
|
74342
|
+
stopping = true;
|
|
74343
|
+
for (const child of children)
|
|
74344
|
+
child.kill("SIGTERM");
|
|
74345
|
+
};
|
|
74346
|
+
}
|
|
74347
|
+
|
|
74298
74348
|
// src/index.ts
|
|
74299
74349
|
var config3 = loadConfig();
|
|
74350
|
+
if (process.env.KIFARU_LOCAL_RUNS === undefined && ["127.0.0.1", "::1", "localhost"].includes(config3.host)) {
|
|
74351
|
+
process.env.KIFARU_LOCAL_RUNS = "1";
|
|
74352
|
+
}
|
|
74300
74353
|
var catalog = loadModelCatalog(config3);
|
|
74301
74354
|
var storage = createSqliteStorage(config3.sessionDbPath);
|
|
74302
74355
|
var workbench = config3.workbenchCwd ? await createWorkbench() : undefined;
|
|
74303
74356
|
async function createWorkbench() {
|
|
74304
|
-
|
|
74357
|
+
fs15.mkdirSync(config3.workbenchCwd, { recursive: true });
|
|
74305
74358
|
try {
|
|
74306
74359
|
const service = await WorkbenchAgentService.create(config3, catalog, createPlatformRepository(storage.db));
|
|
74307
74360
|
console.log(`[session-gateway] live workspace enabled at ${config3.workbenchCwd}`);
|
|
@@ -74354,6 +74407,7 @@ var server = Bun.serve({
|
|
|
74354
74407
|
return new Response("Internal server error", { status: 500 });
|
|
74355
74408
|
}
|
|
74356
74409
|
});
|
|
74410
|
+
var stopLocalRunWorkers = startLocalRunWorkers();
|
|
74357
74411
|
console.log(`[session-gateway] listening on http://${server.hostname}:${server.port} with ${Object.keys(catalog.models).length} model(s)`);
|
|
74358
74412
|
if (config3.hasOAuthProviders) {
|
|
74359
74413
|
console.log(`[session-gateway] OAuth providers enabled: ${config3.oauthProviderIds.join(", ")}`);
|
|
@@ -74365,6 +74419,7 @@ if (config3.hasBootstrapToken) {
|
|
|
74365
74419
|
}
|
|
74366
74420
|
for (const signal of ["SIGINT", "SIGTERM"]) {
|
|
74367
74421
|
process.on(signal, () => {
|
|
74422
|
+
stopLocalRunWorkers();
|
|
74368
74423
|
storage.close();
|
|
74369
74424
|
server.stop(true);
|
|
74370
74425
|
process.exit(0);
|