buildContext function

ConversationContext buildContext(
  1. List<String> userMessages
)

Build context from conversation history.

Implementation

ConversationContext buildContext(List<String> userMessages) {
  final files = <String>{};
  final urls = <String>{};
  final commands = <String>{};
  String? lastLanguage;
  String? lastTopic;

  for (final msg in userMessages) {
    // Parse each message for mentions.
    final parsed = parseUserInput(msg);
    files.addAll(parsed.mentionedFiles);
    urls.addAll(parsed.mentionedUrls);

    if (parsed.isCommand && parsed.commandName != null) {
      commands.add(parsed.commandName!);
    }

    // Detect language from message.
    final lang = detectLanguage(msg);
    if (lang != null) lastLanguage = lang;

    // Extract a rough topic from the last substantive message.
    final trimmed = msg.trim();
    if (trimmed.length > 10 && !trimmed.startsWith('/')) {
      lastTopic = _extractTopic(trimmed);
    }
  }

  return ConversationContext(
    mentionedFiles: files,
    mentionedUrls: urls,
    mentionedCommands: commands,
    currentTopic: lastTopic,
    currentLanguage: lastLanguage,
  );
}