Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions flocks/hooks/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Provides a lightweight hook registry and execution pipeline that mirrors
oh-my-opencode's lifecycle stages:
- chat.message
- llm.call.before
- llm.call.after
- tool.execute.before
- tool.execute.after
- event
Expand All @@ -20,6 +22,8 @@

class HookStage:
CHAT_MESSAGE = "chat.message"
LLM_BEFORE = "llm.call.before"
LLM_AFTER = "llm.call.after"
TOOL_BEFORE = "tool.execute.before"
TOOL_AFTER = "tool.execute.after"
EVENT = "event"
Expand All @@ -39,6 +43,12 @@ class HookBase:
async def chat_message(self, ctx: HookContext) -> None: # pragma: no cover - default no-op
return None

async def llm_before(self, ctx: HookContext) -> None: # pragma: no cover - default no-op
return None

async def llm_after(self, ctx: HookContext) -> None: # pragma: no cover - default no-op
return None

async def tool_before(self, ctx: HookContext) -> None: # pragma: no cover - default no-op
return None

Expand Down Expand Up @@ -98,6 +108,22 @@ async def run_chat_message(
) -> HookContext:
return await cls._run_stage(HookStage.CHAT_MESSAGE, input_data, output_data)

@classmethod
async def run_llm_before(
cls,
input_data: Dict[str, Any],
output_data: Optional[Dict[str, Any]] = None,
) -> HookContext:
return await cls._run_stage(HookStage.LLM_BEFORE, input_data, output_data)

@classmethod
async def run_llm_after(
cls,
input_data: Dict[str, Any],
output_data: Optional[Dict[str, Any]] = None,
) -> HookContext:
return await cls._run_stage(HookStage.LLM_AFTER, input_data, output_data)

@classmethod
async def run_tool_before(
cls,
Expand Down Expand Up @@ -174,6 +200,10 @@ async def _run_stage(
def _resolve_handler(hook: HookBase, stage: str) -> Optional[Callable[[HookContext], Awaitable[None]]]:
if stage == HookStage.CHAT_MESSAGE:
return getattr(hook, "chat_message", None)
if stage == HookStage.LLM_BEFORE:
return getattr(hook, "llm_before", None)
if stage == HookStage.LLM_AFTER:
return getattr(hook, "llm_after", None)
if stage == HookStage.TOOL_BEFORE:
return getattr(hook, "tool_before", None)
if stage == HookStage.TOOL_AFTER:
Expand Down
Loading
Loading