feat(app): add native chat sessions browser
This commit is contained in:
@@ -1,5 +1,13 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
const webChatClient = 'Web';
|
||||
|
||||
String nativeChatMessageId(String operationId) {
|
||||
final compact = operationId.replaceAll(RegExp('[^A-Za-z0-9]'), '');
|
||||
final capped = compact.length > 120 ? compact.substring(0, 120) : compact;
|
||||
return capped.isEmpty ? '' : 'msg_$capped';
|
||||
}
|
||||
|
||||
/// Chat wire adapter. ApiGateway supplies request ids and returns full envelopes.
|
||||
class ChatRepository {
|
||||
ChatRepository(this.gateway);
|
||||
@@ -7,11 +15,14 @@ class ChatRepository {
|
||||
|
||||
Future<List<JsonMap>> sessions() async =>
|
||||
_maps((await gateway.call('chat_sessions'))['chat_sessions']);
|
||||
Future<List<JsonMap>> models(String sessionId) async => _maps(
|
||||
(await gateway.call('chat_models', {
|
||||
Future<JsonMap> models(String sessionId) async {
|
||||
final reply = await gateway.call('chat_models', {
|
||||
'chat_query': {'session_id': sessionId},
|
||||
}))['chat_models'],
|
||||
);
|
||||
});
|
||||
final catalog = reply['chat_models'];
|
||||
return catalog is Map ? Map<String, dynamic>.from(catalog) : {};
|
||||
}
|
||||
|
||||
Future<JsonMap> selectModel(
|
||||
String sessionId,
|
||||
String provider,
|
||||
@@ -34,9 +45,29 @@ class ChatRepository {
|
||||
.whereType<String>()
|
||||
.toList();
|
||||
|
||||
Future<List<String>> backends() async =>
|
||||
((await gateway.call('chat_backends'))['chat_backends'] as List? ??
|
||||
const [])
|
||||
.whereType<String>()
|
||||
.toList();
|
||||
|
||||
Future<JsonMap> shellOutput(
|
||||
String sessionId,
|
||||
String shellId, {
|
||||
int cursor = 0,
|
||||
int limit = 65536,
|
||||
}) => gateway.call('chat_shell_output', {
|
||||
'chat_shell_output_query': {
|
||||
'session_id': sessionId,
|
||||
'shell_id': shellId,
|
||||
'cursor': cursor,
|
||||
'limit': limit,
|
||||
},
|
||||
});
|
||||
|
||||
Future<JsonMap> history(
|
||||
String sessionId, {
|
||||
int limit = 50,
|
||||
int limit = 200,
|
||||
String cursor = '',
|
||||
}) async {
|
||||
final reply = await gateway.call('chat_messages', {
|
||||
@@ -67,15 +98,21 @@ class ChatRepository {
|
||||
required String operationId,
|
||||
String backend = 'opencode',
|
||||
String instructions = '',
|
||||
}) => gateway.call('chat_create', {
|
||||
'chat_create': {
|
||||
'title': title,
|
||||
'directory': directory,
|
||||
'operation_id': operationId,
|
||||
if (backend.isNotEmpty && backend != 'opencode') 'backend': backend,
|
||||
if (instructions.trim().isNotEmpty) 'instructions': instructions.trim(),
|
||||
},
|
||||
});
|
||||
}) {
|
||||
if (title.trim().isEmpty || directory.trim().isEmpty) {
|
||||
throw ArgumentError('Enter a chat name and choose a directory.');
|
||||
}
|
||||
return gateway.call('chat_create', {
|
||||
'chat_create': {
|
||||
'title': title.trim(),
|
||||
'directory': directory.trim(),
|
||||
'operation_id': operationId,
|
||||
if (backend.isNotEmpty && backend != 'opencode') 'backend': backend,
|
||||
if (instructions.trim().isNotEmpty) 'instructions': instructions.trim(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Future<JsonMap> rename(String sessionId, String title) =>
|
||||
gateway.call('chat_rename', {
|
||||
'chat_rename': {'session_id': sessionId, 'title': title.trim()},
|
||||
@@ -96,13 +133,22 @@ class ChatRepository {
|
||||
String text, {
|
||||
required String operationId,
|
||||
List<String> files = const [],
|
||||
List<JsonMap> attachments = const [],
|
||||
}) {
|
||||
if (sessionId.trim().isEmpty) {
|
||||
throw ArgumentError('Select a chat session first.');
|
||||
}
|
||||
if (text.trim().isEmpty) {
|
||||
throw ArgumentError('Enter a message before sending.');
|
||||
}
|
||||
final body = <String, dynamic>{
|
||||
'session_id': sessionId,
|
||||
'text': text.trim(),
|
||||
'operation_id': operationId,
|
||||
'native_message_id': nativeChatMessageId(operationId),
|
||||
};
|
||||
if (files.isNotEmpty) body['files'] = files;
|
||||
if (attachments.isNotEmpty) body['attachments'] = attachments;
|
||||
return gateway.call('chat_prompt', {'chat_prompt': body});
|
||||
}
|
||||
|
||||
@@ -127,17 +173,28 @@ class ChatRepository {
|
||||
String sessionId,
|
||||
String requestId,
|
||||
String decision, {
|
||||
String client = 'web',
|
||||
String client = webChatClient,
|
||||
String message = '',
|
||||
}) => gateway.call('chat_reply', {
|
||||
'chat_reply': {
|
||||
'session_id': sessionId,
|
||||
'request_id': requestId,
|
||||
'decision': decision,
|
||||
'client': client,
|
||||
if (message.trim().isNotEmpty) 'message': message.trim(),
|
||||
},
|
||||
});
|
||||
}) {
|
||||
if (sessionId.trim().isEmpty) {
|
||||
throw ArgumentError('Select a chat session first.');
|
||||
}
|
||||
if (requestId.trim().isEmpty) {
|
||||
throw ArgumentError('This approval request is no longer available.');
|
||||
}
|
||||
if (!const ['once', 'always', 'reject'].contains(decision.trim())) {
|
||||
throw ArgumentError('Choose once, always or reject.');
|
||||
}
|
||||
return gateway.call('chat_reply', {
|
||||
'chat_reply': {
|
||||
'session_id': sessionId,
|
||||
'request_id': requestId,
|
||||
'decision': decision,
|
||||
'client': client,
|
||||
if (message.trim().isNotEmpty) 'message': message.trim(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
static List<JsonMap> _maps(Object? value) => (value as List? ?? const [])
|
||||
.whereType<Map>()
|
||||
|
||||
Reference in New Issue
Block a user