Files
cutdex/lib/integrations/mcp/mcp_settings.dart
T
lingjhf 27e77fff39 feat: initial Cutdex application
Add the Flutter video editing application with project management, media
import, timeline editing, preview, AI assistance, diagnostics and settings.

Include shared UI and native host packages, platform runners, application
tests and release tooling.

Provide usage and contribution guides, the MIT license and third-party
notices. Exclude local credentials, build outputs and validation data.
2026-10-05 23:36:23 +08:00

65 lines
1.8 KiB
Dart

import 'dart:convert';
import 'dart:math';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../persistence/settings/mcp_settings_repository.dart';
import '../../settings/mcp_settings_value.dart';
import '../../settings/settings_write_queue.dart';
export '../../settings/mcp_settings_value.dart';
part 'mcp_settings.g.dart';
@Riverpod(keepAlive: true)
class McpSettings extends _$McpSettings {
late McpSettingsRepository _repository;
final _writes = SettingsWriteQueue();
@override
Future<McpSettingsValue> build() async {
_repository = ref.watch(mcpSettingsRepositoryProvider);
final saved = await _repository.load();
if (saved != null) {
_validate(saved);
return saved;
}
final random = Random.secure();
final value = McpSettingsValue(
enabled: false,
port: 47831,
token: base64UrlEncode(List.generate(32, (_) => random.nextInt(256))),
);
await _persist(value);
return value;
}
Future<void> saveSettings({bool? enabled, int? port}) {
return _writes.run(() async {
if (!ref.mounted) return;
final current = await future;
if (!ref.mounted) return;
final next = current.copyWith(
enabled: enabled ?? current.enabled,
port: port ?? current.port,
);
_validate(next);
await _persist(next);
if (ref.mounted) state = AsyncData(next);
});
}
void _validate(McpSettingsValue value) {
if (value.port < 1 ||
value.port > 65535 ||
value.token.length < 32 ||
value.token.length > 1024 ||
value.token.contains('\r') ||
value.token.contains('\n')) {
throw ArgumentError('Invalid MCP configuration');
}
}
Future<void> _persist(McpSettingsValue value) => _repository.save(value);
}