refactor(channels): dedupe hook and monitor execution paths

This commit is contained in:
Peter Steinberger
2026-02-22 21:18:53 +00:00
parent 06b0a60bef
commit 2081b3a3c4
19 changed files with 347 additions and 213 deletions

View File

@@ -233,6 +233,11 @@ export type HookAgentPayload = {
timeoutSeconds?: number;
};
export type HookAgentDispatchPayload = Omit<HookAgentPayload, "sessionKey"> & {
sessionKey: string;
allowUnsafeExternalContent?: boolean;
};
const listHookChannelValues = () => ["last", ...listChannelPlugins().map((plugin) => plugin.id)];
export type HookMessageChannel = ChannelId | "last";

View File

@@ -42,7 +42,7 @@ import {
extractHookToken,
getHookAgentPolicyError,
getHookChannelError,
type HookMessageChannel,
type HookAgentDispatchPayload,
type HooksConfigResolved,
isHookAgentAllowed,
normalizeAgentPayload,
@@ -69,20 +69,7 @@ const HOOK_AUTH_FAILURE_WINDOW_MS = 60_000;
type HookDispatchers = {
dispatchWakeHook: (value: { text: string; mode: "now" | "next-heartbeat" }) => void;
dispatchAgentHook: (value: {
message: string;
name: string;
agentId?: string;
wakeMode: "now" | "next-heartbeat";
sessionKey: string;
deliver: boolean;
channel: HookMessageChannel;
to?: string;
model?: string;
thinking?: string;
timeoutSeconds?: number;
allowUnsafeExternalContent?: boolean;
}) => string;
dispatchAgentHook: (value: HookAgentDispatchPayload) => string;
};
function sendJson(res: ServerResponse, status: number, body: unknown) {

View File

@@ -7,7 +7,7 @@ import type { CronJob } from "../../cron/types.js";
import { requestHeartbeatNow } from "../../infra/heartbeat-wake.js";
import { enqueueSystemEvent } from "../../infra/system-events.js";
import type { createSubsystemLogger } from "../../logging/subsystem.js";
import type { HookMessageChannel, HooksConfigResolved } from "../hooks.js";
import type { HookAgentDispatchPayload, HooksConfigResolved } from "../hooks.js";
import { createHooksRequestHandler } from "../server-http.js";
type SubsystemLogger = ReturnType<typeof createSubsystemLogger>;
@@ -29,20 +29,7 @@ export function createGatewayHooksRequestHandler(params: {
}
};
const dispatchAgentHook = (value: {
message: string;
name: string;
agentId?: string;
wakeMode: "now" | "next-heartbeat";
sessionKey: string;
deliver: boolean;
channel: HookMessageChannel;
to?: string;
model?: string;
thinking?: string;
timeoutSeconds?: number;
allowUnsafeExternalContent?: boolean;
}) => {
const dispatchAgentHook = (value: HookAgentDispatchPayload) => {
const sessionKey = value.sessionKey.trim();
const mainSessionKey = resolveMainSessionKeyFromConfig();
const jobId = randomUUID();