MCPcopy
hub / github.com/anthropics/claude-code / _extract_field

Method _extract_field

plugins/hookify/core/rule_engine.py:182–254  ·  view source on GitHub ↗

Extract field value from tool input or hook input data. Args: field: Field name like "command", "new_text", "file_path", "reason", "transcript" tool_name: Tool being used (may be empty for Stop events) tool_input: Tool input dict input_data: F

(self, field: str, tool_name: str,
                      tool_input: Dict[str, Any], input_data: Dict[str, Any] = None)

Source from the content-addressed store, hash-verified

180 return False
181
182 def _extract_field(self, field: str, tool_name: str,
183 tool_input: Dict[str, Any], input_data: Dict[str, Any] = None) -> Optional[str]:
184 """Extract field value from tool input or hook input data.
185
186 Args:
187 field: Field name like "command", "new_text", "file_path", "reason", "transcript"
188 tool_name: Tool being used (may be empty for Stop events)
189 tool_input: Tool input dict
190 input_data: Full hook input (for accessing transcript_path, reason, etc.)
191
192 Returns:
193 Field value as string, or None if not found
194 """
195 # Direct tool_input fields
196 if field in tool_input:
197 value = tool_input[field]
198 if isinstance(value, str):
199 return value
200 return str(value)
201
202 # For Stop events and other non-tool events, check input_data
203 if input_data:
204 # Stop event specific fields
205 if field == 'reason':
206 return input_data.get('reason', '')
207 elif field == 'transcript':
208 # Read transcript file if path provided
209 transcript_path = input_data.get('transcript_path')
210 if transcript_path:
211 try:
212 with open(transcript_path, 'r') as f:
213 return f.read()
214 except FileNotFoundError:
215 print(f"Warning: Transcript file not found: {transcript_path}", file=sys.stderr)
216 return ''
217 except PermissionError:
218 print(f"Warning: Permission denied reading transcript: {transcript_path}", file=sys.stderr)
219 return ''
220 except (IOError, OSError) as e:
221 print(f"Warning: Error reading transcript {transcript_path}: {e}", file=sys.stderr)
222 return ''
223 except UnicodeDecodeError as e:
224 print(f"Warning: Encoding error in transcript {transcript_path}: {e}", file=sys.stderr)
225 return ''
226 elif field == 'user_prompt':
227 # For UserPromptSubmit events
228 return input_data.get('user_prompt', '')
229
230 # Handle special cases by tool type
231 if tool_name == 'Bash':
232 if field == 'command':
233 return tool_input.get('command', '')
234
235 elif tool_name in ['Write', 'Edit']:
236 if field == 'content':
237 # Write uses 'content', Edit has 'new_string'
238 return tool_input.get('content') or tool_input.get('new_string', '')
239 elif field == 'new_text' or field == 'new_string':

Callers 1

_check_conditionMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected