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 = {
        'role': msg['role'],
        'content': msg['content'],
        if (msg['toolCalls'] != null) 'tool_calls': msg['toolCalls'],
        if (msg['toolCallId'] != null) 'tool_call_id': msg['toolCallId'],
      };

      if (msg['isSummary'] == 1 || msg['isSummary'] == true) {
        _longMemory.add(chatMsg);
      } else {
        _shortMemory.add(chatMsg);
      }
    }

    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,
    );
  }
}