mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-12-01 00:49:33 +01:00
fix(omp): report tool approvals to Codeman so an omp pane alerts
An omp pane asking for tool approval raised no tab alert, no desktop notification and no Approvals Inbox item. omp inherited hooks:'none' from agentDefaults(), so hooksAvailableForMode() answered false and hook-event-routes never opened an item; there was nothing to resolve either. omp is one of the few non-claude CLIs with real approval events (tool_approval_requested / tool_approval_resolved), so Codeman can install a hook module into <workspace>/.claude/hooks/pre/ - the directory omp scans - on session create. It forwards them as permission_prompt and elicitation_complete, the names the server already routes on, so the existing alert, notification and inbox pipeline works unchanged. The file name comes from the new capabilities.approvalHookFile field rather than a branch on the CLI id (cli-registry-no-id-branching). stop/idle_prompt are deliberately NOT reported: omp's idle stays screen-read via workDetect, which the agent skill's verbs.md now says explicitly.
This commit is contained in:
1 parent
f62c1f7e2f
commit
0bbf483a35
9 files changed
+325
-4
No files matched your search
@@ -1669,6 +1669,127 @@ export async function removeAgentSkillFrom(skillDir: string): Promise<AgentSkill
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// omp approval hooks
|
||||
// ============================================================================
|
||||
|
||||
/** Marks the hook file this module owns inside a workspace. */
|
||||
const OMP_APPROVAL_HOOK_MARKER = 'CODEMAN_OMP_APPROVAL_HOOK_V1';
|
||||
|
||||
/**
|
||||
* omp's own hook directory, workspace-relative. omp scans `<cwd>/.claude/hooks/pre`
|
||||
* and `<cwd>/.claude/hooks/post` (`projectDir` for the `claude` persona) plus a
|
||||
* user-level `<agentDir>/hooks`, and loads each `.ts`/`.js` file's default export as
|
||||
* `(pi: HookAPI) => void`. A project-level `pre` hook is deliberately the target: it
|
||||
* travels with the workspace the session actually runs in, so no per-machine or
|
||||
* per-user install is needed and removing the workspace removes the hook.
|
||||
*/
|
||||
export const OMP_APPROVAL_HOOK_DIR = '.claude/hooks/pre';
|
||||
export const OMP_APPROVAL_HOOK_FILE = 'codeman-approval.ts';
|
||||
|
||||
/**
|
||||
* Source of the hook that turns omp's own approval events into Codeman hook events.
|
||||
*
|
||||
* WHY this exists: omp declares `capabilities.hooks: 'always'` (stock registry) but
|
||||
* installs no Claude-style `settings.local.json` hook block — its hooks are modules.
|
||||
* Without this file `hooksAvailableForMode()` is true while nothing ever POSTs, so
|
||||
* `/api/approvals` stays empty, the tab never goes red and no notification fires: an
|
||||
* omp pane asking for tool approval is silent. Claude solves the same problem by
|
||||
* writing a `Notification: permission_prompt` block into `settings.local.json`.
|
||||
*
|
||||
* The mapping mirrors Claude's names exactly, because the server routes on them
|
||||
* (`APPROVAL_KIND_BY_EVENT` in web/routes/hook-event-routes.ts):
|
||||
* - `tool_approval_requested` -> `permission_prompt` (opens the inbox item)
|
||||
* - `tool_approval_resolved` -> `elicitation_complete` (resolves it, whether the user
|
||||
* approved or denied, so a denied dialog does not leave a red tab forever)
|
||||
*
|
||||
* ⚠️ Fails OPEN and SILENT, like the memory-gate hook: a hook that throws would break
|
||||
* the approval flow it exists to report. Every path catches, and nothing is awaited
|
||||
* unbounded — the fetch is fire-and-forget with a short AbortSignal.
|
||||
*/
|
||||
export function generateOmpApprovalHook(): string {
|
||||
return `// ${OMP_APPROVAL_HOOK_MARKER} — auto-generated by Codeman; safe to delete, rewritten when a session is created.
|
||||
//
|
||||
// Reports omp's tool-approval dialogs to Codeman's /api/hook-event so an omp pane
|
||||
// raises a tab alert and a desktop notification exactly like a claude pane does.
|
||||
// Codeman owns this file: a foreign file of the same name is never overwritten.
|
||||
import { readFile } from "node:fs/promises";
|
||||
import type { HookAPI } from "@oh-my-pi/pi-coding-agent";
|
||||
|
||||
const API_URL = process.env.CODEMAN_API_URL;
|
||||
const SESSION_ID = process.env.CODEMAN_SESSION_ID;
|
||||
const SECRET_FILE = process.env.CODEMAN_HOOK_SECRET_FILE;
|
||||
|
||||
/** POST one Codeman hook event. Never throws, never awaited past its timeout. */
|
||||
async function report(event: string, data: Record<string, unknown>): Promise<void> {
|
||||
if (!API_URL || !SESSION_ID) return;
|
||||
try {
|
||||
const secret = SECRET_FILE ? (await readFile(SECRET_FILE, "utf8")).trim() : "";
|
||||
await fetch(\`\${API_URL}/api/hook-event\`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", "X-Codeman-Hook-Secret": secret },
|
||||
body: JSON.stringify({ event, sessionId: SESSION_ID, data }),
|
||||
signal: AbortSignal.timeout(5000),
|
||||
});
|
||||
} catch {
|
||||
// Best effort by contract: a failed report must never disturb the dialog itself.
|
||||
}
|
||||
}
|
||||
|
||||
export default function codemanApprovalHook(pi: HookAPI): void {
|
||||
pi.on("tool_approval_requested", (event) => {
|
||||
void report("permission_prompt", {
|
||||
tool_name: event.toolName,
|
||||
tool_input: event.reason ? { description: event.reason } : undefined,
|
||||
});
|
||||
});
|
||||
|
||||
pi.on("tool_approval_resolved", (event) => {
|
||||
void report("elicitation_complete", {
|
||||
tool_name: event.toolName,
|
||||
approved: event.approved,
|
||||
});
|
||||
});
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
export type ApprovalHookApplyResult = 'installed' | 'refreshed' | 'unchanged' | 'foreign' | 'symlink';
|
||||
|
||||
/**
|
||||
* Install or refresh the omp approval hook in `<workspace>/.claude/hooks/pre/`.
|
||||
*
|
||||
* Ownership: the file carries `OMP_APPROVAL_HOOK_MARKER` in its first line. A file
|
||||
* WITHOUT the marker is somebody else's hook and is left untouched (`foreign`) — the
|
||||
* same posture `installAgentSkillInto` takes, so Codeman never clobbers user work.
|
||||
* A symlinked directory is refused rather than written through.
|
||||
*
|
||||
* Idempotent: an unchanged file is not rewritten (`unchanged`), so this is safe to call
|
||||
* on every omp session create.
|
||||
*/
|
||||
export async function applyApprovalHookFile(workspace: string, fileName: string): Promise<ApprovalHookApplyResult> {
|
||||
const hookDir = join(workspace, OMP_APPROVAL_HOOK_DIR);
|
||||
const target = join(hookDir, fileName);
|
||||
return withSettingsLock(target, async () => {
|
||||
if ((await isSymlink(hookDir)) || (await isSymlink(join(workspace, '.claude')))) return 'symlink';
|
||||
|
||||
let existing: string | null = null;
|
||||
try {
|
||||
existing = await readFile(target, 'utf-8');
|
||||
} catch {
|
||||
// absent: fresh install
|
||||
}
|
||||
if (existing !== null && !existing.includes(OMP_APPROVAL_HOOK_MARKER)) return 'foreign';
|
||||
|
||||
const content = generateOmpApprovalHook();
|
||||
if (existing === content) return 'unchanged';
|
||||
|
||||
await mkdir(hookDir, { recursive: true });
|
||||
await writeSkillFileAtomic(target, content);
|
||||
return existing === null ? 'installed' : 'refreshed';
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Claude Code's project skills directory, and the meaning `agentSkillInjection: true`
|
||||
* had before `capabilities.agentSkillDir` existed.
|
||||
|
||||
Reference in new issue
Block a user