126 lines
3.6 KiB
Dart
126 lines
3.6 KiB
Dart
import "package:file_picker/file_picker.dart";
|
|
import "package:flutter/foundation.dart";
|
|
|
|
import "../../src/project_store.dart";
|
|
import "../../src/session/session_types.dart";
|
|
import "chat_provider.dart";
|
|
import "projects_provider.dart";
|
|
import "session_provider.dart";
|
|
import "settings_provider.dart";
|
|
|
|
class HomeCoordinator extends ChangeNotifier {
|
|
|
|
HomeCoordinator(this._projects, this._session, this._chat, this._settings);
|
|
|
|
final ProjectsProvider _projects;
|
|
final SessionProvider _session;
|
|
final ChatProvider _chat;
|
|
final SettingsProvider _settings;
|
|
|
|
String? _error;
|
|
String? get error => _error;
|
|
|
|
void clearError() {
|
|
_error = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
void _setError(String msg) {
|
|
_error = msg;
|
|
notifyListeners();
|
|
}
|
|
|
|
|
|
Future<void> pickProjectDirectory() async {
|
|
try {
|
|
final selectedDirectory = await FilePicker.platform.getDirectoryPath(
|
|
dialogTitle: "Select project directory",
|
|
);
|
|
|
|
if (selectedDirectory == null) return;
|
|
|
|
final project = await _projects.addProject(selectedDirectory);
|
|
if (project == null) {
|
|
_setError("The selected folder could not be added as a project.");
|
|
return;
|
|
}
|
|
|
|
_projects.selectProject(project.id);
|
|
_session.clearCurrentSession(workingDirectory: project.workingDirectory);
|
|
_chat.clearConversation();
|
|
await _settings.setActiveProject(project.workingDirectory);
|
|
} catch (e, st) {
|
|
print("Project directory picker failed: $e");
|
|
print(st);
|
|
_setError(e.toString());
|
|
}
|
|
}
|
|
|
|
Future<void> createNewChat() async {
|
|
final selectedProject = _projects.selectedProject;
|
|
if (selectedProject == null) {
|
|
_setError("Choose a project first so the new chat has a working directory.");
|
|
return;
|
|
}
|
|
|
|
await _session.createNewSession(
|
|
workingDirectory: selectedProject.workingDirectory,
|
|
name: "New Chat",
|
|
model: _settings.settings.model,
|
|
);
|
|
_settings.setThreadModel(_settings.settings.model);
|
|
_chat.setConversation(_session.getConversationHistory());
|
|
}
|
|
|
|
Future<void> selectProject(ProjectRecord project) async {
|
|
_projects.selectProject(project.id);
|
|
await _settings.setActiveProject(project.workingDirectory);
|
|
|
|
if (_session.currentSession?.workingDirectory == project.workingDirectory) return;
|
|
|
|
_session.clearCurrentSession(workingDirectory: project.workingDirectory);
|
|
_settings.setThreadModel(null);
|
|
_chat.clearConversation();
|
|
}
|
|
|
|
Future<void> openSession(SessionSummary session) async {
|
|
await _session.loadSession(session);
|
|
_chat.setConversation(_session.getConversationHistory());
|
|
_projects.selectProjectByWorkingDirectory(_session.activeWorkingDirectory);
|
|
_settings.setThreadModel(_session.currentSession?.model);
|
|
}
|
|
|
|
Future<void> sendMessage(String text) async {
|
|
if (text.isEmpty) return;
|
|
|
|
if (_session.currentSession == null) {
|
|
final selectedProject = _projects.selectedProject;
|
|
if (selectedProject == null) {
|
|
_setError("Pick a project before starting a chat.");
|
|
return;
|
|
}
|
|
await _session.createNewSession(
|
|
workingDirectory: selectedProject.workingDirectory,
|
|
name: "New Chat",
|
|
model: _settings.settings.model,
|
|
);
|
|
_settings.setThreadModel(_settings.settings.model);
|
|
_chat.setConversation(_session.getConversationHistory());
|
|
}
|
|
|
|
try {
|
|
await _chat.sendMessage(text);
|
|
} catch (e, st) {
|
|
print("Failed to send message: $e");
|
|
print(st);
|
|
_setError(e.toString());
|
|
} finally {
|
|
await _session.refreshSessions();
|
|
}
|
|
}
|
|
|
|
Future<void> deleteSession(SessionSummary session) async {
|
|
await _session.deleteSession(session);
|
|
}
|
|
|
|
}
|