6cd7774811
A chat opened from a run or pipeline now goes in a new pane beside the active chat instead of replacing it, becomes active and takes the keyboard once built. Settings > Open in chat chooses its harness and model (models listed from that harness's most recent chat).
187 lines
6.2 KiB
Dart
187 lines
6.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../application/open_in_chat_preferences.dart';
|
|
import '../../core/api_gateway.dart';
|
|
import '../chat/data/chat_repository.dart';
|
|
import 'settings_group.dart';
|
|
|
|
/// The harness and model Open in chat (a run's or pipeline's More menu)
|
|
/// starts its chats with. The server lists models only for an existing chat,
|
|
/// so a harness's models come from its most recent chat; with none, only the
|
|
/// harness default is offered.
|
|
class SettingsOpenInChatSection extends StatefulWidget {
|
|
const SettingsOpenInChatSection({super.key, required this.gateway});
|
|
|
|
final ApiGateway gateway;
|
|
|
|
@override
|
|
State<SettingsOpenInChatSection> createState() =>
|
|
_SettingsOpenInChatSectionState();
|
|
}
|
|
|
|
class _SettingsOpenInChatSectionState extends State<SettingsOpenInChatSection> {
|
|
late final ChatRepository _chats = ChatRepository(widget.gateway);
|
|
OpenInChatPreferences _chosen = const OpenInChatPreferences();
|
|
List<String>? _harnesses;
|
|
List<JsonMap> _models = const [];
|
|
bool _loadingModels = false;
|
|
String? _error;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_load();
|
|
}
|
|
|
|
Future<void> _load() async {
|
|
try {
|
|
final chosen = await OpenInChatPreferences.load(widget.gateway);
|
|
final harnesses = await _chats.backends();
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_chosen = chosen;
|
|
_harnesses = harnesses;
|
|
});
|
|
await _loadModels(chosen.harness);
|
|
} catch (error) {
|
|
if (mounted) setState(() => _error = '$error');
|
|
}
|
|
}
|
|
|
|
/// The models of [harness]'s most recent chat, or none.
|
|
Future<void> _loadModels(String harness) async {
|
|
setState(() {
|
|
_models = const [];
|
|
_loadingModels = harness.isNotEmpty;
|
|
});
|
|
if (harness.isEmpty) return;
|
|
try {
|
|
final sessions = await _chats.sessions();
|
|
final latest =
|
|
sessions.where((session) => session['backend'] == harness).toList()
|
|
..sort(
|
|
(a, b) => '${b['updated_at'] ?? ''}'.compareTo(
|
|
'${a['updated_at'] ?? ''}',
|
|
),
|
|
);
|
|
final models = latest.isEmpty
|
|
? const <JsonMap>[]
|
|
: await _chats.models('${latest.first['id']}');
|
|
if (mounted && _chosen.harness == harness) {
|
|
setState(() {
|
|
_models = models;
|
|
_loadingModels = false;
|
|
});
|
|
}
|
|
} catch (_) {
|
|
if (mounted) setState(() => _loadingModels = false);
|
|
}
|
|
}
|
|
|
|
Future<void> _save(OpenInChatPreferences next) async {
|
|
setState(() => _chosen = next);
|
|
await next.save(widget.gateway);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final harnesses = _harnesses;
|
|
final error = _error;
|
|
return SettingsGroup(
|
|
title: 'Open in chat',
|
|
footer: Text(
|
|
'Used when a run or pipeline is opened in a new chat. Models come '
|
|
"from this harness's most recent chat.",
|
|
),
|
|
children: [
|
|
if (error != null)
|
|
ListTile(
|
|
leading: const Icon(Icons.error_outline),
|
|
title: const Text('Could not load the harnesses'),
|
|
subtitle: Text(error),
|
|
trailing: TextButton(
|
|
onPressed: () {
|
|
setState(() => _error = null);
|
|
_load();
|
|
},
|
|
child: const Text('Retry'),
|
|
),
|
|
)
|
|
else if (harnesses == null)
|
|
const ListTile(
|
|
leading: SizedBox.square(
|
|
dimension: 20,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
),
|
|
title: Text('Loading harnesses…'),
|
|
)
|
|
else ...[
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 12, 16, 8),
|
|
child: DropdownMenu<String>(
|
|
key: const Key('open-in-chat-harness'),
|
|
expandedInsets: EdgeInsets.zero,
|
|
label: const Text('Harness'),
|
|
initialSelection: harnesses.contains(_chosen.harness)
|
|
? _chosen.harness
|
|
: '',
|
|
dropdownMenuEntries: [
|
|
const DropdownMenuEntry(value: '', label: 'Default'),
|
|
for (final harness in harnesses)
|
|
DropdownMenuEntry(value: harness, label: harness),
|
|
],
|
|
onSelected: (harness) async {
|
|
final value = harness ?? '';
|
|
if (value == _chosen.harness) return;
|
|
await _save(OpenInChatPreferences(harness: value));
|
|
await _loadModels(value);
|
|
},
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 8, 16, 12),
|
|
child: DropdownMenu<String>(
|
|
key: ValueKey('open-in-chat-model-${_chosen.harness}'),
|
|
expandedInsets: EdgeInsets.zero,
|
|
enabled: _chosen.harness.isNotEmpty && !_loadingModels,
|
|
label: Text(_loadingModels ? 'Loading models…' : 'Model'),
|
|
initialSelection: _chosen.hasModel
|
|
? '${_chosen.provider}/${_chosen.model}'
|
|
: '',
|
|
dropdownMenuEntries: [
|
|
const DropdownMenuEntry(value: '', label: 'Harness default'),
|
|
if (_chosen.hasModel &&
|
|
!_models.any(
|
|
(m) =>
|
|
'${m['provider']}/${m['id']}' ==
|
|
'${_chosen.provider}/${_chosen.model}',
|
|
))
|
|
DropdownMenuEntry(
|
|
value: '${_chosen.provider}/${_chosen.model}',
|
|
label: _chosen.model,
|
|
),
|
|
for (final model in _models)
|
|
DropdownMenuEntry(
|
|
value: '${model['provider']}/${model['id']}',
|
|
label: '${model['name'] ?? model['id']}',
|
|
),
|
|
],
|
|
onSelected: (value) {
|
|
final picked = value ?? '';
|
|
final slash = picked.indexOf('/');
|
|
_save(
|
|
OpenInChatPreferences(
|
|
harness: _chosen.harness,
|
|
provider: slash < 0 ? '' : picked.substring(0, slash),
|
|
model: slash < 0 ? '' : picked.substring(slash + 1),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
],
|
|
);
|
|
}
|
|
}
|