The-Agency/lib/ui/providers/session_provider.dart

161 lines
4.7 KiB
Dart

import "package:flutter/foundation.dart";
import "package:uuid/uuid.dart";
import "../../src/session/conversation_history.dart";
import "../../src/session/session_store.dart";
import "../../src/session/session_types.dart";
class SessionProvider extends ChangeNotifier {
SessionProvider() {
_loadSessions();
}
final SessionStore _sessionStore = SessionStore.instance;
final ConversationHistory _conversationHistory = ConversationHistory();
List<SessionSummary> _sessions = <SessionSummary>[];
String? _currentSessionId;
ConversationSession? _currentSession;
String? _activeWorkingDirectory;
List<SessionSummary> get sessions => _sessions;
String? get currentSessionId => _currentSessionId;
ConversationSession? get currentSession => _currentSession;
String? get activeWorkingDirectory => _activeWorkingDirectory;
List<SessionSummary> sessionsForWorkingDirectory(String? workingDirectory) {
final normalizedDirectory = workingDirectory?.trim();
if (normalizedDirectory == null || normalizedDirectory.isEmpty) {
return List<SessionSummary>.unmodifiable(_sessions);
}
return List<SessionSummary>.unmodifiable(
_sessions.where(
(session) => session.workingDirectory == normalizedDirectory,
),
);
}
void selectWorkingDirectory(String? workingDirectory) {
_activeWorkingDirectory = workingDirectory?.trim();
notifyListeners();
}
void clearCurrentSession({String? workingDirectory}) {
_conversationHistory.setSession(
ConversationSession(
id: "",
name: "",
created: DateTime.now().toUtc(),
updated: DateTime.now().toUtc(),
workingDirectory: workingDirectory?.trim(),
),
);
_currentSession = null;
_currentSessionId = null;
_activeWorkingDirectory = workingDirectory?.trim();
notifyListeners();
}
Future<void> _loadSessions() async {
try {
_sessions = await _sessionStore.listSessions();
notifyListeners();
} catch (error, stackTrace) {
_logException("Failed to load sessions", error, stackTrace);
_sessions = <SessionSummary>[];
}
}
Future<void> createNewSession({
String? workingDirectory,
String? name,
}) async {
try {
const uuid = Uuid();
final newSessionId = uuid.v4();
final now = DateTime.now().toUtc();
final normalizedDirectory = workingDirectory?.trim();
final newSession = ConversationSession(
id: newSessionId,
name: name ?? "New Chat",
created: now,
updated: now,
workingDirectory:
normalizedDirectory == null || normalizedDirectory.isEmpty
? null
: normalizedDirectory,
);
await _sessionStore.saveSession(newSession);
_conversationHistory.setSession(newSession);
_currentSession = newSession;
_currentSessionId = newSessionId;
_activeWorkingDirectory = newSession.workingDirectory;
await _loadSessions();
notifyListeners();
} catch (error, stackTrace) {
_logException("Failed to create a new session", error, stackTrace);
}
}
Future<void> loadSession(String id) async {
try {
final session = await _sessionStore.loadSession(id);
if (session != null) {
_conversationHistory.setSession(session);
_currentSession = session;
_currentSessionId = id;
_activeWorkingDirectory = session.workingDirectory;
notifyListeners();
}
} catch (error, stackTrace) {
_logException("Failed to load session $id", error, stackTrace);
_currentSession = null;
_currentSessionId = null;
_activeWorkingDirectory = null;
}
}
Future<void> deleteSession(String id) async {
try {
await _sessionStore.deleteSession(id);
if (_currentSessionId == id) {
_conversationHistory.setSession(
ConversationSession(
id: "",
name: "",
created: DateTime.now().toUtc(),
updated: DateTime.now().toUtc(),
),
);
_currentSession = null;
_currentSessionId = null;
_activeWorkingDirectory = null;
}
await _loadSessions();
notifyListeners();
} catch (error, stackTrace) {
_logException("Failed to delete session $id", error, stackTrace);
}
}
Future<void> refreshSessions() async {
try {
await _loadSessions();
} catch (error, stackTrace) {
_logException("Failed to refresh sessions", error, stackTrace);
}
}
ConversationHistory getConversationHistory() => _conversationHistory;
void _logException(String message, Object error, StackTrace stackTrace) {
print("$message: $error");
print(stackTrace);
}
}