/**
 * Hooks token management via platform IPC.
 * Allows agent containers to enable/disable and rotate their hooks token.
 */
import path from "path";
import { getContext } from "../client.js";
import { writeIpcFile, clearStaleResult, pollJsonFile } from "../ipc.js";
function groupFolder() {
    return process.env.NANOCLAW_GROUP_FOLDER ?? "";
}
function tasksDir(ipcDir) {
    return path.join(ipcDir, "tasks");
}
function resultFile(ipcDir) {
    return path.join(ipcDir, "hooks-result.json");
}
async function sendAndWait(ipcDir, payload) {
    const result = resultFile(ipcDir);
    clearStaleResult(result);
    writeIpcFile(tasksDir(ipcDir), { ...payload, groupFolder: groupFolder() });
    return pollJsonFile(result, (d) => d.ok !== undefined, 15000);
}
/**
 * Get the current hooks config for this group.
 */
export async function getHooksConfig(ipcDir) {
    const dir = ipcDir ?? getContext().ipcDir;
    return sendAndWait(dir, { type: "hooks_get" });
}
/**
 * Enable or disable hooks for this group.
 * Enabling auto-generates a token if none exists.
 */
export async function setHooksEnabled(enabled, ipcDir) {
    const dir = ipcDir ?? getContext().ipcDir;
    return sendAndWait(dir, { type: "hooks_set_enabled", enabled });
}
/**
 * Rotate the hooks bearer token, generating a new one.
 * Returns the new token.
 */
export async function rotateHooksToken(ipcDir) {
    const dir = ipcDir ?? getContext().ipcDir;
    return sendAndWait(dir, { type: "hooks_rotate" });
}
function hookResultFile(ipcDir, requestId) {
    return path.join(ipcDir, `hooks-result-${requestId}.json`);
}
function genRequestId() {
    return `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
}
/**
 * Trigger an async agent run via IPC.
 * Returns immediately after the task is queued.
 */
export async function triggerAgent(prompt, ipcDir) {
    const dir = ipcDir ?? getContext().ipcDir;
    const requestId = genRequestId();
    const resultFile = hookResultFile(dir, requestId);
    clearStaleResult(resultFile);
    writeIpcFile(path.join(dir, 'tasks'), { type: 'hooks_agent', prompt, requestId });
    return pollJsonFile(resultFile, (d) => typeof d.ok === 'boolean', 15_000);
}
/**
 * Trigger a wake run via IPC (reads HEARTBEAT.md from the group workspace).
 * Returns immediately after the task is queued.
 */
export async function wake(ipcDir) {
    const dir = ipcDir ?? getContext().ipcDir;
    const requestId = genRequestId();
    const resultFile = hookResultFile(dir, requestId);
    clearStaleResult(resultFile);
    writeIpcFile(path.join(dir, 'tasks'), { type: 'hooks_wake', requestId });
    return pollJsonFile(resultFile, (d) => typeof d.ok === 'boolean', 15_000);
}
/**
 * Invoke the agent synchronously via IPC and return the result.
 * Accepts either a free-form prompt or a tool name + args.
 * Waits up to 90 seconds for the agent to respond.
 */
export async function invoke(input, ipcDir) {
    const dir = ipcDir ?? getContext().ipcDir;
    const requestId = genRequestId();
    const resultFile = hookResultFile(dir, requestId);
    clearStaleResult(resultFile);
    const payload = typeof input === 'string'
        ? { type: 'hooks_invoke', prompt: input, requestId }
        : { type: 'hooks_invoke', tool: input.tool, args: input.args ?? {}, requestId };
    writeIpcFile(path.join(dir, 'tasks'), payload);
    return pollJsonFile(resultFile, (d) => typeof d.ok === 'boolean', 90_000);
}
//# sourceMappingURL=hooks.js.map