using System; using System.Threading.Tasks; using Newtonsoft.Json.Linq; using SwarmUI.Accounts; namespace Mrleo1nid.SwarmAssistent; /// CRUD for About-the-user preferences (global + per-persona). public partial class SwarmAssistentExtension { public async Task AssistentListUserPrefs(Session session, string scope = null, string persona = null, int limit = 200) { await Task.CompletedTask; if (Memory is null) { return new JObject { ["error"] = "memory not ready" }; } try { string pid = AssistentConfig.SafeId(persona) ?? Config?.DefaultPersonaId() ?? "neutral"; JArray rows = Memory.ListUserPrefs(scope, pid, limit); return new JObject { ["success"] = true, ["prefs"] = rows, ["total"] = Memory.CountUserPrefs(), ["persona"] = pid, }; } catch (Exception ex) { return new JObject { ["error"] = $"user prefs list: {ex.Message}" }; } } public async Task AssistentUpsertUserPref(Session session, string key, string text, string scope = "global", string persona = null, string source = "user", bool pinned = false) { await Task.CompletedTask; if (Memory is null) { return new JObject { ["error"] = "memory not ready" }; } try { string pid = AssistentConfig.SafeId(persona) ?? Config?.DefaultPersonaId() ?? "neutral"; JObject row = Memory.UpsertUserPref(key, text, scope, pid, source, pinned); return new JObject { ["success"] = true, ["pref"] = row }; } catch (Exception ex) { return new JObject { ["error"] = $"user pref upsert: {ex.Message}" }; } } public async Task AssistentForgetUserPref(Session session, string key, string scope = "global", string persona = null) { await Task.CompletedTask; if (Memory is null) { return new JObject { ["error"] = "memory not ready" }; } if (string.IsNullOrWhiteSpace(key)) { return new JObject { ["error"] = "key required" }; } try { string pid = AssistentConfig.SafeId(persona) ?? Config?.DefaultPersonaId() ?? "neutral"; bool ok = Memory.ForgetUserPref(key, scope, pid); return new JObject { ["success"] = ok, ["key"] = key.Trim(), ["scope"] = scope }; } catch (Exception ex) { return new JObject { ["error"] = $"user pref forget: {ex.Message}" }; } } public async Task AssistentClearUserPrefs(Session session, string scope = "all", string persona = null) { await Task.CompletedTask; if (Memory is null) { return new JObject { ["error"] = "memory not ready" }; } try { string pid = AssistentConfig.SafeId(persona) ?? Config?.DefaultPersonaId() ?? "neutral"; int n = Memory.ClearUserPrefs(scope, pid); return new JObject { ["success"] = true, ["deleted"] = n, ["scope"] = scope ?? "all" }; } catch (Exception ex) { return new JObject { ["error"] = $"user prefs clear: {ex.Message}" }; } } }