Skip to content

Commit dbb20e7

Browse files
committed
refactor(plugins): consolidate lifecycle hook dispatch
1 parent 8f2850d commit dbb20e7

1 file changed

Lines changed: 68 additions & 198 deletions

File tree

‎src/plugins/hooks.ts‎

Lines changed: 68 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -996,30 +996,6 @@ export function createHookRunner(
996996
);
997997
}
998998

999-
/**
1000-
* Run model_call_started hook.
1001-
* Allows plugins to observe sanitized model-call metadata.
1002-
* Runs in parallel (fire-and-forget).
1003-
*/
1004-
async function runModelCallStarted(
1005-
event: PluginHookModelCallStartedEvent,
1006-
ctx: PluginHookAgentContext,
1007-
): Promise<void> {
1008-
return runVoidHook("model_call_started", event, ctx);
1009-
}
1010-
1011-
/**
1012-
* Run model_call_ended hook.
1013-
* Allows plugins to observe sanitized terminal model-call metadata.
1014-
* Runs in parallel (fire-and-forget).
1015-
*/
1016-
async function runModelCallEnded(
1017-
event: PluginHookModelCallEndedEvent,
1018-
ctx: PluginHookAgentContext,
1019-
): Promise<void> {
1020-
return runVoidHook("model_call_ended", event, ctx);
1021-
}
1022-
1023999
/**
10241000
* Run agent_end hook.
10251001
* Allows plugins to analyze completed conversations.
@@ -1033,24 +1009,6 @@ export function createHookRunner(
10331009
return runVoidHook("agent_end", withAgentRunId(event, ctx), ctx, optionsLocal);
10341010
}
10351011

1036-
/**
1037-
* Run llm_input hook.
1038-
* Allows plugins to observe the exact input payload sent to the LLM.
1039-
* Runs in parallel (fire-and-forget).
1040-
*/
1041-
async function runLlmInput(event: PluginHookLlmInputEvent, ctx: PluginHookAgentContext) {
1042-
return runVoidHook("llm_input", event, ctx);
1043-
}
1044-
1045-
/**
1046-
* Run llm_output hook.
1047-
* Allows plugins to observe the exact output payload returned by the LLM.
1048-
* Runs in parallel (fire-and-forget).
1049-
*/
1050-
async function runLlmOutput(event: PluginHookLlmOutputEvent, ctx: PluginHookAgentContext) {
1051-
return runVoidHook("llm_output", event, ctx);
1052-
}
1053-
10541012
/**
10551013
* Run before_agent_finalize hook.
10561014
* Allows plugins to request one more model pass before a natural final reply
@@ -1088,18 +1046,6 @@ export function createHookRunner(
10881046
return runVoidHook("after_compaction", event, ctx);
10891047
}
10901048

1091-
/**
1092-
* Run before_reset hook.
1093-
* Fired when /new or /reset clears a session, before messages are lost.
1094-
* Runs in parallel (fire-and-forget).
1095-
*/
1096-
async function runBeforeReset(
1097-
event: PluginHookBeforeResetEvent,
1098-
ctx: PluginHookAgentContext,
1099-
): Promise<void> {
1100-
return runVoidHook("before_reset", event, ctx);
1101-
}
1102-
11031049
// =========================================================================
11041050
// Message Hooks
11051051
// =========================================================================
@@ -1145,28 +1091,6 @@ export function createHookRunner(
11451091
);
11461092
}
11471093

1148-
/**
1149-
* Run message_received hook.
1150-
* Runs in parallel (fire-and-forget).
1151-
*/
1152-
async function runMessageReceived(
1153-
event: PluginHookMessageReceivedEvent,
1154-
ctx: PluginHookMessageContext,
1155-
): Promise<void> {
1156-
return runVoidHook("message_received", event, ctx);
1157-
}
1158-
1159-
/**
1160-
* Run channel_pairing_requested hook.
1161-
* Observation-only; slow/failing handlers must not block pairing flow.
1162-
*/
1163-
async function runChannelPairingRequested(
1164-
event: Parameters<PluginHookHandlerMap["channel_pairing_requested"]>[0],
1165-
ctx: Parameters<PluginHookHandlerMap["channel_pairing_requested"]>[1],
1166-
): Promise<void> {
1167-
return runVoidHook("channel_pairing_requested", event, ctx);
1168-
}
1169-
11701094
/**
11711095
* Run before_dispatch hook.
11721096
* Allows plugins to inspect or handle a message before model dispatch.
@@ -1296,17 +1220,6 @@ export function createHookRunner(
12961220
);
12971221
}
12981222

1299-
/**
1300-
* Run message_sent hook.
1301-
* Runs in parallel (fire-and-forget).
1302-
*/
1303-
async function runMessageSent(
1304-
event: PluginHookMessageSentEvent,
1305-
ctx: PluginHookMessageContext,
1306-
): Promise<void> {
1307-
return runVoidHook("message_sent", event, ctx);
1308-
}
1309-
13101223
/**
13111224
* Run before_agent_run gate hook.
13121225
* Fires after session resolution and workspace preparation, before model inference.
@@ -1524,28 +1437,6 @@ export function createHookRunner(
15241437
// Session Hooks
15251438
// =========================================================================
15261439

1527-
/**
1528-
* Run session_start hook.
1529-
* Runs in parallel (fire-and-forget).
1530-
*/
1531-
async function runSessionStart(
1532-
event: PluginHookSessionStartEvent,
1533-
ctx: PluginHookSessionContext,
1534-
): Promise<void> {
1535-
return runVoidHook("session_start", event, ctx);
1536-
}
1537-
1538-
/**
1539-
* Run session_end hook.
1540-
* Runs in parallel (fire-and-forget).
1541-
*/
1542-
async function runSessionEnd(
1543-
event: PluginHookSessionEndEvent,
1544-
ctx: PluginHookSessionContext,
1545-
): Promise<void> {
1546-
return runVoidHook("session_end", event, ctx);
1547-
}
1548-
15491440
/**
15501441
* @deprecated Core prepares thread-bound subagent bindings through channel
15511442
* session-binding adapters before subagent_spawned fires. This remains only
@@ -1579,62 +1470,10 @@ export function createHookRunner(
15791470
);
15801471
}
15811472

1582-
/**
1583-
* Run subagent_spawned hook.
1584-
* Runs in parallel (fire-and-forget).
1585-
*/
1586-
async function runSubagentSpawned(
1587-
event: PluginHookSubagentSpawnedEvent,
1588-
ctx: PluginHookSubagentContext,
1589-
): Promise<void> {
1590-
return runVoidHook("subagent_spawned", event, ctx);
1591-
}
1592-
1593-
/** Run portable subagent progress presentation hooks. */
1594-
async function runSubagentProgress(
1595-
event: PluginHookSubagentProgressEvent,
1596-
ctx: PluginHookSubagentContext,
1597-
): Promise<void> {
1598-
return runVoidHook("subagent_progress", event, ctx);
1599-
}
1600-
1601-
/**
1602-
* Run subagent_ended hook.
1603-
* Runs in parallel (fire-and-forget).
1604-
*/
1605-
async function runSubagentEnded(
1606-
event: PluginHookSubagentEndedEvent,
1607-
ctx: PluginHookSubagentContext,
1608-
): Promise<void> {
1609-
return runVoidHook("subagent_ended", event, ctx);
1610-
}
1611-
16121473
// =========================================================================
16131474
// Gateway Hooks
16141475
// =========================================================================
16151476

1616-
/**
1617-
* Run gateway_start hook.
1618-
* Runs in parallel (fire-and-forget).
1619-
*/
1620-
async function runGatewayStart(
1621-
event: PluginHookGatewayStartEvent,
1622-
ctx: PluginHookGatewayContext,
1623-
): Promise<void> {
1624-
return runVoidHook("gateway_start", event, ctx);
1625-
}
1626-
1627-
/**
1628-
* Run gateway_stop hook.
1629-
* Runs in parallel (fire-and-forget).
1630-
*/
1631-
async function runGatewayStop(
1632-
event: PluginHookGatewayStopEvent,
1633-
ctx: PluginHookGatewayContext,
1634-
): Promise<void> {
1635-
return runVoidHook("gateway_stop", event, ctx);
1636-
}
1637-
16381477
async function runHeartbeatPromptContribution(
16391478
event: PluginHeartbeatPromptContributionEvent,
16401479
ctx: PluginHookAgentContext,
@@ -1645,26 +1484,6 @@ export function createHookRunner(
16451484
>("heartbeat_prompt_contribution", event, ctx, { mergeResults: mergeAgentTurnPrepare });
16461485
}
16471486

1648-
/**
1649-
* Run cron_reconciled after the Gateway scheduler reaches a complete state.
1650-
*/
1651-
async function runCronReconciled(
1652-
event: PluginHookCronReconciledEvent,
1653-
ctx: PluginHookCronReconciledContext,
1654-
): Promise<void> {
1655-
return runVoidHook("cron_reconciled", event, ctx);
1656-
}
1657-
1658-
/**
1659-
* Run cron_changed hook for gateway-owned cron lifecycle changes.
1660-
*/
1661-
async function runCronChanged(
1662-
event: PluginHookCronChangedEvent,
1663-
ctx: PluginHookGatewayContext,
1664-
): Promise<void> {
1665-
return runVoidHook("cron_changed", event, ctx);
1666-
}
1667-
16681487
// =========================================================================
16691488
// Skill Hooks
16701489
// =========================================================================
@@ -1810,48 +1629,99 @@ export function createHookRunner(
18101629
runAgentTurnPrepare,
18111630
runBeforePromptBuild,
18121631
runBeforeAgentReply,
1813-
runModelCallStarted,
1814-
runModelCallEnded,
1815-
runLlmInput,
1816-
runLlmOutput,
1632+
runModelCallStarted: async (
1633+
event: PluginHookModelCallStartedEvent,
1634+
ctx: PluginHookAgentContext,
1635+
): Promise<void> => runVoidHook("model_call_started", event, ctx),
1636+
runModelCallEnded: async (
1637+
event: PluginHookModelCallEndedEvent,
1638+
ctx: PluginHookAgentContext,
1639+
): Promise<void> => runVoidHook("model_call_ended", event, ctx),
1640+
runLlmInput: async (
1641+
event: PluginHookLlmInputEvent,
1642+
ctx: PluginHookAgentContext,
1643+
): Promise<void> => runVoidHook("llm_input", event, ctx),
1644+
runLlmOutput: async (
1645+
event: PluginHookLlmOutputEvent,
1646+
ctx: PluginHookAgentContext,
1647+
): Promise<void> => runVoidHook("llm_output", event, ctx),
18171648
runBeforeAgentFinalize,
18181649
runAgentEnd,
18191650
runBeforeCompaction,
18201651
runAfterCompaction,
1821-
runBeforeReset,
1652+
runBeforeReset: async (
1653+
event: PluginHookBeforeResetEvent,
1654+
ctx: PluginHookAgentContext,
1655+
): Promise<void> => runVoidHook("before_reset", event, ctx),
18221656
// Lifecycle gate hooks
18231657
runBeforeAgentRun,
18241658
// Message hooks
18251659
runInboundClaim,
18261660
runInboundClaimForPlugin,
18271661
runInboundClaimForPluginOutcome,
1828-
runChannelPairingRequested,
1829-
runMessageReceived,
1662+
runChannelPairingRequested: async (
1663+
event: Parameters<PluginHookHandlerMap["channel_pairing_requested"]>[0],
1664+
ctx: Parameters<PluginHookHandlerMap["channel_pairing_requested"]>[1],
1665+
): Promise<void> => runVoidHook("channel_pairing_requested", event, ctx),
1666+
runMessageReceived: async (
1667+
event: PluginHookMessageReceivedEvent,
1668+
ctx: PluginHookMessageContext,
1669+
): Promise<void> => runVoidHook("message_received", event, ctx),
18301670
runBeforeDispatch,
18311671
runReplyDispatch,
18321672
runReplyPayloadSending,
18331673
runMessageSending,
1834-
runMessageSent,
1674+
runMessageSent: async (
1675+
event: PluginHookMessageSentEvent,
1676+
ctx: PluginHookMessageContext,
1677+
): Promise<void> => runVoidHook("message_sent", event, ctx),
18351678
// Tool hooks
18361679
runBeforeToolCall,
18371680
runAfterToolCall,
18381681
runToolResultPersist,
18391682
// Message write hooks
18401683
runBeforeMessageWrite,
18411684
// Session hooks
1842-
runSessionStart,
1843-
runSessionEnd,
1685+
runSessionStart: async (
1686+
event: PluginHookSessionStartEvent,
1687+
ctx: PluginHookSessionContext,
1688+
): Promise<void> => runVoidHook("session_start", event, ctx),
1689+
runSessionEnd: async (
1690+
event: PluginHookSessionEndEvent,
1691+
ctx: PluginHookSessionContext,
1692+
): Promise<void> => runVoidHook("session_end", event, ctx),
18441693
runSubagentSpawning,
18451694
runSubagentDeliveryTarget,
1846-
runSubagentSpawned,
1847-
runSubagentProgress,
1848-
runSubagentEnded,
1695+
runSubagentSpawned: async (
1696+
event: PluginHookSubagentSpawnedEvent,
1697+
ctx: PluginHookSubagentContext,
1698+
): Promise<void> => runVoidHook("subagent_spawned", event, ctx),
1699+
runSubagentProgress: async (
1700+
event: PluginHookSubagentProgressEvent,
1701+
ctx: PluginHookSubagentContext,
1702+
): Promise<void> => runVoidHook("subagent_progress", event, ctx),
1703+
runSubagentEnded: async (
1704+
event: PluginHookSubagentEndedEvent,
1705+
ctx: PluginHookSubagentContext,
1706+
): Promise<void> => runVoidHook("subagent_ended", event, ctx),
18491707
// Gateway hooks
1850-
runGatewayStart,
1851-
runGatewayStop,
1708+
runGatewayStart: async (
1709+
event: PluginHookGatewayStartEvent,
1710+
ctx: PluginHookGatewayContext,
1711+
): Promise<void> => runVoidHook("gateway_start", event, ctx),
1712+
runGatewayStop: async (
1713+
event: PluginHookGatewayStopEvent,
1714+
ctx: PluginHookGatewayContext,
1715+
): Promise<void> => runVoidHook("gateway_stop", event, ctx),
18521716
runHeartbeatPromptContribution,
1853-
runCronReconciled,
1854-
runCronChanged,
1717+
runCronReconciled: async (
1718+
event: PluginHookCronReconciledEvent,
1719+
ctx: PluginHookCronReconciledContext,
1720+
): Promise<void> => runVoidHook("cron_reconciled", event, ctx),
1721+
runCronChanged: async (
1722+
event: PluginHookCronChangedEvent,
1723+
ctx: PluginHookGatewayContext,
1724+
): Promise<void> => runVoidHook("cron_changed", event, ctx),
18551725
// Skill hooks
18561726
runSkillProposalEvaluate,
18571727
runSkillProposalChanged,

0 commit comments

Comments
 (0)