init method

Future<void> init()

Initializes memory by loading history from persistence.

Implementation

Future<void> init() async {
  if (persistence == null) return;

  try {
    logger.info('Loading chat history from persistence', tag: 'MEMORY');
    final messages = await persistence!.loadMessages();

    for (var msg in messages) {
      final chatMsg = VanturaMessage(
        role: MessageRole.values.byName(msg['role'] as String),
        content: msg['content'] as String?,
        toolCalls: (msg['toolCalls'] as List?)?.cast<Map<String, dynamic>>(),
        toolCallId: msg['toolCallId'] as String?,
        isSummary: (msg['isSummary'] == 1 || msg['isSummary'] == true),
      );

      if (chatMsg.isSummary) {
        _longMemory.add(chatMsg);
      } else {
        _shortMemory.add(chatMsg);
      }
    }

    _cachedMessages = null; // Invalidate cache after loading

    logger.info(
      'Chat history loaded',
      tag: 'MEMORY',
      extra: {
        'short_count': _shortMemory.length,
        'long_count': _longMemory.length,
      },
    );
  } catch (e, stackTrace) {
    logger.error(
      'Failed to load chat history',
      tag: 'MEMORY',
      error: e,
      stackTrace: stackTrace,
    );
  }
}