oclaw/runtime/hooks/ts_hook_runner.ts
oliver dbbe3add6a 重构主控编排与运行时预热链路,统一工作区提示词/专家调度协议并补齐 wiki 记忆注入与写回闭环。
同时收敛启动与运维脚本默认行为(含 wiki worker)、更新 Admin 可观测性与相关测试,降低首轮时延并提高运行稳定性。

Made-with: Cursor
2026-04-26 08:34:33 +08:00

45 lines
1.7 KiB
TypeScript

/**
* Invoked as: npx --yes tsx ts_hook_runner.ts <absolute-handler.ts> [exportName]
* stdin: JSON { type, action, sessionKey, context, messages?, timestamp? }
* stdout: JSON { "context"?: object } merged into the hook event in Python
*/
import { readFileSync } from "node:fs";
import { pathToFileURL } from "node:url";
const handlerPath = process.argv[2];
const exportName = (process.argv[3] || "default").trim();
if (!handlerPath) {
console.error("oclaw: ts_hook_runner: missing handler path");
process.exit(2);
}
const raw = readFileSync(0, "utf-8");
const data = JSON.parse(raw) as Record<string, unknown>;
const event = {
type: data.type,
action: data.action,
sessionKey: data.sessionKey,
context: (typeof data.context === "object" && data.context) ? (data.context as Record<string, unknown>) : {},
messages: Array.isArray(data.messages) ? data.messages : [],
timestamp: data.timestamp,
};
const mod: Record<string, unknown> = (await import(pathToFileURL(handlerPath).href)) as Record<string, unknown>;
let fn: ((ev: unknown) => unknown) | undefined;
if (exportName && exportName !== "default" && mod[exportName] && typeof mod[exportName] === "function") {
fn = mod[exportName] as (ev: unknown) => unknown;
} else {
fn = (mod.default ?? mod.handle ?? mod.handler) as (ev: unknown) => unknown;
}
if (typeof fn !== "function") {
console.error("oclaw: ts_hook_runner: no function export in", handlerPath, "export", exportName);
process.exit(3);
}
const r = fn(event);
if (r && typeof (r as { then?: unknown }).then === "function") {
await (r as Promise<unknown>);
}
process.stdout.write(
JSON.stringify({ context: event.context }, (_k, v) => (typeof v === "bigint" ? v.toString() : v), 0),
);