Skip to main content
Component: FileChangeHandler Module: gaia.utils.file_watcher Import: from gaia.utils import FileChangeHandler

Overview

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

  1. File Event Handling
    • Detect file creation
    • Detect file modification
    • Detect file deletion
    • Optional: Detect file moves
  2. Filtering
    • File extension filters
    • Filename pattern matching
    • Ignore patterns (e.g., temp files, hidden files)
  3. Debouncing
    • Prevent duplicate events
    • Configurable debounce time
    • Handle rapid successive changes
  4. Callback System
    • on_created(event) callback
    • on_modified(event) callback
    • on_deleted(event) callback
    • Pass file path and metadata
  5. Integration with watchdog
    • Extends FileSystemEventHandler
    • Works with Observer
    • Thread-safe

Non-Functional Requirements

  1. Performance
    • Low overhead monitoring
    • Efficient event filtering
    • Non-blocking callbacks
  2. Reliability
    • Handle edge cases (permission errors, symlinks)
    • Graceful degradation
    • Proper cleanup
  3. 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:
The callback-based design decouples the handler from any specific agent:

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.md

Implementation 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