Files

70 lines
2.6 KiB
Dart

import '../../../core/api_gateway.dart';
/// A model switch the server accepted, as its own reply reported it.
typedef ChatModelSelection = ({
String provider,
String model,
String selection,
});
/// Harnesses with a verified catalog and model-selection path, matching the
/// web's `supportsChatModels`.
bool supportsChatModels(String backend) =>
const ['', 'opencode', 'codex', 'claude'].contains(backend.trim());
/// The recorded current model, or the web's "Model" when none was recorded.
String chatModelLabel(String model, String selection) {
final recorded = model.trim();
final label = recorded.isEmpty ? 'Model' : recorded;
return selection == 'next_turn' ? '$label · next turn' : label;
}
/// The helper shown above the catalog, verbatim from the web.
String chatModelSelectionNote(String backend) {
if (backend.trim() == 'codex') {
return 'Saved by Pipelines for the next turn you send. Does not resume this '
'thread or edit global Codex configuration.';
}
if (backend.trim() == 'claude') {
return 'Saved by Pipelines for the next turn you send. Does not edit your '
'global Claude Code configuration.';
}
return 'Applies to the next turn in this chat.';
}
/// A model's display name, or its id when the server gave no name.
String chatModelName(JsonMap model) {
final name = '${model['name'] ?? ''}'.trim();
return name.isNotEmpty ? name : '${model['id'] ?? ''}'.trim();
}
/// The rows a query matches: a case-insensitive substring over the display
/// name, id and provider, in the server's order. A blank query keeps the whole
/// catalog, and a row with neither name nor id is never offered.
List<JsonMap> filterChatModels(List<JsonMap> models, String query) {
final needle = query.trim().toLowerCase();
final result = <JsonMap>[];
for (final model in models) {
if (chatModelName(model).isEmpty) continue;
if (needle.isNotEmpty) {
final name = '${model['name'] ?? ''}';
final id = '${model['id'] ?? ''}';
final provider = '${model['provider'] ?? ''}';
final haystack = '$name\u0000$id\u0000$provider'.toLowerCase();
if (!haystack.contains(needle)) continue;
}
result.add(model);
}
return result;
}
/// The recorded facts a model control displays, so a row replaced with the
/// same values never resets a newer local choice.
({String provider, String model, String selection}) chatModelRecordedFacts(
JsonMap session,
) => (
provider: '${session['model_provider'] ?? ''}'.trim(),
model: '${session['model'] ?? ''}'.trim(),
selection: '${session['model_selection'] ?? ''}'.trim(),
);