import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../integrations/platform/file_picker.dart'; import '../../integrations/platform/platform_capabilities.dart'; import '../../workspace/media_file_extensions.dart'; import '../../workspace/external_media_import.dart'; import '../../workspace/projects.dart'; import 'agent_composer_controller.dart'; import 'agent_media_attachments.dart'; import 'composer_document.dart'; /// Composition survives presentation changes, including temporarily hidden chat. final agentComposerStoreProvider = Provider((ref) { final store = AgentComposerStore( createComposer: (projectId) => AgentComposerController( canImport: () => ref.mounted && projectId != null && ref.read(platformCapabilitiesProvider).localFileSelection && ref.read(projectsProvider).value?.activeProject?.id == projectId && ref.read(projectsProvider).value?.projectBusy == false && !ref.read(externalMediaImportProvider(projectId)).isLoading, pickFiles: () => ref .read(filePickerProvider) .pickFiles(allowedExtensions: selectableMediaFileExtensions), importMedia: (files) => ref .read(externalMediaImportProvider(projectId!).notifier) .importFiles(files), ), ); ref.onDispose(store.dispose); return store; }); final class AgentComposerStore { AgentComposerStore({required this.createComposer}); final AgentComposerController Function(String?) createComposer; ComposerDocumentController _draft = ComposerDocumentController(); ComposerDocumentController get draft => _draft; final _projectDrafts = {}; final _inputs = <(String?, String), ComposerDocumentController>{}; final _composers = <(String?, String), AgentComposerController>{}; ComposerDocumentController input( String? projectId, String? conversationId, { bool projectScoped = false, }) => conversationId == null ? projectScoped ? _projectDrafts.putIfAbsent( projectId, () => projectId == null ? ComposerDocumentController() : _projectDrafts.remove(null) ?? ComposerDocumentController(), ) : draft : _inputs.putIfAbsent(( projectId, conversationId, ), ComposerDocumentController.new); List attachments(String? projectId, String key) => composer(projectId, key).attachments; AgentComposerController composer(String? projectId, String key) => _composers.putIfAbsent((projectId, key), () => createComposer(projectId)); void promoteDraft(String projectId, String draftKey, String conversationId) { final destination = (projectId, conversationId); _inputs.remove(destination)?.dispose(); final scoped = _projectDrafts.remove(projectId); _inputs[destination] = scoped ?? _draft; if (scoped == null) _draft = ComposerDocumentController(); final composer = _composers.remove((projectId, draftKey)); if (composer != null) { _composers.remove(destination)?.dispose(); _composers[destination] = composer; } } void removeConversation(String projectId, String conversationId) { _inputs.remove((projectId, conversationId))?.dispose(); _composers.remove((projectId, conversationId))?.dispose(); } void removeDraft(String? projectId, String draftKey) { _composers.remove((projectId, draftKey))?.dispose(); } void dispose() { draft.dispose(); for (final input in _projectDrafts.values) { input.dispose(); } _projectDrafts.clear(); for (final input in _inputs.values) { input.dispose(); } _inputs.clear(); for (final composer in _composers.values) { composer.dispose(); } _composers.clear(); } }