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.
This commit is contained in:
2026-11-09 20:16:13 +00:00
parent 06001697c8
commit b57405df29
7 changed files with 776 additions and 0 deletions
@@ -0,0 +1,226 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'package:pipelines_app/features/chat/conversation/chat_effort_control.dart';
import 'package:pipelines_app/features/chat/conversation/chat_efforts.dart';
const _levels = [
{'id': 'low', 'name': 'Low', 'description': 'Quick answers'},
{'id': 'high', 'name': 'High'},
{'id': 'xhigh', 'name': 'Extra high'},
];
JsonMap _row({
String effort = '',
String defaultEffort = 'high',
List<Object> levels = _levels,
String model = 'thinker',
}) => {
'id': 's',
'backend': 'codex',
'model': model,
'model_provider': 'openai',
'effort': effort,
'default_effort': defaultEffort,
'efforts': levels,
};
class _EffortGateway implements ApiGateway {
_EffortGateway({this.refuse});
final String? refuse;
final requests = <JsonMap>[];
JsonMap reread = const {};
@override
Stream<JsonMap> get frames => const Stream.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
requests.add({'type': type, ...fields});
if (type == 'chat_effort') {
final refused = refuse;
if (refused != null) throw ProtocolError(refused);
final effort = '${(fields['chat_effort'] as Map)['effort']}';
return {
'chat_effort': {
'session_id': 's',
'effort': effort,
'efforts': _levels,
'default_effort': 'high',
},
};
}
if (type == 'chat_models') {
return {'chat_models': const <Object>[], 'chat_effort': reread};
}
return {};
}
}
Widget _material(
ApiGateway gateway,
JsonMap row, {
bool running = false,
ValueChanged<ChatEffortState>? onChanged,
}) => MaterialApp(
home: Scaffold(
appBar: AppBar(
actions: [
ChatEffortControl(
gateway: gateway,
sessionId: 's',
session: row,
running: running,
onChanged: onChanged,
),
],
),
),
);
void main() {
test('state reads a row and folds an echo back into it', () {
final state = ChatEffortState.fromJson(_row(effort: 'xhigh'));
expect(state.levels.map((level) => level.id), ['low', 'high', 'xhigh']);
expect(state.currentName, 'Extra high');
expect(chatEffortLabel(ChatEffortState.fromJson(_row())), 'Thinking: High');
expect(
chatEffortLabel(ChatEffortState.fromJson(_row(defaultEffort: ''))),
'Thinking: Default',
);
expect(ChatEffortState.fromJson({'efforts': 'bad'}).supported, isFalse);
final folded = state.applyTo({'id': 's', 'title': 'Chat'});
expect(folded['title'], 'Chat');
expect(ChatEffortState.fromJson(folded).signature, state.signature);
});
testWidgets("shows the row's level without a request and switches it", (
tester,
) async {
final gateway = _EffortGateway();
final changes = <String>[];
await tester.pumpWidget(
_material(gateway, _row(), onChanged: (s) => changes.add(s.effort)),
);
await tester.pumpAndSettle();
expect(find.text('Thinking: High'), findsOneWidget);
expect(gateway.requests, isEmpty);
await tester.tap(find.byKey(const Key('chat-effort')));
await tester.pumpAndSettle();
expect(find.text('Applies from the next turn you send.'), findsOneWidget);
expect(find.text('Quick answers'), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('chat-effort-option-low')));
await tester.pumpAndSettle();
expect(gateway.requests.single, {
'type': 'chat_effort',
'chat_effort': {'session_id': 's', 'effort': 'low'},
});
expect(find.text('Thinking: Low'), findsOneWidget);
expect(changes, ['low']);
});
testWidgets('a model or harness without levels shows no control', (
tester,
) async {
await tester.pumpWidget(_material(_EffortGateway(), _row(levels: [])));
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-effort')), findsNothing);
await tester.pumpWidget(_material(_EffortGateway(), {'id': 's'}));
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-effort')), findsNothing);
});
testWidgets('a running turn keeps its level and the label says so', (
tester,
) async {
final gateway = _EffortGateway();
final row = _row();
await tester.pumpWidget(_material(gateway, row, running: true));
await tester.tap(find.byKey(const Key('chat-effort')));
await tester.pumpAndSettle();
expect(find.textContaining('A turn is running.'), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('chat-effort-option-xhigh')));
await tester.pumpAndSettle();
expect(find.text('Thinking: Extra high · next turn'), findsOneWidget);
await tester.pumpWidget(_material(gateway, row));
await tester.pumpAndSettle();
expect(find.text('Thinking: Extra high'), findsOneWidget);
});
testWidgets('a refusal keeps the level and reports the reason', (
tester,
) async {
final gateway = _EffortGateway(refuse: 'level refused');
await tester.pumpWidget(_material(gateway, _row()));
await tester.tap(find.byKey(const Key('chat-effort')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const ValueKey('chat-effort-option-low')));
await tester.pumpAndSettle();
expect(find.textContaining('level refused'), findsOneWidget);
expect(find.text('Thinking: High'), findsOneWidget);
});
testWidgets("a model switch re-reads the new model's levels", (tester) async {
final gateway = _EffortGateway();
gateway.reread = {
'session_id': 's',
'effort': '',
'efforts': [
{'id': 'max', 'name': 'Max'},
],
'default_effort': 'max',
};
final changes = <ChatEffortState>[];
await tester.pumpWidget(_material(gateway, _row(), onChanged: changes.add));
await tester.pumpAndSettle();
await tester.pumpWidget(
_material(gateway, _row(model: 'other'), onChanged: changes.add),
);
await tester.pumpAndSettle();
expect(gateway.requests.single['type'], 'chat_models');
expect(find.text('Thinking: Max'), findsOneWidget);
expect(changes.single.defaultEffort, 'max');
});
testWidgets('Cupertino uses an action sheet that reports a refusal', (
tester,
) async {
Widget app(ApiGateway gateway) => CupertinoApp(
home: CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
trailing: ChatEffortControl(
gateway: gateway,
sessionId: 's',
session: _row(),
),
),
child: const SizedBox.shrink(),
),
);
final refusing = _EffortGateway(refuse: 'level refused');
await tester.pumpWidget(app(refusing));
await tester.tap(find.byKey(const Key('chat-effort')));
await tester.pumpAndSettle();
expect(find.byType(CupertinoActionSheet), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('chat-effort-option-low')));
await tester.pumpAndSettle();
expect(find.byType(CupertinoActionSheet), findsOneWidget);
expect(find.textContaining('level refused'), findsOneWidget);
final gateway = _EffortGateway();
await tester.pumpWidget(app(gateway));
await tester.pumpAndSettle();
await tester.tapAt(const Offset(10, 10));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-effort')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const ValueKey('chat-effort-option-xhigh')));
await tester.pumpAndSettle();
expect(find.byType(CupertinoActionSheet), findsNothing);
expect(gateway.requests.single['type'], 'chat_effort');
});
}
@@ -0,0 +1,90 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'chat_thread_fixtures.dart';
/// The conversation header places the thinking control beside model and
/// mode only when the chat's model has levels.
Map<String, dynamic> _session({bool levels = true}) => {
'id': 's',
'backend': 'codex',
'model': 'gpt-test',
'model_provider': 'openai',
'mode': 'ask',
'mode_name': 'Ask for approval',
'directory': '/srv',
if (levels) ...{
'effort': 'high',
'efforts': [
{'id': 'low', 'name': 'Low'},
{'id': 'high', 'name': 'High'},
],
},
};
Future<void> _pump(
WidgetTester tester, {
required bool cupertino,
required Map<String, dynamic> session,
}) async {
tester.view.physicalSize = const Size(1200, 800);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage(const []);
gateway.subscribe = () async => const {};
final view = ChatThreadView(
gateway: gateway,
sessionId: 's',
title: 'Thinking',
session: session,
onBack: () {},
);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: view))
: MaterialApp(home: Scaffold(body: view)),
);
await tester.pumpAndSettle();
}
void main() {
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
testWidgets('$host header shows the level beside model and mode', (
tester,
) async {
await _pump(tester, cupertino: cupertino, session: _session());
final header = find.byKey(const Key('chat-thread-header'));
for (final key in ['chat-model', 'chat-mode', 'chat-effort']) {
expect(
find.descendant(of: header, matching: find.byKey(Key(key))),
findsOneWidget,
);
}
expect(find.text('Thinking: High'), findsOneWidget);
final mode = tester.getRect(find.byKey(const Key('chat-mode')));
final effort = tester.getRect(find.byKey(const Key('chat-effort')));
expect(effort.left, greaterThanOrEqualTo(mode.right));
expect((effort.center.dy - mode.center.dy).abs(), lessThan(2));
});
testWidgets('$host header has no thinking control without levels', (
tester,
) async {
await _pump(
tester,
cupertino: cupertino,
session: _session(levels: false),
);
expect(find.byKey(const Key('chat-model')), findsOneWidget);
expect(find.byKey(const Key('chat-effort')), findsNothing);
});
}
}