saveProfile method

Future<void> saveProfile(
  1. LlmConfig profile
)

Saves or creates a named LLM profile.

Implementation

Future<void> saveProfile(LlmConfig profile) async {
  var sanitized = _sanitizeInitialLlmConfig(profile);
  if (sanitized.id == null || sanitized.id!.isEmpty) {
    sanitized = sanitized.copyWith(
      id: 'llm_${DateTime.now().millisecondsSinceEpoch}',
    );
  }
  if (sanitized.name == null || sanitized.name!.isEmpty) {
    sanitized = sanitized.copyWith(name: sanitized.displayName);
  }

  final targetId = sanitized.profileId;
  final idx = _llmConfigs.indexWhere(
    (c) =>
        (sanitized.id != null && c.id == sanitized.id) ||
        c.profileId == targetId,
  );

  if (idx != -1) {
    _llmConfigs[idx] = sanitized;
  } else {
    _llmConfigs.add(sanitized);
  }

  if (_selectedLlmId == targetId ||
      _selectedLlmId.isEmpty ||
      _llmConfigs.length == 1) {
    _selectedLlmId = targetId;
    _llmConfig = sanitized;
  }

  await _storage.saveLlmConfigs(_llmConfigs, selectedId: _selectedLlmId);
  await _storage.saveLlmConfig(_llmConfig);
  notifyListeners();
  unawaited(fetchActiveModelPrice());
}