AuthorBuddy/Services/SettingsService.cs

334 lines
12 KiB
C#
Raw Normal View History

2026-05-16 16:13:35 +02:00
using AuthorBuddy.Web.Data;
using AuthorBuddy.Web.Models;
2026-05-20 06:12:54 +02:00
using LiteDB;
2026-05-16 16:13:35 +02:00
namespace AuthorBuddy.Web.Services;
public class SettingsService : ISettingsService
{
private const string KeyOllamaUrl = "ollama_url";
private const string KeyRagModel = "ollama_rag_model";
private const string KeyStyleModel = "ollama_style_model";
private const string KeySpellingModel = "ollama_spelling_model";
private const string KeyTheme = "theme";
private const string KeyLektorPrompt = "ollama_lektor_prompt";
private const string KeyFactCheckTemplate = "ollama_fact_check";
private const string KeySpellingPrompt = "ollama_spelling_prompt";
2026-05-20 06:12:54 +02:00
private const string KeyAdvocatusDiaboli = "ollama_advocatus_diaboli";
private const string KeyHookFinder = "ollama_hook_finder";
private const string KeyEli5 = "ollama_eli5";
2026-05-16 16:13:35 +02:00
2026-05-20 06:12:54 +02:00
private readonly ILiteDatabase _db;
2026-05-16 16:13:35 +02:00
private readonly OllamaSettings _ollamaConfig;
2026-05-17 07:27:18 +02:00
private int? _currentUserId;
private int? _currentProjectId;
2026-05-16 16:13:35 +02:00
2026-05-20 06:12:54 +02:00
public SettingsService(ILiteDatabase db, OllamaSettings ollamaConfig)
2026-05-16 16:13:35 +02:00
{
2026-05-20 06:12:54 +02:00
_db = db;
2026-05-16 16:13:35 +02:00
_ollamaConfig = ollamaConfig;
}
2026-05-17 07:27:18 +02:00
public int? CurrentProjectId
2026-05-16 16:13:35 +02:00
{
2026-05-17 07:27:18 +02:00
get => _currentProjectId;
set => _currentProjectId = value;
2026-05-16 16:13:35 +02:00
}
2026-05-20 06:12:54 +02:00
public string? CurrentFilePath { get; set; }
private const string KeyLastProjectId = "last_project_id";
public Task<int?> GetLastProjectIdAsync()
{
var col = _db.GetCollection<SettingEntity>("settings");
var setting = col.FindOne(s => s.Key == KeyLastProjectId && s.UserId == _currentUserId && s.ProjectId == null);
if (setting != null && int.TryParse(setting.Value, out var id))
return Task.FromResult<int?>(id);
return Task.FromResult<int?>(null);
}
public Task SaveLastProjectIdAsync(int projectId)
{
var col = _db.GetCollection<SettingEntity>("settings");
var existing = col.FindOne(s => s.Key == KeyLastProjectId && s.UserId == _currentUserId && s.ProjectId == null);
if (existing != null)
{
existing.Value = projectId.ToString();
col.Update(existing);
}
else
{
col.Insert(new SettingEntity
{
Key = KeyLastProjectId,
Value = projectId.ToString(),
UserId = _currentUserId,
ProjectId = null
});
}
return Task.CompletedTask;
}
private string? GetSetting(string key, int? userId, int? projectId)
2026-05-17 07:27:18 +02:00
{
2026-05-20 06:12:54 +02:00
var col = _db.GetCollection<SettingEntity>("settings");
2026-05-16 16:13:35 +02:00
2026-05-20 06:12:54 +02:00
var matches = col.Find(s =>
2026-05-17 07:27:18 +02:00
s.Key == key &&
(s.UserId == userId || s.UserId == null) &&
(s.ProjectId == projectId || s.ProjectId == null));
2026-05-16 16:13:35 +02:00
2026-05-20 06:12:54 +02:00
return matches
2026-05-17 07:27:18 +02:00
.OrderByDescending(s => s.UserId != null)
.ThenByDescending(s => s.ProjectId != null)
2026-05-20 06:12:54 +02:00
.FirstOrDefault()?.Value;
2026-05-17 07:27:18 +02:00
}
2026-05-20 06:12:54 +02:00
private string? Get(string key)
2026-05-17 07:27:18 +02:00
{
2026-05-20 06:12:54 +02:00
return GetSetting(key, _currentUserId, null);
2026-05-17 07:27:18 +02:00
}
2026-05-16 16:13:35 +02:00
2026-05-20 06:12:54 +02:00
private void Set(string key, string value)
2026-05-16 16:13:35 +02:00
{
2026-05-20 06:12:54 +02:00
var col = _db.GetCollection<SettingEntity>("settings");
2026-05-17 07:27:18 +02:00
2026-05-20 06:12:54 +02:00
var existing = col.FindOne(s =>
s.Key == key && s.UserId == _currentUserId && s.ProjectId == null);
2026-05-17 07:27:18 +02:00
2026-05-16 16:13:35 +02:00
if (existing != null)
2026-05-17 07:27:18 +02:00
{
2026-05-16 16:13:35 +02:00
existing.Value = value;
2026-05-20 06:12:54 +02:00
col.Update(existing);
2026-05-17 07:27:18 +02:00
}
2026-05-16 16:13:35 +02:00
else
2026-05-17 07:27:18 +02:00
{
2026-05-20 06:12:54 +02:00
col.Insert(new SettingEntity
2026-05-17 07:27:18 +02:00
{
Key = key,
Value = value,
UserId = _currentUserId,
2026-05-20 06:12:54 +02:00
ProjectId = null
2026-05-17 07:27:18 +02:00
});
}
2026-05-16 16:13:35 +02:00
}
2026-05-20 06:12:54 +02:00
public Task<string?> GetOllamaUrlAsync() => Task.FromResult(Get(KeyOllamaUrl));
public Task<string?> GetRagModelAsync() => Task.FromResult(Get(KeyRagModel));
public Task<string?> GetStyleModelAsync() => Task.FromResult(Get(KeyStyleModel));
public Task<string?> GetLektorSystemPromptAsync() => Task.FromResult(Get(KeyLektorPrompt));
public Task<string?> GetFactCheckTemplateAsync() => Task.FromResult(Get(KeyFactCheckTemplate));
public Task<string?> GetSpellingPromptAsync() => Task.FromResult(Get(KeySpellingPrompt));
public Task<string?> GetSpellingModelAsync() => Task.FromResult(Get(KeySpellingModel));
public Task<string?> GetAdvocatusDiaboliPromptAsync() => Task.FromResult(Get(KeyAdvocatusDiaboli));
public Task<string?> GetHookFinderPromptAsync() => Task.FromResult(Get(KeyHookFinder));
public Task<string?> GetEli5PromptAsync() => Task.FromResult(Get(KeyEli5));
public Task<string?> GetThemeAsync() => Task.FromResult(Get(KeyTheme));
public Task SaveOllamaUrlAsync(string url)
2026-05-16 16:13:35 +02:00
{
2026-05-20 06:12:54 +02:00
Set(KeyOllamaUrl, url);
2026-05-16 16:13:35 +02:00
_ollamaConfig.OllamaUrl = url;
2026-05-20 06:12:54 +02:00
return Task.CompletedTask;
2026-05-16 16:13:35 +02:00
}
2026-05-20 06:12:54 +02:00
public Task SaveRagModelAsync(string model)
2026-05-16 16:13:35 +02:00
{
2026-05-20 06:12:54 +02:00
Set(KeyRagModel, model);
_ollamaConfig.EmbedModel = model;
return Task.CompletedTask;
2026-05-16 16:13:35 +02:00
}
2026-05-20 06:12:54 +02:00
public Task SaveStyleModelAsync(string model)
2026-05-16 16:13:35 +02:00
{
2026-05-20 06:12:54 +02:00
Set(KeyStyleModel, model);
2026-05-16 16:13:35 +02:00
_ollamaConfig.StyleModel = model;
2026-05-20 06:12:54 +02:00
return Task.CompletedTask;
2026-05-16 16:13:35 +02:00
}
2026-05-20 06:12:54 +02:00
public Task SaveLektorSystemPromptAsync(string prompt)
2026-05-16 16:13:35 +02:00
{
2026-05-20 06:12:54 +02:00
Set(KeyLektorPrompt, prompt);
2026-05-16 16:13:35 +02:00
_ollamaConfig.LektorSystemPrompt = prompt;
2026-05-20 06:12:54 +02:00
return Task.CompletedTask;
2026-05-16 16:13:35 +02:00
}
2026-05-20 06:12:54 +02:00
public Task SaveFactCheckTemplateAsync(string prompt)
2026-05-16 16:13:35 +02:00
{
2026-05-20 06:12:54 +02:00
Set(KeyFactCheckTemplate, prompt);
2026-05-16 16:13:35 +02:00
_ollamaConfig.FactCheckTemplate = prompt;
2026-05-20 06:12:54 +02:00
return Task.CompletedTask;
2026-05-16 16:13:35 +02:00
}
2026-05-17 07:27:18 +02:00
2026-05-20 06:12:54 +02:00
public Task SaveSpellingPromptAsync(string prompt)
2026-05-16 16:13:35 +02:00
{
2026-05-20 06:12:54 +02:00
Set(KeySpellingPrompt, prompt);
2026-05-16 16:13:35 +02:00
_ollamaConfig.SpellingPrompt = prompt;
2026-05-20 06:12:54 +02:00
return Task.CompletedTask;
2026-05-16 16:13:35 +02:00
}
2026-05-20 06:12:54 +02:00
public Task SaveSpellingModelAsync(string model)
2026-05-16 16:13:35 +02:00
{
2026-05-20 06:12:54 +02:00
Set(KeySpellingModel, model);
2026-05-16 16:13:35 +02:00
_ollamaConfig.SpellingModel = model;
2026-05-20 06:12:54 +02:00
return Task.CompletedTask;
}
public Task SaveAdvocatusDiaboliPromptAsync(string prompt)
{
Set(KeyAdvocatusDiaboli, prompt);
_ollamaConfig.AdvocatusDiaboliPrompt = prompt;
return Task.CompletedTask;
}
public Task SaveHookFinderPromptAsync(string prompt)
{
Set(KeyHookFinder, prompt);
_ollamaConfig.HookFinderPrompt = prompt;
return Task.CompletedTask;
}
public Task SaveEli5PromptAsync(string prompt)
{
Set(KeyEli5, prompt);
_ollamaConfig.Eli5Prompt = prompt;
return Task.CompletedTask;
2026-05-16 16:13:35 +02:00
}
public Task SaveThemeAsync(string theme)
2026-05-20 06:12:54 +02:00
{
Set(KeyTheme, theme);
return Task.CompletedTask;
}
2026-05-16 16:13:35 +02:00
2026-05-20 06:12:54 +02:00
public Task<List<UserEntity>> GetUsersAsync()
2026-05-17 07:27:18 +02:00
{
2026-05-20 06:12:54 +02:00
var col = _db.GetCollection<UserEntity>("users");
return Task.FromResult(col.FindAll().ToList());
2026-05-17 07:27:18 +02:00
}
2026-05-20 06:12:54 +02:00
public Task<UserEntity> GetCurrentUserAsync()
2026-05-17 07:27:18 +02:00
{
2026-05-20 06:12:54 +02:00
var col = _db.GetCollection<UserEntity>("users");
var user = col.FindOne(u => u.IsCurrent);
return Task.FromResult(user ?? col.FindAll().First());
2026-05-17 07:27:18 +02:00
}
2026-05-20 06:12:54 +02:00
public Task SetCurrentUserAsync(int userId)
2026-05-17 07:27:18 +02:00
{
2026-05-20 06:12:54 +02:00
var col = _db.GetCollection<UserEntity>("users");
var users = col.FindAll().ToList();
2026-05-17 07:27:18 +02:00
foreach (var u in users)
2026-05-20 06:12:54 +02:00
{
2026-05-17 07:27:18 +02:00
u.IsCurrent = u.Id == userId;
2026-05-20 06:12:54 +02:00
col.Update(u);
}
2026-05-17 07:27:18 +02:00
_currentUserId = userId;
2026-05-20 06:12:54 +02:00
return Task.CompletedTask;
2026-05-17 07:27:18 +02:00
}
2026-05-20 06:12:54 +02:00
public Task<UserEntity> CreateUserAsync(string name)
2026-05-17 07:27:18 +02:00
{
2026-05-20 06:12:54 +02:00
var col = _db.GetCollection<UserEntity>("users");
2026-05-17 07:27:18 +02:00
var user = new UserEntity { Name = name };
2026-05-20 06:12:54 +02:00
col.Insert(user);
return Task.FromResult(user);
}
public Task LoadProjectSettingsAsync()
{
var settings = _db.GetCollection<SettingEntity>("settings");
var entries = new (string Key, Action<string> Setter)[]
{
(KeyOllamaUrl, val => _ollamaConfig.OllamaUrl = val),
(KeyRagModel, val => _ollamaConfig.EmbedModel = val),
(KeyStyleModel, val => _ollamaConfig.StyleModel = val),
(KeySpellingModel, val => _ollamaConfig.SpellingModel = val),
(KeyTheme, val => _ollamaConfig.Theme = val),
(KeyLektorPrompt, val => _ollamaConfig.LektorSystemPrompt = val),
(KeyFactCheckTemplate, val => _ollamaConfig.FactCheckTemplate = val),
(KeySpellingPrompt, val => _ollamaConfig.SpellingPrompt = val),
(KeyAdvocatusDiaboli, val => _ollamaConfig.AdvocatusDiaboliPrompt = val),
(KeyHookFinder, val => _ollamaConfig.HookFinderPrompt = val),
(KeyEli5, val => _ollamaConfig.Eli5Prompt = val),
};
foreach (var entry in entries)
{
var val = Get(entry.Key);
if (val != null) entry.Setter(val);
}
return Task.CompletedTask;
2026-05-17 07:27:18 +02:00
}
2026-05-20 06:12:54 +02:00
public Task InitializeAsync(int projectId = -1)
2026-05-16 16:13:35 +02:00
{
2026-05-20 06:12:54 +02:00
var users = _db.GetCollection<UserEntity>("users");
var settings = _db.GetCollection<SettingEntity>("settings");
2026-05-16 16:13:35 +02:00
2026-05-20 06:12:54 +02:00
var userExists = users.FindAll().Any();
2026-05-17 07:27:18 +02:00
if (!userExists)
{
var defaultUser = new UserEntity { Name = "Standardbenutzer", IsCurrent = true };
2026-05-20 06:12:54 +02:00
users.Insert(defaultUser);
2026-05-17 07:27:18 +02:00
_currentUserId = defaultUser.Id;
}
else
{
2026-05-20 06:12:54 +02:00
var current = users.FindOne(u => u.IsCurrent);
_currentUserId = current?.Id ?? users.FindAll().First().Id;
2026-05-17 07:27:18 +02:00
}
2026-05-16 16:13:35 +02:00
var entries = new (string Key, string Default)[]
{
(KeyOllamaUrl, _ollamaConfig.OllamaUrl),
(KeyRagModel, _ollamaConfig.StyleModel),
(KeyStyleModel, _ollamaConfig.StyleModel),
(KeySpellingModel, _ollamaConfig.SpellingModel),
(KeyTheme, _ollamaConfig.Theme),
(KeyLektorPrompt, _ollamaConfig.LektorSystemPrompt),
(KeyFactCheckTemplate, _ollamaConfig.FactCheckTemplate),
(KeySpellingPrompt, _ollamaConfig.SpellingPrompt),
2026-05-20 06:12:54 +02:00
(KeyAdvocatusDiaboli, _ollamaConfig.AdvocatusDiaboliPrompt),
(KeyHookFinder, _ollamaConfig.HookFinderPrompt),
(KeyEli5, _ollamaConfig.Eli5Prompt),
2026-05-16 16:13:35 +02:00
};
foreach (var entry in entries)
{
2026-05-20 06:12:54 +02:00
var existing = settings.FindOne(s =>
s.Key == entry.Key && s.UserId == _currentUserId && s.ProjectId == projectId);
2026-05-17 07:27:18 +02:00
2026-05-16 16:13:35 +02:00
if (existing != null)
{
var val = existing.Value;
if (entry.Key == KeyOllamaUrl) _ollamaConfig.OllamaUrl = val;
2026-05-20 06:12:54 +02:00
else if (entry.Key == KeyRagModel) _ollamaConfig.EmbedModel = val;
2026-05-16 16:13:35 +02:00
else if (entry.Key == KeyStyleModel) _ollamaConfig.StyleModel = val;
else if (entry.Key == KeySpellingModel) _ollamaConfig.SpellingModel = val;
else if (entry.Key == KeyTheme) _ollamaConfig.Theme = val;
else if (entry.Key == KeyLektorPrompt) _ollamaConfig.LektorSystemPrompt = val;
else if (entry.Key == KeyFactCheckTemplate) _ollamaConfig.FactCheckTemplate = val;
else if (entry.Key == KeySpellingPrompt) _ollamaConfig.SpellingPrompt = val;
2026-05-20 06:12:54 +02:00
else if (entry.Key == KeyAdvocatusDiaboli) _ollamaConfig.AdvocatusDiaboliPrompt = val;
else if (entry.Key == KeyHookFinder) _ollamaConfig.HookFinderPrompt = val;
else if (entry.Key == KeyEli5) _ollamaConfig.Eli5Prompt = val;
2026-05-16 16:13:35 +02:00
}
else
{
2026-05-20 06:12:54 +02:00
settings.Insert(new SettingEntity
2026-05-17 07:27:18 +02:00
{
Key = entry.Key,
Value = entry.Default,
UserId = _currentUserId,
ProjectId = null
});
2026-05-16 16:13:35 +02:00
}
}
2026-05-20 06:12:54 +02:00
return Task.CompletedTask;
2026-05-16 16:13:35 +02:00
}
}