Gitea CI / Build Android APK (pull_request) Canceled after 0s
Gitea CI / Build Windows x64 app (pull_request) Canceled after 0s
Gitea CI / Audit and package Windows x64 (pull_request) Canceled after 0s
Gitea CI / Assemble and validate CI artifacts (pull_request) Canceled after 0s
Gitea CI / Gitea CI passed (pull_request) Canceled after 0s
Gitea CI / Check code and tests (pull_request) Canceled after 6m29s
176 lines
6.1 KiB
Dart
176 lines
6.1 KiB
Dart
import 'package:cutdex/l10n/l10n.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../integrations/editor_tools.dart';
|
|
|
|
import 'package:cutdex_ui/icons.dart';
|
|
import 'package:cutdex_ui/widgets.dart';
|
|
import 'package:cutdex_ui/theme.dart';
|
|
|
|
import '../../integrations/mcp/mcp_settings.dart';
|
|
import 'settings_layout.dart';
|
|
import '../../settings/settings_operation.dart';
|
|
import '../../shared/widgets/error_notification_host.dart';
|
|
|
|
class McpSettingsPanel extends ConsumerStatefulWidget {
|
|
const McpSettingsPanel({super.key, this.workspace});
|
|
final (String, String)? workspace;
|
|
|
|
@override
|
|
ConsumerState<McpSettingsPanel> createState() => _McpSettingsPanelState();
|
|
}
|
|
|
|
class _McpSettingsPanelState extends ConsumerState<McpSettingsPanel> {
|
|
String? _port;
|
|
String? _error;
|
|
|
|
Future<void> _toggle(bool enabled, McpSettingsValue settings) async {
|
|
final port = int.tryParse(_port ?? '${settings.port}');
|
|
if (enabled && (port == null || port < 1 || port > 65535)) {
|
|
setState(() => _error = context.l10n.invalidPort);
|
|
return;
|
|
}
|
|
setState(() => _error = null);
|
|
await ref
|
|
.read(settingsOperationProvider('mcp').notifier)
|
|
.run(
|
|
() => ref
|
|
.read(mcpSettingsProvider.notifier)
|
|
.saveSettings(enabled: enabled, port: enabled ? port : null),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Theme(
|
|
data: AppTheme.settingsContentTheme(Theme.of(context)),
|
|
child: Builder(builder: _buildContent),
|
|
);
|
|
|
|
Widget _buildContent(BuildContext context) {
|
|
final operation = ref.watch(settingsOperationProvider('mcp'));
|
|
final error =
|
|
_error ?? (operation.hasError ? context.l10n.saveMcpFailed : null);
|
|
final settings = ref.watch(mcpSettingsProvider);
|
|
final value = settings.asData?.value;
|
|
final ids = widget.workspace;
|
|
return SettingsPageSection(
|
|
title: 'MCP',
|
|
icon: AppIcons.terminal,
|
|
children: [
|
|
Consumer(
|
|
builder: (context, ref, _) {
|
|
final connection = ids == null
|
|
? null
|
|
: ref.watch(editorMcpConnectionProvider(ids.$1, ids.$2));
|
|
final status = value?.enabled != true
|
|
? context.l10n.disabled
|
|
: ids == null
|
|
? context.l10n.waitingForProject
|
|
: connection!.hasError
|
|
? context.l10n.startFailed
|
|
: connection.isLoading
|
|
? context.l10n.starting
|
|
: context.l10n.running;
|
|
return AppField(
|
|
label: context.l10n.mcpService,
|
|
description: settings.isLoading
|
|
? context.l10n.loadingSettings
|
|
: status,
|
|
orientation: AppFieldOrientation.horizontal,
|
|
child: Align(
|
|
alignment: AlignmentDirectional.centerEnd,
|
|
child: AppSwitch(
|
|
semanticLabel: context.l10n.mcpService,
|
|
value: value?.enabled ?? false,
|
|
onChanged: value == null || operation.isLoading
|
|
? null
|
|
: (enabled) => _toggle(enabled, value),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
if (value != null) ...[
|
|
AppField(
|
|
orientation: AppFieldOrientation.horizontal,
|
|
label: context.l10n.listenPort,
|
|
child: SizedBox(
|
|
width: AppSettingsMetrics.controlWidth,
|
|
child: TextFormField(
|
|
style: AppTheme.inputTextStyle(context),
|
|
cursorHeight: AppTheme.inputCursorHeight(context),
|
|
textAlignVertical: TextAlignVertical.center,
|
|
initialValue: '${value.port}',
|
|
enabled: !value.enabled && !operation.isLoading,
|
|
keyboardType: TextInputType.number,
|
|
decoration: const InputDecoration(
|
|
isDense: true,
|
|
prefixText: '127.0.0.1 : ',
|
|
),
|
|
onChanged: (text) => _port = text,
|
|
),
|
|
),
|
|
),
|
|
AppField(
|
|
orientation: AppFieldOrientation.horizontal,
|
|
label: context.l10n.accessToken,
|
|
child: IconButton(
|
|
tooltip: context.l10n.copyAccessToken,
|
|
icon: const Icon(AppIcons.copy),
|
|
onPressed: () async {
|
|
await Clipboard.setData(ClipboardData(text: value.token));
|
|
if (context.mounted) {
|
|
ErrorNotificationHost.of(
|
|
context,
|
|
).showMessage(content: Text(context.l10n.accessTokenCopied));
|
|
}
|
|
},
|
|
),
|
|
),
|
|
],
|
|
if (settings.hasError)
|
|
TextButton(
|
|
onPressed: () => ref.invalidate(mcpSettingsProvider),
|
|
child: Text(context.l10n.loadMcpFailed),
|
|
),
|
|
Consumer(
|
|
builder: (context, ref, _) {
|
|
final connection = ids == null
|
|
? null
|
|
: ref.watch(editorMcpConnectionProvider(ids.$1, ids.$2));
|
|
if (value?.enabled != true || connection?.hasError != true) {
|
|
return const SizedBox.shrink();
|
|
}
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(
|
|
context.l10n.mcpStartError(
|
|
context.l10n.errorMessage(connection!.error!),
|
|
),
|
|
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
|
),
|
|
TextButton(
|
|
onPressed: operation.isLoading
|
|
? null
|
|
: () => ref.invalidate(
|
|
editorMcpConnectionProvider(ids!.$1, ids.$2),
|
|
),
|
|
child: Text(context.l10n.retryStart),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
if (error != null)
|
|
Text(
|
|
error,
|
|
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|