Source Code:
src/gaia/utils/file_watcher.pyComponent: FileChangeHandler
Module:
gaia.utils.file_watcher
Import: from gaia.utils import FileChangeHandlerOverview
FileChangeHandler provides a reusable file system watcher for GAIA agents. It monitors directories for file changes (create, modify, delete) and triggers callbacks, enabling agents to automatically process new files. Key Features:- File event detection (create, modify, delete)
- Callback-based architecture
- File extension filtering
- Pattern-based ignore rules
- Debouncing to prevent duplicate events
- Integration with watchdog library
Requirements
Functional Requirements
-
File Event Handling
- Detect file creation
- Detect file modification
- Detect file deletion
- Optional: Detect file moves
-
Filtering
- File extension filters
- Filename pattern matching
- Ignore patterns (e.g., temp files, hidden files)
-
Debouncing
- Prevent duplicate events
- Configurable debounce time
- Handle rapid successive changes
-
Callback System
on_created(event)callbackon_modified(event)callbackon_deleted(event)callback- Pass file path and metadata
-
Integration with watchdog
- Extends
FileSystemEventHandler - Works with
Observer - Thread-safe
- Extends
Non-Functional Requirements
-
Performance
- Low overhead monitoring
- Efficient event filtering
- Non-blocking callbacks
-
Reliability
- Handle edge cases (permission errors, symlinks)
- Graceful degradation
- Proper cleanup
-
Usability
- Simple API
- Good defaults
- Clear error messages
API Specification
File Location
Public Interface
Testing Requirements
Unit Tests
File:tests/sdk/test_file_change_handler.py
Usage Examples
Example 1: EMR Intake Agent (Auto-Process Forms)
Example 2: Document Indexing Agent
Implementation Details
Extraction from ChatAgent (Completed)
FileChangeHandler was originally embedded in hub/agents/python/chat/gaia_agent_chat/agent.py as a tightly-coupled inner class. It has been extracted to src/gaia/utils/file_watcher.py as a generic, callback-based implementation. ChatAgent now imports it from there:
Debouncing Implementation
Extension Filtering
Dependencies
Required Packages
Import Dependencies
Documentation Updates Required
docs/sdk/core/agent-system.mdx
Add new section after Tool Mixins:Update EMR Example
Replace manual file watching with FileChangeHandler in medical-intake-build-guide.mdImplementation Checklist
Step 1: Create File
- Create
src/gaia/utils/directory - Create
src/gaia/utils/__init__.py - Create
src/gaia/utils/file_watcher.py - Add copyright header
Step 2: Implement Class
- Extend
FileSystemEventHandler - Implement
__init__with callbacks - Implement
on_created() - Implement
on_modified() - Implement
on_deleted() - Implement
_should_process() - Implement
_is_debounced()
Step 3: Add Features
- Extension filtering
- Ignore patterns
- Debouncing
- Error handling in callbacks
- Logging
Step 4: Write Tests
- Create
tests/sdk/test_file_change_handler.py - Test imports
- Test callbacks
- Test filtering
- Test debouncing
- Test with real Observer
- Test error handling
Step 5: Export & Document
- Add to
src/gaia/__init__.py - Add to
__all__list - Update docs/sdk/core/agent-system.mdx
- Add examples to docs/sdk/core/agent-system.mdx
Step 6: Validate
- Can import:
from gaia import FileChangeHandler - Example code works
- All tests pass
- EMR agent can use it
FileChangeHandler Technical Specification