@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
83 lines • 3.99 kB
JavaScript
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
if (typeof path === "string" && /^\.\.?\//.test(path)) {
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
});
}
return path;
};
// The `module` job's worker (senpi#3566 todo 4): the launch barrier is installed before the module is loaded, so every
// launch the module makes - at import time included - passes it. On Bun the module graph goes through the extension
// importer with only the child_process redirect (never loader.ts, which pulls the session writers in).
import { parentPort, workerData } from "node:worker_threads";
import "../../../windows-power-throttling-startup.js";
import { createBunExtensionImporter } from "../../../core/extensions/bun-extension-importer.js";
import { CHILD_PROCESS_REDIRECTS, childProcessVirtualModule, GATED_CHILD_PROCESS_ID, GATED_WORKER_THREADS_ID, WORKER_THREADS_REDIRECTS, workerThreadsVirtualModule, } from "./gated-modules.js";
import { installWorkerBarrier } from "./gated-worker.js";
import { createModuleHost } from "./module-host.js";
const data = workerData;
installWorkerBarrier(data);
async function loadNamespace(path) {
if (Reflect.get(globalThis, "Bun") === undefined)
return import(__rewriteRelativeImportExtension(path));
const importer = createBunExtensionImporter({
[GATED_CHILD_PROCESS_ID]: childProcessVirtualModule(),
[GATED_WORKER_THREADS_ID]: workerThreadsVirtualModule(),
}, { ...CHILD_PROCESS_REDIRECTS, ...WORKER_THREADS_REDIRECTS });
return importer.importNamespace(path);
}
/** Aborted when the job is cancelled; the module sees it as `host.signal`. */
const cancellation = new AbortController();
parentPort?.on("message", (message) => {
if (typeof message === "object" && message !== null && Reflect.get(message, "kind") === "cancel")
cancellation.abort();
});
function createWorkerHost() {
const port = parentPort;
if (port === null)
throw new Error("module job worker without a parent port");
const pending = new Map();
let next = 0;
port.on("message", (message) => {
if (typeof message !== "object" || message === null || Reflect.get(message, "kind") !== "host_reply")
return;
const callId = String(Reflect.get(message, "callId"));
const call = pending.get(callId);
if (call === undefined)
return;
pending.delete(callId);
const error = Reflect.get(message, "error");
if (typeof error === "string")
call.reject(Object.assign(new Error(error), { code: Reflect.get(message, "code") }));
else
call.resolve(Reflect.get(message, "value"));
});
const send = (hostKind, name, args) => new Promise((resolve, reject) => {
const callId = `m${++next}`;
pending.set(callId, { resolve, reject });
port.postMessage({ kind: "host_call", callId, hostKind, name, args });
});
return createModuleHost(cancellation.signal, send);
}
try {
const namespace = await loadNamespace(data.modulePath);
const target = namespace[data.exportName];
if (typeof target !== "function") {
parentPort?.postMessage({
kind: "module_error",
code: "offload_module_unavailable",
message: `${data.modulePath} has no function export "${data.exportName}"`,
});
}
else {
parentPort?.postMessage({ kind: "module_result", value: await target(data.args, createWorkerHost()) });
}
}
catch (error) {
parentPort?.postMessage({
kind: "module_error",
code: "module_job_failed",
message: error instanceof Error ? error.message : String(error),
});
}
//# sourceMappingURL=module-job-worker.js.map