MCPcopy Create free account
hub / github.com/Louisym/MiniCC / HookRunner

Class HookRunner

tutorials/08_hook_system.py:116–274  ·  view source on GitHub ↗

Hook 执行引擎。 负责在工具执行前后运行 hook 命令,收集结果。 对应源码: hooks.rs:49-206

Source from the content-addressed store, hash-verified

114
115
116class HookRunner:
117 """
118 Hook 执行引擎。
119
120 负责在工具执行前后运行 hook 命令,收集结果。
121 对应源码: hooks.rs:49-206
122 """
123
124 def __init__(self, config: HookConfig):
125 self.config = config
126
127 def run_pre_tool_use(self, tool_name: str, tool_input: str) -> HookRunResult:
128 """
129 运行所有 PreToolUse hook。
130
131 如果任何一个 hook 返回"拒绝",工具就不会被执行。
132 """
133 return self._run_commands(
134 event=HookEvent.PRE_TOOL_USE,
135 commands=self.config.pre_tool_use,
136 tool_name=tool_name,
137 tool_input=tool_input,
138 tool_output=None,
139 is_error=False,
140 )
141
142 def run_post_tool_use(
143 self, tool_name: str, tool_input: str,
144 tool_output: str, is_error: bool,
145 ) -> HookRunResult:
146 """
147 运行所有 PostToolUse hook。
148
149 如果任何一个 hook 返回"拒绝",工具结果会被标记为错误。
150 """
151 return self._run_commands(
152 event=HookEvent.POST_TOOL_USE,
153 commands=self.config.post_tool_use,
154 tool_name=tool_name,
155 tool_input=tool_input,
156 tool_output=tool_output,
157 is_error=is_error,
158 )
159
160 def _run_commands(
161 self,
162 event: HookEvent,
163 commands: list[str],
164 tool_name: str,
165 tool_input: str,
166 tool_output: Optional[str],
167 is_error: bool,
168 ) -> HookRunResult:
169 """
170 逐个运行 hook 命令。
171
172 重要:如果任何一个 hook 返回"拒绝"(exit code 2),
173 后续的 hook 不再运行,直接返回拒绝结果。

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected