|
5 | 5 | "encoding/json" |
6 | 6 | "fmt" |
7 | 7 | "log" |
| 8 | + "os" |
| 9 | + "path/filepath" |
8 | 10 | "slices" |
9 | 11 | "strings" |
10 | 12 | "sync" |
@@ -76,6 +78,22 @@ const compactSentinel = "\x00compact\x00" |
76 | 78 | // The diff content follows immediately after the sentinel. |
77 | 79 | const reviewSentinel = "\x00review\x00" |
78 | 80 |
|
| 81 | +// debugLog appends a log entry to ~/.ratchet/debug.log when debug mode is active. |
| 82 | +func debugLog(format string, args ...any) { |
| 83 | + home, err := os.UserHomeDir() |
| 84 | + if err != nil { |
| 85 | + return |
| 86 | + } |
| 87 | + logPath := filepath.Join(home, ".ratchet", "debug.log") |
| 88 | + f, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) |
| 89 | + if err != nil { |
| 90 | + return |
| 91 | + } |
| 92 | + defer f.Close() |
| 93 | + logger := log.New(f, "", log.LstdFlags) |
| 94 | + logger.Printf(format, args...) |
| 95 | +} |
| 96 | + |
79 | 97 | // broadcastStream wraps a send-message stream and fans out each event to the broadcaster. |
80 | 98 | type broadcastStream struct { |
81 | 99 | pb.RatchetDaemon_SendMessageServer |
@@ -152,6 +170,12 @@ func (s *Service) handleChat(ctx context.Context, sessionID, userMessage string, |
152 | 170 | Content: userMessage, |
153 | 171 | }) |
154 | 172 |
|
| 173 | + // Debug: log outgoing messages. |
| 174 | + if s.engine.Debug { |
| 175 | + msgJSON, _ := json.Marshal(messages) |
| 176 | + debugLog("[chat] session=%s sending %d messages: %s", sessionID, len(messages), string(msgJSON)) |
| 177 | + } |
| 178 | + |
155 | 179 | // Stream from provider (save user message AFTER successful stream start, |
156 | 180 | // so failed requests don't pollute conversation history). |
157 | 181 | eventCh, err := prov.Stream(ctx, messages, nil) // tools will be added in later task |
@@ -271,6 +295,11 @@ func (s *Service) handleChat(ctx context.Context, sessionID, userMessage string, |
271 | 295 | } |
272 | 296 | } |
273 | 297 |
|
| 298 | + // Debug: log response content. |
| 299 | + if s.engine.Debug { |
| 300 | + debugLog("[chat] session=%s response (%d chars): %s", sessionID, len(fullResponse), fullResponse) |
| 301 | + } |
| 302 | + |
274 | 303 | // Save assistant response |
275 | 304 | if err := s.saveMessage(ctx, sessionID, "assistant", fullResponse, "", ""); err != nil { |
276 | 305 | log.Printf("save assistant message: %v", err) |
|
0 commit comments