Add Knowledge Hub books FTS and remove training UI (0.16.0).
Index Assistent/books search.jsonl, expose knowledge hops/catalog in chat, and drop QLoRA/HF training stack. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SwarmUI.Accounts;
|
||||
|
||||
namespace Mrleo1nid.SwarmAssistent;
|
||||
|
||||
public partial class SwarmAssistentExtension
|
||||
{
|
||||
public async Task<JObject> AssistentListKnowledgeCatalog(Session session, string persona = null)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
if (Memory is null)
|
||||
{
|
||||
return new JObject { ["error"] = "memory not ready" };
|
||||
}
|
||||
try
|
||||
{
|
||||
Memory.EnsureBooksIndex();
|
||||
string pid = AssistentConfig.SafeId(persona) ?? Config.DefaultPersonaId();
|
||||
return new JObject
|
||||
{
|
||||
["success"] = true,
|
||||
["knowledge"] = BuildKnowledgeCatalog(pid),
|
||||
["editable"] = Config.IsOverlayPersona(pid) && !Config.IsProtectedPersona(pid),
|
||||
};
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
return new JObject { ["error"] = $"knowledge catalog: {ex.Message}" };
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<JObject> AssistentSearchKnowledge(Session session, string query, string persona = null, int limit = 8, string rating = null, string book = null)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
if (Memory is null)
|
||||
{
|
||||
return new JObject { ["error"] = "memory not ready" };
|
||||
}
|
||||
query = (query ?? "").Trim();
|
||||
if (query.Length < 1)
|
||||
{
|
||||
return new JObject { ["error"] = "query required" };
|
||||
}
|
||||
try
|
||||
{
|
||||
string pid = AssistentConfig.SafeId(persona) ?? Config.DefaultPersonaId();
|
||||
JArray results;
|
||||
if (!string.IsNullOrWhiteSpace(book))
|
||||
{
|
||||
results = Memory.LookupBooks(query, limit, [book.Trim()], rating);
|
||||
}
|
||||
else
|
||||
{
|
||||
results = SearchKnowledge(pid, query, limit, rating);
|
||||
}
|
||||
return new JObject
|
||||
{
|
||||
["success"] = true,
|
||||
["query"] = query,
|
||||
["persona"] = pid,
|
||||
["results"] = results,
|
||||
["civitai_results"] = CivitaiResultsShim(results),
|
||||
};
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
return new JObject { ["error"] = $"knowledge search: {ex.Message}" };
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<JObject> AssistentGetKnowledgeAttach(Session session, string persona = null)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
string pid = AssistentConfig.SafeId(persona) ?? Config.DefaultPersonaId();
|
||||
return new JObject
|
||||
{
|
||||
["success"] = true,
|
||||
["persona"] = pid,
|
||||
["attach"] = new JArray(ResolveAttachedBooks(pid)),
|
||||
["editable"] = Config.IsOverlayPersona(pid) && !Config.IsProtectedPersona(pid),
|
||||
["source"] = Config.PersonaSource(pid),
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<JObject> AssistentSaveKnowledgeAttach(Session session, string persona, JArray attach)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
string pid = AssistentConfig.SafeId(persona);
|
||||
if (pid is null)
|
||||
{
|
||||
return new JObject { ["error"] = "persona required" };
|
||||
}
|
||||
if (Config.IsProtectedPersona(pid))
|
||||
{
|
||||
return new JObject { ["error"] = "bundled/pack personas cannot edit attach here — clone to overlay first" };
|
||||
}
|
||||
if (!Config.IsOverlayPersona(pid))
|
||||
{
|
||||
return new JObject { ["error"] = "overlay persona required" };
|
||||
}
|
||||
try
|
||||
{
|
||||
JArray cleaned = [];
|
||||
if (attach is not null)
|
||||
{
|
||||
foreach (JToken t in attach)
|
||||
{
|
||||
string id = AssistentConfig.SafeId(t?.ToString());
|
||||
if (id is not null)
|
||||
{
|
||||
cleaned.Add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Config.SaveKnowledgeAttachOverlay(pid, cleaned);
|
||||
return new JObject
|
||||
{
|
||||
["success"] = true,
|
||||
["persona"] = pid,
|
||||
["attach"] = new JArray(ResolveAttachedBooks(pid)),
|
||||
};
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
return new JObject { ["error"] = $"save knowledge attach: {ex.Message}" };
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user