Skip to content

Conversation

@sveneisenschmidt
Copy link
Owner

Resolves #29

Problem

The two model loaders had inconsistent behavior when watching for changes:

  • N8nApiModelLoader: Fired watch callback on every poll regardless of whether models changed
  • JsonFileModelLoader: Only fired callback when file content changed

This inconsistency blocks a proper solution for #30 (webhook notifications on model changes).

Changes

Base ModelLoader Class

  • Added constructor with lastHash property for change tracking
  • Added getModelsHash(models) method for consistent hash calculation across all loaders
  • Both loaders now inherit this shared functionality

N8nApiModelLoader

  • Now uses getModelsHash() from base class instead of firing callback on every poll
  • Compares model hash before firing callback
  • Renamed lastModelsHash to lastHash for consistency
  • Removed duplicate crypto import

JsonFileModelLoader

  • Switched from file hash to model hash comparison
  • Now uses getModelsHash() from base class
  • Removed getFileHash() method (no longer needed)
  • Removed crypto import (now in base class)

Tests

  • Updated N8nApiModelLoader tests to use lastHash instead of lastModelsHash
  • Added two new tests for change detection in N8nApiModelLoader
  • Updated JsonFileModelLoader watch test to handle new behavior

Benefits

  • Callbacks only fire when models actually change (not on every poll)
  • Consistent behavior across all model loaders
  • Enables proper webhook notifications for model changes (Improvement: Add notifier function to call webhooks on model change #30)
  • Formatting changes (whitespace in models.json) no longer trigger reloads
  • Shared code in base class reduces duplication

Testing

All 367 tests pass with 95.82% code coverage.

@github-actions
Copy link

Docker image for this PR is ready:

docker pull ghcr.io/sveneisenschmidt/n8n-openai-bridge:pr-31

Test it locally:

docker run -d -p 3333:3333 -e BEARER_TOKEN=test-token ghcr.io/sveneisenschmidt/n8n-openai-bridge:pr-31

Resolves #29

Problem:
- N8nApiModelLoader fired watch callback on every poll regardless of changes
- JsonFileModelLoader only fired callback when file content changed
- This inconsistency blocked proper solution for #30

Changes:
- Added getModelsHash() method to base ModelLoader class
- Both loaders now use consistent lastHash variable name
- N8nApiModelLoader now compares model hash before firing callback
- JsonFileModelLoader switched from file hash to model hash comparison
- Updated tests to reflect new behavior

Benefits:
- Callbacks only fire when models actually change
- Consistent behavior across all model loaders
- Enables proper webhook notifications for model changes
- Formatting changes (whitespace) no longer trigger reloads
@sveneisenschmidt sveneisenschmidt force-pushed the fix/model-loader-callback-consistency branch from b38e65a to a0719f6 Compare October 25, 2025 06:22
@github-actions
Copy link

Docker image for this PR is ready:

docker pull ghcr.io/sveneisenschmidt/n8n-openai-bridge:pr-31

Test it locally:

docker run -d -p 3333:3333 -e BEARER_TOKEN=test-token ghcr.io/sveneisenschmidt/n8n-openai-bridge:pr-31

@sveneisenschmidt sveneisenschmidt merged commit a61628e into main Oct 25, 2025
10 checks passed
@sveneisenschmidt sveneisenschmidt deleted the fix/model-loader-callback-consistency branch October 25, 2025 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix discrepancy between N8nApiModelLoader and JsonFileModelLoader

2 participants