fix: send_to_session für alle ExecuteTool-Pfade + generelle FixOrphanedToolUse

- AnthropicService: db/chatService/anthropicService/userId/telegramService auch im
  RunAgentChat (Zeile 899) und ChatNonStreaming (Zeile 1105) ExecuteTool-Aufruf
  durchreichen – Telegram-Flow nutzt ChatNonStreaming, hatte zuvor null-Parameter
- ChatService: FixOrphanedAskUserChoice → FixOrphanedToolUse ersetzt
  Prüft gesamte History auf orphaned tool_use-Blöcke (nicht nur ask_user_choice am Ende)
  Synthetische tool_results werden eingefügt um Anthropic API 400 zu verhindern
- ToolService: Debug-Logging für send_to_session nach /tmp/gott_send_to_session.log
This commit is contained in:
Meleki committed 2026-03-11 13:50:12 +01:00
1 parent 2486eee047
commit e374c70c89
3 files changed
+98 -40

No files matched your search

+16
View File
@@ -729,10 +729,19 @@ public class ToolService
}
case "send_to_session":
{
var debugLog = "/tmp/gott_send_to_session.log";
void Log(string msg) { try { File.AppendAllText(debugLog, $"[{DateTime.Now:HH:mm:ss}] {msg}\n"); } catch {} }
Log($"Tool aufgerufen – sessionId={sessionId}, agentId={agentId}, userId={userId}");
Log($"db={db != null}, chatService={chatService != null}, anthropicService={anthropicService != null}, telegramService={telegramService != null}");
if (db == null || chatService == null || anthropicService == null)
{
Log("ABBRUCH: db/chatService/anthropicService ist null");
return "Fehler: send_to_session nicht verfügbar";
}
var sessionIdOrName = input.GetProperty("session_id_or_name").GetString() ?? "";
var message = input.GetProperty("message").GetString() ?? "";
Log($"sessionIdOrName={sessionIdOrName}, message={message}");
if (string.IsNullOrEmpty(sessionIdOrName))
return "Fehler: session_id_or_name fehlt";
if (string.IsNullOrEmpty(message))
@@ -740,8 +749,10 @@ public class ToolService
// Ziel-Session per ID oder Name suchen (instanzweit, kein User-Check)
var targetSession = await db.GetSession(sessionIdOrName);
Log($"GetSession={targetSession?.SessionId ?? "null"}");
if (targetSession == null)
targetSession = await db.GetSessionByName(sessionIdOrName);
Log($"GetSessionByName={targetSession?.SessionId ?? "null"}");
if (targetSession == null)
return $"Fehler: Session '{sessionIdOrName}' nicht gefunden";
@@ -751,12 +762,14 @@ public class ToolService
// Context für Ziel-Session vorbereiten (analog zu CronjobService)
var ctx = await chatService.PrepareContext(targetSessionId, userId: null);
Log($"PrepareContext={ctx != null}");
if (ctx == null)
return $"Fehler: Session '{targetSession.Name}' ist nicht konfiguriert (kein Agent zugewiesen?)";
// History aufbauen – User-Message NICHT in DB speichern (wie Cronjob)
// Stattdessen als kontextualisierte Nachricht in die History injizieren
var history = await chatService.BuildHistoryFromDb(targetSessionId, promptBlocks: ctx.PromptBlocks);
Log($"History geladen, {history.Count} Einträge");
var senderAgent = agentId != null ? await db.GetAgent(agentId) : null;
var senderName = senderAgent?.Name ?? agentId ?? "Unbekannter Agent";
var senderInfo = $"[Nachricht von Agent \"{senderName}\"]: {message}";
@@ -765,10 +778,13 @@ public class ToolService
try
{
Log("Rufe ChatNonStreaming auf...");
await anthropicService.ChatNonStreaming(ctx, history);
Log("ChatNonStreaming erfolgreich");
}
catch (Exception ex)
{
Log($"ChatNonStreaming FEHLER: {ex}");
Console.WriteLine($"[send_to_session] ChatNonStreaming Fehler: {ex}");
return $"Fehler beim Senden an Session \"{targetSession.Name}\": {ex.Message}";
}