Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/core/providers/memory_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import '../services/memory_store.dart';
class MemoryProvider extends ChangeNotifier {
List<AssistantMemory> _memories = <AssistantMemory>[];
bool _initialized = false;
bool _loading = false;

MemoryProvider() {
// Start loading memories asynchronously on creation
_loadAllSilent();
}

List<AssistantMemory> get memories => List.unmodifiable(_memories);

Expand All @@ -13,21 +19,30 @@ class MemoryProvider extends ChangeNotifier {

Future<void> initialize() async {
if (_initialized) return;
await loadAll();
_initialized = true;
await _loadAllSilent();
}

Future<void> loadAll() async {
Future<void> _loadAllSilent() async {
if (_loading) return;
_loading = true;
try {
_memories = await MemoryStore.getAll();
_initialized = true;
notifyListeners();
} catch (e) {
debugPrint('Failed to load memories: $e');
_memories = <AssistantMemory>[];
_initialized = true; // Still mark as initialized to prevent infinite retries
notifyListeners();
} finally {
_loading = false;
}
}

Future<void> loadAll() async {
await _loadAllSilent();
if (_loading) return;
}
Future<AssistantMemory> add({
required String assistantId,
required String content,
Expand Down
5 changes: 3 additions & 2 deletions lib/features/home/services/message_builder_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,9 @@ class MessageBuilderService {
}) async {
try {
if (assistant?.enableMemory == true) {
final mp = contextProvider.read<MemoryProvider>();
final mems = mp.getForAssistant(assistant!.id);
final mp = contextProvider.read<MemoryProvider>();
await mp.initialize();
final mems = mp.getForAssistant(assistant!.id);
final buf = StringBuffer();
buf.writeln('## Memories');
buf.writeln(
Expand Down
1 change: 1 addition & 0 deletions lib/features/home/services/tool_handler_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ class ToolHandlerService {

try {
final mp = contextProvider.read<MemoryProvider>();
await mp.initialize();

if (name == 'create_memory') {
final content = (args['content'] ?? '').toString();
Expand Down