fix: send_to_session – fremde Sessions erlauben, Telegram-Notify, Fehler-Logging

This commit is contained in:
Meleki committed 2026-03-11 13:36:38 +01:00
1 parent c18b94cd4e
commit 2486eee047
4 files changed
+53 -15

No files matched your search

+36 -14
View File
@@ -377,7 +377,7 @@ public class ToolService
};
}
public async Task<string> ExecuteTool(string name, JsonElement input, string workspace, MemoryService memoryService = null, string agentId = null, CancellationToken cancellationToken = default, RemoteConnectionManager remoteMgr = null, string sessionId = null, VectorSearchService vectorSearch = null, EmbeddingBackgroundService embeddingBg = null, DatabaseService db = null, ChatService chatService = null, AnthropicService anthropicService = null, string userId = null)
public async Task<string> ExecuteTool(string name, JsonElement input, string workspace, MemoryService memoryService = null, string agentId = null, CancellationToken cancellationToken = default, RemoteConnectionManager remoteMgr = null, string sessionId = null, VectorSearchService vectorSearch = null, EmbeddingBackgroundService embeddingBg = null, DatabaseService db = null, ChatService chatService = null, AnthropicService anthropicService = null, string userId = null, TelegramService telegramService = null)
{
switch (name)
{
@@ -738,27 +738,19 @@ public class ToolService
if (string.IsNullOrEmpty(message))
return "Fehler: message fehlt";
// Ziel-Session per ID oder Name suchen
// Ziel-Session per ID oder Name suchen (instanzweit, kein User-Check)
var targetSession = await db.GetSession(sessionIdOrName);
if (targetSession == null && !string.IsNullOrEmpty(userId))
{
// Fallback: per Name suchen (case-insensitive)
var allSessions = await db.GetSessionsWithStats(userId);
targetSession = allSessions
.FirstOrDefault(s => string.Equals(s.Session.Name, sessionIdOrName, StringComparison.OrdinalIgnoreCase))
?.Session;
}
if (targetSession == null)
targetSession = await db.GetSessionByName(sessionIdOrName);
if (targetSession == null)
return $"Fehler: Session '{sessionIdOrName}' nicht gefunden";
if (!string.IsNullOrEmpty(userId) && targetSession.UserId != userId)
return "Fehler: Zugriff auf diese Session nicht erlaubt";
var targetSessionId = targetSession.SessionId;
if (targetSessionId == sessionId)
return "Fehler: Kann keine Nachricht an die eigene Session senden";
// Context für Ziel-Session vorbereiten (analog zu CronjobService)
var ctx = await chatService.PrepareContext(targetSessionId, targetSession.UserId);
var ctx = await chatService.PrepareContext(targetSessionId, userId: null);
if (ctx == null)
return $"Fehler: Session '{targetSession.Name}' ist nicht konfiguriert (kein Agent zugewiesen?)";
@@ -771,9 +763,39 @@ public class ToolService
var userContent = new List<object> { new { type = "text", text = senderInfo } };
history.Add(new Dictionary<string, object> { ["role"] = "user", ["content"] = userContent });
await anthropicService.ChatNonStreaming(ctx, history);
try
{
await anthropicService.ChatNonStreaming(ctx, history);
}
catch (Exception ex)
{
Console.WriteLine($"[send_to_session] ChatNonStreaming Fehler: {ex}");
return $"Fehler beim Senden an Session \"{targetSession.Name}\": {ex.Message}";
}
await db.UpdateSessionTimestamp(targetSessionId);
// Telegram-Benachrichtigung senden falls Session einen Bot hat
if (telegramService != null)
{
try
{
var updatedSession = await db.GetSession(targetSessionId);
if (updatedSession != null && !string.IsNullOrEmpty(updatedSession.TelegramChatId))
{
var recentMsgs = await db.GetLatestActiveMessages(targetSessionId, 5);
var lastMsg = recentMsgs
.Where(m => m.Role == "assistant" && m.Type == "text")
.LastOrDefault();
if (lastMsg != null)
await telegramService.SendNotification(targetSessionId, lastMsg.Content);
}
}
catch (Exception ex)
{
Console.WriteLine($"[send_to_session] Telegram-Benachrichtigung fehlgeschlagen: {ex.Message}");
}
}
return $"Nachricht wurde erfolgreich an Session \"{targetSession.Name}\" gesendet. Der Agent dort hat geantwortet.";
}
default: