feat(models): Add filters support and auto-validation for model-specific filters

This commit is contained in:
cogwheel0
2025-12-05 22:19:31 +05:30
parent f676f50c85
commit c630ce8c27
18 changed files with 469 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'toggle_filter.dart';
part 'model.freezed.dart';
@@ -17,6 +18,10 @@ sealed class Model with _$Model {
Map<String, dynamic>? metadata,
List<String>? supportedParameters,
List<String>? toolIds,
/// Toggleable filters that can be enabled/disabled per chat.
/// These come from OpenWebUI filters with `toggle = True`.
List<ToggleFilter>? filters,
}) = _Model;
factory Model.fromJson(Map<String, dynamic> json) {
@@ -147,6 +152,17 @@ sealed class Model with _$Model {
}
}
// Extract toggle filters from the model response
// These come from OpenWebUI filters with toggle=True set
List<ToggleFilter>? filters;
final filtersData = json['filters'];
if (filtersData is List && filtersData.isNotEmpty) {
filters = filtersData
.whereType<Map<String, dynamic>>()
.map((f) => ToggleFilter.fromJson(f))
.toList();
}
final idRaw = json['id'];
final id = idRaw?.toString();
if (id == null || id.isEmpty) {
@@ -174,6 +190,7 @@ sealed class Model with _$Model {
},
metadata: mergedMetadata,
toolIds: toolIds,
filters: filters,
);
}
@@ -190,6 +207,7 @@ sealed class Model with _$Model {
'metadata': metadata,
'architecture': capabilities?['architecture'],
'toolIds': toolIds,
'filters': filters?.map((f) => f.toJson()).toList(),
};
data.removeWhere((_, value) => value == null);
return data;

View File

@@ -0,0 +1,48 @@
import 'package:freezed_annotation/freezed_annotation.dart';
part 'toggle_filter.freezed.dart';
/// Represents a toggleable filter that can be enabled/disabled per chat.
///
/// These filters are created by OpenWebUI when a filter function has
/// `toggle = True` set in its module. They appear as buttons next to
/// web search, image generation, and code interpreter buttons.
@freezed
sealed class ToggleFilter with _$ToggleFilter {
const ToggleFilter._();
const factory ToggleFilter({
/// Unique identifier for the filter function.
required String id,
/// Display name for the filter.
required String name,
/// Optional description of what the filter does.
String? description,
/// Optional icon URL for the filter.
String? icon,
/// Whether this filter has user-configurable valves.
@Default(false) bool hasUserValves,
}) = _ToggleFilter;
factory ToggleFilter.fromJson(Map<String, dynamic> json) {
return ToggleFilter(
id: json['id'] as String,
name: json['name'] as String,
description: json['description'] as String?,
icon: json['icon'] as String?,
hasUserValves: json['has_user_valves'] as bool? ?? false,
);
}
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
if (description != null) 'description': description,
if (icon != null) 'icon': icon,
'has_user_valves': hasUserValves,
};
}