feat(app): add chat foundation
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// Chat wire adapter. ApiGateway supplies request ids and returns full envelopes.
|
||||
class ChatRepository {
|
||||
ChatRepository(this.gateway);
|
||||
final ApiGateway gateway;
|
||||
|
||||
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', {
|
||||
'chat_query': {'session_id': sessionId},
|
||||
}))['chat_models'],
|
||||
);
|
||||
Future<JsonMap> selectModel(
|
||||
String sessionId,
|
||||
String provider,
|
||||
String model,
|
||||
) => gateway.call('chat_model', {
|
||||
'chat_model': {
|
||||
'session_id': sessionId,
|
||||
'provider': provider,
|
||||
'model': model,
|
||||
},
|
||||
});
|
||||
Future<List<JsonMap>> shells(String sessionId) async => _maps(
|
||||
(await gateway.call('chat_shells', {
|
||||
'chat_query': {'session_id': sessionId},
|
||||
}))['chat_shells'],
|
||||
);
|
||||
Future<List<String>> directories() async =>
|
||||
((await gateway.call('chat_directories'))['chat_directories'] as List? ??
|
||||
const [])
|
||||
.whereType<String>()
|
||||
.toList();
|
||||
|
||||
Future<JsonMap> history(
|
||||
String sessionId, {
|
||||
int limit = 50,
|
||||
String cursor = '',
|
||||
}) async {
|
||||
final reply = await gateway.call('chat_messages', {
|
||||
'chat_query': {'session_id': sessionId, 'limit': limit, 'cursor': cursor},
|
||||
});
|
||||
final page = reply['chat_history'];
|
||||
if (page is Map) {
|
||||
return {
|
||||
'messages': _maps(page['messages']),
|
||||
'nextCursor': '${page['next_cursor'] ?? ''}',
|
||||
'hasMore': page['has_more'] == true,
|
||||
};
|
||||
}
|
||||
return {
|
||||
'messages': _maps(reply['chat_messages']),
|
||||
'nextCursor': '',
|
||||
'hasMore': false,
|
||||
};
|
||||
}
|
||||
|
||||
Future<JsonMap> subscribe(String sessionId) =>
|
||||
gateway.call('chat_subscribe', {
|
||||
'chat_subscribe': {'session_id': sessionId},
|
||||
});
|
||||
Future<JsonMap> create({
|
||||
required String title,
|
||||
required String directory,
|
||||
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(),
|
||||
},
|
||||
});
|
||||
Future<JsonMap> rename(String sessionId, String title) =>
|
||||
gateway.call('chat_rename', {
|
||||
'chat_rename': {'session_id': sessionId, 'title': title.trim()},
|
||||
});
|
||||
Future<JsonMap> star(String sessionId, bool starred) =>
|
||||
gateway.call('chat_star', {
|
||||
'chat_query': {'session_id': sessionId, 'starred': starred},
|
||||
});
|
||||
Future<JsonMap> close(String sessionId) => gateway.call('chat_close', {
|
||||
'chat_query': {'session_id': sessionId},
|
||||
});
|
||||
Future<JsonMap> reopen(String sessionId) => gateway.call('chat_reopen', {
|
||||
'chat_query': {'session_id': sessionId},
|
||||
});
|
||||
|
||||
Future<JsonMap> prompt(
|
||||
String sessionId,
|
||||
String text, {
|
||||
required String operationId,
|
||||
List<String> files = const [],
|
||||
}) {
|
||||
final body = <String, dynamic>{
|
||||
'session_id': sessionId,
|
||||
'text': text.trim(),
|
||||
'operation_id': operationId,
|
||||
};
|
||||
if (files.isNotEmpty) body['files'] = files;
|
||||
return gateway.call('chat_prompt', {'chat_prompt': body});
|
||||
}
|
||||
|
||||
Future<JsonMap> interrupt(String sessionId) =>
|
||||
gateway.call('chat_interrupt', {
|
||||
'chat_query': {'session_id': sessionId},
|
||||
});
|
||||
Future<JsonMap> answerForm(
|
||||
String sessionId,
|
||||
String formId,
|
||||
JsonMap answers, {
|
||||
String action = '',
|
||||
}) => gateway.call('chat_answer', {
|
||||
'chat_answer': {
|
||||
'session_id': sessionId,
|
||||
'form_id': formId,
|
||||
'answers': answers,
|
||||
if (action.isNotEmpty) 'action': action,
|
||||
},
|
||||
});
|
||||
Future<JsonMap> replyPermission(
|
||||
String sessionId,
|
||||
String requestId,
|
||||
String decision, {
|
||||
String client = 'web',
|
||||
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(),
|
||||
},
|
||||
});
|
||||
|
||||
static List<JsonMap> _maps(Object? value) => (value as List? ?? const [])
|
||||
.whereType<Map>()
|
||||
.map((item) => Map<String, dynamic>.from(item))
|
||||
.toList();
|
||||
}
|
||||
Reference in New Issue
Block a user