Files
pipelines/app/lib/features/chat/conversation/chat_efforts.dart
T
alan b57405df29 Show a thinking-level control beside the chat model and mode
Material menu or Cupertino action sheet of the levels the chat's model
accepts, sent with chat_effort through the chat's own gateway. Hidden when
the model has none; says when a running turn keeps its level; re-reads the
levels after a model switch.
2026-11-09 20:16:13 +00:00

117 lines
3.8 KiB
Dart

import '../../../core/api_gateway.dart';
/// One thinking level the chat's model accepts, as its harness names it.
typedef ChatEffortLevel = ({String id, String name, String description});
/// A chat's thinking levels: the levels its current (or saved next-turn)
/// model accepts, the stored level (empty for the harness default) and the
/// level the harness uses when none was chosen, when it reports one.
///
/// The server sends these on the session row (`effort`, `efforts`,
/// `default_effort`) and in a `chat_effort` reply or a `chat_models` reply's
/// `chat_effort` field. No levels means the model has none the harness can
/// set, so no control is shown.
class ChatEffortState {
const ChatEffortState({
this.levels = const [],
this.effort = '',
this.defaultEffort = '',
});
/// Reads a session row or a `chat_effort` body.
factory ChatEffortState.fromJson(Object? value) {
if (value is! Map) return const ChatEffortState();
final raw = value['efforts'];
final levels = <ChatEffortLevel>[];
if (raw is List) {
for (final item in raw) {
if (item is! Map) continue;
final id = '${item['id'] ?? ''}'.trim();
if (id.isEmpty || levels.any((level) => level.id == id)) continue;
final name = '${item['name'] ?? ''}'.trim();
levels.add((
id: id,
name: name.isEmpty ? id : name,
description: '${item['description'] ?? ''}'.trim(),
));
}
}
return ChatEffortState(
levels: levels,
effort: '${value['effort'] ?? ''}'.trim(),
defaultEffort: '${value['default_effort'] ?? ''}'.trim(),
);
}
final List<ChatEffortLevel> levels;
final String effort;
final String defaultEffort;
bool get supported => levels.isNotEmpty;
/// The level in effect: the stored one, else the harness default.
String get current => effort.isNotEmpty ? effort : defaultEffort;
/// The current level's name, or "Default" when the harness did not say.
String get currentName {
for (final level in levels) {
if (level.id == current) return level.name;
}
return 'Default';
}
/// The facts the control displays, so an identical row never resets it.
String get signature =>
'$effort\u0000$defaultEffort\u0000${levels.map((l) => l.id).join(',')}';
/// The row with this state folded in.
JsonMap applyTo(JsonMap row) => {
...row,
'effort': effort,
'default_effort': defaultEffort,
'efforts': [
for (final level in levels)
{
'id': level.id,
'name': level.name,
if (level.description.isNotEmpty) 'description': level.description,
},
],
};
}
/// The header's label for [state].
String chatEffortLabel(ChatEffortState state) =>
'Thinking: ${state.currentName}';
/// The note shown with the levels: the choice never changes a running turn.
String chatEffortNote({required bool running}) => running
? 'A turn is running. The new level applies from the next turn you send.'
: 'Applies from the next turn you send.';
/// Sets the chat's level with `chat_effort` and returns the server's echo.
Future<ChatEffortState> selectChatEffort(
ApiGateway gateway,
String sessionId,
String effort,
) async {
final session = sessionId.trim();
if (session.isEmpty) throw ArgumentError('Select a chat session first.');
final reply = await gateway.call('chat_effort', {
'chat_effort': {'session_id': session, 'effort': effort.trim()},
});
return ChatEffortState.fromJson(reply['chat_effort']);
}
/// Re-reads the chat's levels after its model changed, from the
/// `chat_models` reply's `chat_effort` field.
Future<ChatEffortState> readChatEffort(
ApiGateway gateway,
String sessionId,
) async {
final reply = await gateway.call('chat_models', {
'chat_query': {'session_id': sessionId.trim()},
});
return ChatEffortState.fromJson(reply['chat_effort']);
}