Chat: /new continues the same chat on a fresh session
The composer's /new used to create a separate chat. It now sends chat_new_session. The server starts a fresh native session on the chat's own harness (OpenCode, Codex or Claude Code) and re-keys the operator_sessions row in one transaction. The chat keeps its title, directory, backend, colour, star, model and mode. The old row is kept unlisted with replaced_by, and its native session stays resumable. The reply and the chat_session_changed push carry previous_id, so every client swaps the row in place and an open conversation follows it to the fresh, empty transcript with a notice.
This commit is contained in:
@@ -16,7 +16,7 @@ class _ModeScreenGateway implements ApiGateway {
|
||||
_claude('s', 'Wire chat', mode: 'default', name: 'Ask for permission'),
|
||||
];
|
||||
|
||||
/// The `chat_session` a create replies with.
|
||||
/// The `chat_session` a `/new` replies with, for the chat `s`.
|
||||
JsonMap created = _claude('n1', 'New chat');
|
||||
|
||||
/// Holds list reads while set, so a test can look before the re-read lands.
|
||||
@@ -33,8 +33,10 @@ class _ModeScreenGateway implements ApiGateway {
|
||||
final rows = [...sessions];
|
||||
await listGate?.future;
|
||||
return {'chat_sessions': rows};
|
||||
case 'chat_create':
|
||||
return {'chat_session': created};
|
||||
case 'chat_new_session':
|
||||
return {
|
||||
'chat_session': {...created, 'previous_id': 's'},
|
||||
};
|
||||
case 'chat_messages':
|
||||
return {
|
||||
'chat_history': {
|
||||
@@ -234,7 +236,7 @@ void main() {
|
||||
}
|
||||
|
||||
testWidgets(
|
||||
'a created chat whose reply names its mode shows it before the list re-reads',
|
||||
'a new session whose reply names its mode shows it before the list re-reads',
|
||||
(tester) async {
|
||||
final gateway = _ModeScreenGateway()
|
||||
..created = _claude('n1', 'New chat', mode: 'plan', name: 'Plan');
|
||||
@@ -258,7 +260,7 @@ void main() {
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'a created chat whose reply lacks its mode shows it when the list re-reads',
|
||||
'a new session whose reply lacks its mode shows it when the list re-reads',
|
||||
(tester) async {
|
||||
final gateway = _ModeScreenGateway();
|
||||
await _pump(
|
||||
@@ -271,12 +273,9 @@ void main() {
|
||||
await tester.pumpAndSettle();
|
||||
gateway
|
||||
..listGate = Completer<void>()
|
||||
..sessions = [
|
||||
_claude('n1', 'New chat', mode: 'plan', name: 'Plan'),
|
||||
...gateway.sessions,
|
||||
];
|
||||
..sessions = [_claude('n1', 'New chat', mode: 'plan', name: 'Plan')];
|
||||
await runNew(tester);
|
||||
// The row is open on the create reply, which names no mode.
|
||||
// The row is open on the /new reply, which names no mode.
|
||||
expect(find.text('New chat'), findsWidgets);
|
||||
expect(_shown(tester), isNull);
|
||||
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_commands.dart';
|
||||
import 'package:pipelines_app/features/chat/screen.dart';
|
||||
|
||||
/// A chat destination server whose `/new` re-keys chat `s` to `n1`, as the
|
||||
/// registry does, and which can push another device's move.
|
||||
class _NewSessionGateway implements ApiGateway {
|
||||
final calls = <({String type, JsonMap fields})>[];
|
||||
final pushed = StreamController<JsonMap>.broadcast();
|
||||
var current = _row('s');
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => pushed.stream;
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add((type: type, fields: fields));
|
||||
switch (type) {
|
||||
case 'chat_sessions':
|
||||
return {
|
||||
'chat_sessions': [current, _other],
|
||||
};
|
||||
case 'chat_new_session':
|
||||
current = _row('n1');
|
||||
return {
|
||||
'chat_session': {...current, 'previous_id': 's'},
|
||||
};
|
||||
case 'chat_messages':
|
||||
final query = fields['chat_query'] as JsonMap;
|
||||
final old = query['session_id'] == 's';
|
||||
return {
|
||||
'chat_history': {
|
||||
'messages': [
|
||||
if (old)
|
||||
{
|
||||
'id': 'm1',
|
||||
'role': 'user',
|
||||
'parts': [
|
||||
{'type': 'text', 'text': 'Earlier question'},
|
||||
],
|
||||
},
|
||||
],
|
||||
'next_cursor': '',
|
||||
'has_more': false,
|
||||
},
|
||||
};
|
||||
}
|
||||
return <String, dynamic>{};
|
||||
}
|
||||
|
||||
Iterable<String> subscribed() => [
|
||||
for (final call in calls)
|
||||
if (call.type == 'chat_subscribe')
|
||||
'${(call.fields['chat_subscribe'] as Map?)?['session_id']}',
|
||||
];
|
||||
}
|
||||
|
||||
JsonMap _row(String id) => {
|
||||
'id': id,
|
||||
'title': 'Wire chat',
|
||||
'backend': 'claude',
|
||||
'directory': '/w',
|
||||
'color': 'teal',
|
||||
'starred': true,
|
||||
'resume_command': 'cd /w && claude --resume $id',
|
||||
};
|
||||
|
||||
const JsonMap _other = {'id': 't', 'title': 'Other chat', 'directory': '/w'};
|
||||
|
||||
final _desktop = TargetPlatformVariant.only(TargetPlatform.linux);
|
||||
|
||||
Future<void> _pump(WidgetTester tester, _NewSessionGateway gateway) async {
|
||||
tester.view.physicalSize = const Size(1400, 900);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: ThemeData(platform: TargetPlatform.linux),
|
||||
home: Scaffold(body: ChatScreen(gateway: gateway)),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(const Key('chat-row-s')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Earlier question'), findsOneWidget);
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'/new keeps the chat in its row and opens the fresh, empty session',
|
||||
(tester) async {
|
||||
final gateway = _NewSessionGateway();
|
||||
await _pump(tester, gateway);
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-composer-field')),
|
||||
'/new',
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.pump();
|
||||
final field = tester.widget<EditableText>(
|
||||
find.descendant(
|
||||
of: find.byKey(const Key('chat-composer-field')),
|
||||
matching: find.byType(EditableText),
|
||||
),
|
||||
);
|
||||
if (field.controller.text.isNotEmpty) {
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
}
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.where((c) => c.type == 'chat_create'), isEmpty);
|
||||
expect(find.byKey(const Key('chat-row-s')), findsNothing);
|
||||
expect(find.byKey(const Key('chat-row-n1')), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-row-t')), findsOneWidget);
|
||||
expect(find.text(chatNewSessionNotice), findsOneWidget);
|
||||
expect(find.text('Earlier question'), findsNothing);
|
||||
expect(gateway.subscribed().last, 'n1');
|
||||
},
|
||||
variant: _desktop,
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'an open chat follows a new session started on another device',
|
||||
(tester) async {
|
||||
final gateway = _NewSessionGateway();
|
||||
await _pump(tester, gateway);
|
||||
gateway.current = _row('n1');
|
||||
gateway.pushed.add({
|
||||
'type': 'chat_session_changed',
|
||||
'chat_session': {...gateway.current, 'previous_id': 's'},
|
||||
});
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byKey(const Key('chat-row-s')), findsNothing);
|
||||
expect(find.byKey(const Key('chat-row-n1')), findsOneWidget);
|
||||
expect(find.text(chatNewSessionElsewhereNotice), findsOneWidget);
|
||||
expect(find.text('Earlier question'), findsNothing);
|
||||
expect(gateway.subscribed().last, 'n1');
|
||||
},
|
||||
variant: _desktop,
|
||||
);
|
||||
}
|
||||
@@ -52,7 +52,7 @@ void main() {
|
||||
}
|
||||
expect(find.text('/new'), findsOneWidget);
|
||||
expect(
|
||||
find.text('Start a fresh session in this directory'),
|
||||
find.text('Clear the context: continue this chat in a fresh session'),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
@@ -252,7 +252,7 @@ void main() {
|
||||
expect(gateway.promptBodies, isEmpty);
|
||||
expect(
|
||||
find.text(
|
||||
'/new — Start a fresh session in this directory\n'
|
||||
'/new — Clear the context: continue this chat in a fresh session\n'
|
||||
'/close — Finish this session for every client, keeping its history\n'
|
||||
'/rename <title> — Rename this session\n'
|
||||
'/stop — Stop the turn that is running\n'
|
||||
|
||||
@@ -13,20 +13,25 @@ import 'chat_composer_bar_fixtures.dart';
|
||||
/// on-screen keyboard's Return breaks the line instead.
|
||||
final _desktop = TargetPlatformVariant.only(TargetPlatform.linux);
|
||||
|
||||
/// Answers the session commands' create, close and rename requests.
|
||||
/// Answers the session commands' new-session, close and rename requests.
|
||||
class _SessionGateway extends ComposerBarGateway {
|
||||
String? refuse;
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
if (type == 'chat_create' ||
|
||||
if (type == 'chat_new_session' ||
|
||||
type == 'chat_close' ||
|
||||
type == 'chat_rename') {
|
||||
calls.add((type: type, fields: fields));
|
||||
if (refuse != null) throw ProtocolError(refuse!);
|
||||
if (type == 'chat_create') {
|
||||
if (type == 'chat_new_session') {
|
||||
return {
|
||||
'chat_session': {'id': 'n1', 'title': 'New chat', 'directory': '/w'},
|
||||
'chat_session': {
|
||||
'id': 'n1',
|
||||
'title': 'Old',
|
||||
'directory': '/w',
|
||||
'previous_id': 's',
|
||||
},
|
||||
};
|
||||
}
|
||||
return <String, dynamic>{};
|
||||
@@ -44,6 +49,7 @@ void main() {
|
||||
late _SessionGateway gateway;
|
||||
JsonMap? created;
|
||||
String? createdNotice;
|
||||
String? replacedId;
|
||||
String? renamed;
|
||||
var closed = 0;
|
||||
|
||||
@@ -51,6 +57,7 @@ void main() {
|
||||
gateway = _SessionGateway();
|
||||
created = null;
|
||||
createdNotice = null;
|
||||
replacedId = null;
|
||||
renamed = null;
|
||||
closed = 0;
|
||||
});
|
||||
@@ -71,7 +78,8 @@ void main() {
|
||||
drafts: MemoryChatDraftStore(),
|
||||
transcripts: transcripts,
|
||||
newOperationId: () => 'op-1',
|
||||
onCreatedSession: (session, notice) {
|
||||
onNewSession: (previousId, session, notice) {
|
||||
replacedId = previousId;
|
||||
created = session;
|
||||
createdNotice = notice;
|
||||
},
|
||||
@@ -95,32 +103,37 @@ void main() {
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
testWidgets('/new creates in this directory and hands the row over', (
|
||||
testWidgets('/new keeps this chat on a fresh session and hands it over', (
|
||||
tester,
|
||||
) async {
|
||||
await pump(tester);
|
||||
await run(tester, '/new');
|
||||
|
||||
final body = gateway.sent('chat_create').single['chat_create'] as JsonMap;
|
||||
expect(body['title'], 'New chat');
|
||||
expect(body['directory'], '/w');
|
||||
expect(body['operation_id'], 'op-1');
|
||||
expect(gateway.sent('chat_create'), isEmpty);
|
||||
final body =
|
||||
gateway.sent('chat_new_session').single['chat_new_session'] as JsonMap;
|
||||
expect(body['session_id'], 's');
|
||||
expect(body['instructions'], isNotEmpty);
|
||||
expect(replacedId, 's');
|
||||
expect(created?['id'], 'n1');
|
||||
expect(createdNotice, 'Started a new session “New chat” in /w.');
|
||||
expect(created?['title'], 'Old');
|
||||
expect(created?.containsKey('previous_id'), isFalse);
|
||||
expect(createdNotice, startsWith('New session started.'));
|
||||
expect(gateway.promptBodies, isEmpty);
|
||||
}, variant: _desktop);
|
||||
|
||||
testWidgets('/new without a directory sends nothing and reports it', (
|
||||
testWidgets('/new refused keeps the reason here and hands nothing over', (
|
||||
tester,
|
||||
) async {
|
||||
await pump(tester, directory: '');
|
||||
gateway.refuse = 'a turn is still running in this chat';
|
||||
await pump(tester);
|
||||
await run(tester, '/new');
|
||||
|
||||
expect(gateway.sent('chat_create'), isEmpty);
|
||||
expect(gateway.sent('chat_new_session'), hasLength(1));
|
||||
expect(created, isNull);
|
||||
expect(
|
||||
find.text(
|
||||
"Can't start a new session here: the server reported no directory "
|
||||
'for this chat.',
|
||||
"Couldn't start a new session: a turn is still running in this chat",
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
@@ -18,7 +18,7 @@ void main() {
|
||||
'/help',
|
||||
]);
|
||||
expect(chatCommands.map((command) => command.summary), [
|
||||
'Start a fresh session in this directory',
|
||||
'Clear the context: continue this chat in a fresh session',
|
||||
'Finish this session for every client, keeping its history',
|
||||
'Rename this session',
|
||||
'Stop the turn that is running',
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/data/chat_repository.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_controller.dart';
|
||||
|
||||
import 'chat_test_harness.dart';
|
||||
import 'fake_chat_gateway.dart';
|
||||
|
||||
/// `/new` keeps a chat: its row changes id in place, here and on the other
|
||||
/// devices that receive the move as `chat_session_changed` with `previous_id`.
|
||||
void main() {
|
||||
late FakeChatGateway gateway;
|
||||
late ChatSessionsController controller;
|
||||
|
||||
setUp(() async {
|
||||
gateway = FakeChatGateway();
|
||||
gateway.sessionsHandler = () async => {
|
||||
'chat_sessions': [
|
||||
{'id': 's1', 'title': 'First', 'directory': '/w'},
|
||||
{'id': 's2', 'title': 'Second', 'directory': '/w', 'color': 'teal'},
|
||||
{'id': 's3', 'title': 'Third', 'directory': '/w'},
|
||||
],
|
||||
};
|
||||
controller = ChatSessionsController(
|
||||
ChatRepository(gateway),
|
||||
localStore: MemoryChatSessionLocalStore(),
|
||||
);
|
||||
await controller.refresh();
|
||||
});
|
||||
tearDown(() => controller.dispose());
|
||||
|
||||
List<String> ids() => [for (final row in controller.sessions) '${row['id']}'];
|
||||
|
||||
test('a new session takes the row it replaces, in place', () {
|
||||
controller.replaceSession('s2', {
|
||||
'id': 'n2',
|
||||
'title': 'Second',
|
||||
'directory': '/w',
|
||||
'color': 'teal',
|
||||
});
|
||||
expect(ids(), ['s1', 'n2', 's3']);
|
||||
expect(controller.sessions[1]['color'], 'teal');
|
||||
});
|
||||
|
||||
test('a replaced row this device never listed joins at the head', () {
|
||||
controller.replaceSession('gone', {'id': 'n9', 'title': 'Elsewhere'});
|
||||
expect(ids(), ['n9', 's1', 's2', 's3']);
|
||||
});
|
||||
|
||||
test('another device\'s /new swaps the row live and is reported', () async {
|
||||
final lists = gateway.count('chat_sessions');
|
||||
final replaced = <(String, JsonMap)>[];
|
||||
controller.onSessionReplaced = (previous, row) =>
|
||||
replaced.add((previous, row));
|
||||
gateway.pushed.add({
|
||||
'type': 'chat_session_changed',
|
||||
'chat_session': {
|
||||
'id': 'n2',
|
||||
'previous_id': 's2',
|
||||
'title': 'Second',
|
||||
'directory': '/w',
|
||||
'color': 'teal',
|
||||
'resume_command': 'cd /w && claude --resume n2',
|
||||
},
|
||||
});
|
||||
await pumpEventQueue();
|
||||
expect(ids(), ['s1', 'n2', 's3']);
|
||||
expect(controller.sessions[1].containsKey('previous_id'), isFalse);
|
||||
expect(controller.sessions[1]['resume_command'], contains('n2'));
|
||||
expect(replaced.single.$1, 's2');
|
||||
expect(replaced.single.$2['id'], 'n2');
|
||||
expect(gateway.count('chat_sessions'), lists);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user