src/hooks/json-error-recovery/
Responsibility
Provides automatic JSON error detection and recovery for OpenCode plugin tool execution. This hook monitors tool output for JSON parse errors and injects a standardized error reminder to guide users toward correcting their JSON syntax.
Design
Core Components
- JSON_ERROR_TOOL_EXCLUDE_LIST: Set of tools excluded from JSON error checking (bash, read, glob, webfetch, gh_grep_searchgithub, websearch_web_search_exa)
- JSON_ERROR_PATTERNS: Array of regex patterns for detecting various JSON error messages
- JSON_ERROR_REMINDER: Standardized error message template instructing users on JSON correction
- createJsonErrorRecoveryHook(): Factory function that returns the OpenCode plugin hook
Hook Architecture
The hook implements the OpenCode plugin's tool.execute.after lifecycle hook:
- Triggered after every tool execution
- Validates output is a string
- Checks for JSON error patterns in output
- Appends error reminder when JSON errors are detected
Error Detection Logic
- Tool Exclusion Check: Skips excluded tools immediately
- Output Type Check: Verifies output is a string before processing
- Marker Check: Skips output already containing the error reminder marker
- Pattern Matching: Tests output against multiple JSON error regex patterns
- Reminder Injection: Appends standardized error reminder to output
Flow
Tool Execution → tool.execute.after Hook Trigger →
[Check Exclusion] → [Validate Output Type] → [Check Marker] →
[Pattern Matching] → [Inject Reminder if JSON Error Detected] → Return Output
Detailed Execution Sequence
- Hook Registration:
createJsonErrorRecoveryHook() is called during plugin initialization
- Event Subscription: Hook subscribes to
tool.execute.after lifecycle event
- Filtering: Excluded tools are checked first for performance optimization
- Validation: Output type is verified to be a string
- Duplicate Prevention: Outputs already containing the error marker are skipped
- Pattern Testing: Output is tested against all JSON error patterns
- Reminder Injection: If any pattern matches, the standardized error reminder is appended to the output
- Result Return: Modified output is returned to the OpenCode plugin system
Integration
Consumers
- Primary Consumer: OpenCode plugin system via the
tool.execute.after lifecycle hook
- Error Path: JSON errors in tool arguments are detected and surfaced to users
Dependencies
- OpenCode Plugin SDK:
@opencode-ai/plugin for PluginInput type definitions
- Lifecycle Events: Relies on the
tool.execute.after event being emitted by OpenCode
Integration Points
- Plugin Initialization: Hook is created during plugin startup via
createJsonErrorRecoveryHook()
- Tool Execution Pipeline: Integrates into the post-execution phase of all tool calls
- User Feedback Loop: Provides immediate, actionable feedback when JSON errors occur
Configuration
The hook uses hardcoded constants for:
- Excluded tools list
- JSON error patterns
- Error reminder message
These can be extended or modified by updating the hook implementation.