diff --git a/Endpoints/AgentEndpoint.cs b/Endpoints/AgentEndpoint.cs
index ff29be6..2d56646 100644
--- a/Endpoints/AgentEndpoint.cs
+++ b/Endpoints/AgentEndpoint.cs
@@ -64,6 +64,7 @@ public static class AgentEndpoint
a.UseIdentity,
a.UseUserInfo,
a.UseMemory,
+ a.UseAutoContext,
a.UseWebSearch,
a.WebSearchVersion,
a.AllowUserChoice,
@@ -119,6 +120,7 @@ public static class AgentEndpoint
UseIdentity = !body.TryGetProperty("useIdentity", out var ui) || ui.GetBoolean(),
UseUserInfo = !body.TryGetProperty("useUserInfo", out var uu) || uu.GetBoolean(),
UseMemory = !body.TryGetProperty("useMemory", out var um) || um.GetBoolean(),
+ UseAutoContext = !body.TryGetProperty("useAutoContext", out var uac) || uac.GetBoolean(),
UseWebSearch = body.TryGetProperty("webSearchVersion", out var wsv) && !string.IsNullOrEmpty(wsv.GetString()),
WebSearchVersion = body.TryGetProperty("webSearchVersion", out var wsv2) ? wsv2.GetString() ?? "" : "",
AllowUserChoice = !body.TryGetProperty("allowUserChoice", out var auc) || auc.GetBoolean(),
@@ -160,6 +162,7 @@ public static class AgentEndpoint
if (body.TryGetProperty("useIdentity", out var ui)) agent.UseIdentity = ui.GetBoolean();
if (body.TryGetProperty("useUserInfo", out var uu)) agent.UseUserInfo = uu.GetBoolean();
if (body.TryGetProperty("useMemory", out var um)) agent.UseMemory = um.GetBoolean();
+ if (body.TryGetProperty("useAutoContext", out var uac)) agent.UseAutoContext = uac.GetBoolean();
if (body.TryGetProperty("webSearchVersion", out var wsv))
{
agent.WebSearchVersion = wsv.GetString() ?? "";
diff --git a/Endpoints/ChatEndpoint.cs b/Endpoints/ChatEndpoint.cs
index 4a9597c..a2d3f70 100644
--- a/Endpoints/ChatEndpoint.cs
+++ b/Endpoints/ChatEndpoint.cs
@@ -208,7 +208,7 @@ public static class ChatEndpoint
try
{
- await anthropic.StreamChat(res, ctx.ModelId, ctx.SystemPrompt, messages, ctx.ChainId, ctx.Workspace, sessionId, agentId: ctx.AgentId, useMemory: ctx.UseMemory, webSearchVersion: ctx.WebSearchVersion, maxIterations: ctx.MaxIterations, toolChoice: effectiveToolChoice, review: ctx.Review, finalizer: ctx.Finalizer, allowUserChoice: ctx.Agent?.AllowUserChoice ?? true, planMode: ctx.PlanMode, workCt: workCt, hardCt: hardCt, sseCt: sseCt, remoteMgr: remoteMgr);
+ await anthropic.StreamChat(res, ctx.ModelId, ctx.SystemPrompt, messages, ctx.ChainId, ctx.Workspace, sessionId, agentId: ctx.AgentId, useMemory: ctx.UseMemory, webSearchVersion: ctx.WebSearchVersion, maxIterations: ctx.MaxIterations, toolChoice: effectiveToolChoice, review: ctx.Review, finalizer: ctx.Finalizer, allowUserChoice: ctx.Agent?.AllowUserChoice ?? true, planMode: ctx.PlanMode, useAutoContext: ctx.Agent?.UseAutoContext ?? true, workCt: workCt, hardCt: hardCt, sseCt: sseCt, remoteMgr: remoteMgr);
}
finally
{
diff --git a/Models/AgentRecord.cs b/Models/AgentRecord.cs
index 2d312b7..f4e6e33 100644
--- a/Models/AgentRecord.cs
+++ b/Models/AgentRecord.cs
@@ -16,6 +16,7 @@ public class AgentRecord
public bool UseIdentity { get; set; } = true;
public bool UseUserInfo { get; set; } = true;
public bool UseMemory { get; set; } = true;
+ public bool UseAutoContext { get; set; } = true;
public bool UseWebSearch { get; set; }
public string WebSearchVersion { get; set; } = "";
public bool AllowUserChoice { get; set; } = true;
diff --git a/Services/AnthropicService.cs b/Services/AnthropicService.cs
index 332d9f0..fdc2f2e 100644
--- a/Services/AnthropicService.cs
+++ b/Services/AnthropicService.cs
@@ -55,6 +55,7 @@ public class AnthropicService
SubAgentContext finalizer = null,
bool allowUserChoice = true,
bool planMode = false,
+ bool useAutoContext = true,
CancellationToken workCt = default,
CancellationToken hardCt = default,
CancellationToken sseCt = default,
@@ -76,6 +77,46 @@ public class AnthropicService
try
{
+ // ── Automatische Kontext-Injektion via Embedding-Suche ──
+ if (useAutoContext && _vectorSearch != null && systemPrompt != null && messages.Count > 0)
+ {
+ try
+ {
+ // Letzte User-Message als Query extrahieren
+ var lastUserText = ExtractLastUserText(messages);
+ if (!string.IsNullOrWhiteSpace(lastUserText))
+ {
+ var contextResults = await _vectorSearch.SearchAsync(lastUserText, agentId, topN: 5, minScore: 0.45f);
+ // Nur Ergebnisse aus anderen Sessions (nicht aktuelle Chain) und nicht zu kurze
+ contextResults.RemoveAll(r => r.ChainId == chainId || (r.TextPreview?.Length ?? 0) < 20);
+ // Memory-Dateien durchsuchen
+ var memoryResults = await _vectorSearch.SearchMemoryFilesAsync(lastUserText, agentId, topN: 3, minScore: 0.45f);
+
+ if (contextResults.Count > 0 || memoryResults.Count > 0)
+ {
+ var ctxSb = new StringBuilder();
+ ctxSb.AppendLine("\n");
+ ctxSb.AppendLine("Die folgende Information wurde automatisch abgerufen und könnte relevant sein:");
+ foreach (var r in contextResults)
+ {
+ var date = DateTime.TryParse(r.CreatedAt, out var dt) ? dt.ToString("yyyy-MM-dd HH:mm") : r.CreatedAt;
+ ctxSb.AppendLine($"- [Gespräch, {r.Role}, {date}]: {r.TextPreview}");
+ }
+ foreach (var r in memoryResults)
+ {
+ ctxSb.AppendLine($"- [Memory, {r.FilePath}]: {r.TextPreview}");
+ }
+ ctxSb.AppendLine("");
+ systemPrompt.Add(new { type = "text", text = ctxSb.ToString() });
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"[AutoContext] Error: {ex.Message}");
+ }
+ }
+
while (iterations < maxIterations)
{
// Hard-Stop: Sofortiger Abbruch aus while-Loop (Doppel-Klick Stop)
@@ -838,7 +879,7 @@ public class AnthropicService
string toolResult;
bool isError = false;
- try { toolResult = await _toolService.ExecuteTool(toolName, toolInput, agentWorkspace, _memoryService, agentId, workCt, vectorSearch: _vectorSearch); }
+ try { toolResult = await _toolService.ExecuteTool(toolName, toolInput, agentWorkspace, _memoryService, agentId, workCt, vectorSearch: _vectorSearch, embeddingBg: _embeddingBg); }
catch (OperationCanceledException) { throw; }
catch (Exception ex) { toolResult = $"Fehler: {ex.Message}"; isError = true; }
@@ -1044,7 +1085,7 @@ public class AnthropicService
string toolResult;
bool isError = false;
- try { toolResult = await _toolService.ExecuteTool(toolName, toolInput, ctx.Workspace, _memoryService, ctx.AgentId, vectorSearch: _vectorSearch); }
+ try { toolResult = await _toolService.ExecuteTool(toolName, toolInput, ctx.Workspace, _memoryService, ctx.AgentId, vectorSearch: _vectorSearch, embeddingBg: _embeddingBg); }
catch (OperationCanceledException) { throw; }
catch (Exception ex) { toolResult = $"Fehler: {ex.Message}"; isError = true; }
@@ -1345,7 +1386,7 @@ public class AnthropicService
string toolResult;
bool isError = false;
- try { toolResult = await _toolService.ExecuteTool(currentToolName, inputEl, workspace, _memoryService, agentId, workCt, remoteMgr: remoteMgr, sessionId: sessionId, vectorSearch: _vectorSearch); }
+ try { toolResult = await _toolService.ExecuteTool(currentToolName, inputEl, workspace, _memoryService, agentId, workCt, remoteMgr: remoteMgr, sessionId: sessionId, vectorSearch: _vectorSearch, embeddingBg: _embeddingBg); }
catch (OperationCanceledException) { throw; }
catch (Exception ex) { toolResult = $"Fehler: {ex.Message}"; isError = true; }
@@ -1468,6 +1509,35 @@ public class AnthropicService
: result;
}
+ ///
+ /// Extrahiert den Text der letzten User-Message aus der Message-Liste.
+ ///
+ private static string ExtractLastUserText(List