Skip to content

Commit df91dd8

Browse files
committed
Add temporary debug logging for proxy requests and responses
1 parent 7f77466 commit df91dd8

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

internal/proxy/proxy.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ import (
1919

2020
const defaultBaseURL = "https://api.commandcode.ai"
2121
const defaultTimeout = 300 * time.Second
22+
const debugLogLimit = 20000
23+
24+
func truncateLog(s string) string {
25+
if len(s) <= debugLogLimit {
26+
return s
27+
}
28+
return s[:debugLogLimit] + fmt.Sprintf("... [truncated %d bytes]", len(s)-debugLogLimit)
29+
}
2230

2331
// Proxy struct
2432
type Proxy struct {
@@ -90,6 +98,8 @@ func (p *Proxy) CreateUpstreamRequest(ctx context.Context, ccBody api.CCRequestB
9098
return nil, fmt.Errorf("failed to build request: %w", err)
9199
}
92100

101+
log.Printf("[DEBUG] CommandCode request body: %s", truncateLog(string(reqJSON)))
102+
93103
ccReq, err := http.NewRequestWithContext(ctx, http.MethodPost,
94104
p.BaseURL+"/alpha/generate", bytes.NewReader(reqJSON))
95105
if err != nil {
@@ -140,6 +150,8 @@ func (p *Proxy) HandleChatCompletions(w http.ResponseWriter, r *http.Request) {
140150
return
141151
}
142152

153+
log.Printf("[DEBUG] Client request body: %s", truncateLog(string(body)))
154+
143155
var openAIReq api.OpenAIChatRequest
144156
if err := json.Unmarshal(body, &openAIReq); err != nil {
145157
http.Error(w, fmt.Sprintf(`{"error":{"message":"Invalid JSON: %s"}}`, err.Error()), http.StatusBadRequest)
@@ -219,6 +231,7 @@ func (p *Proxy) StreamResponse(w http.ResponseWriter, r *http.Request, ccResp *h
219231
if line == "" {
220232
continue
221233
}
234+
log.Printf("[DEBUG] CommandCode stream line: %s", truncateLog(line))
222235

223236
var event api.CCStreamEvent
224237
if err := json.Unmarshal([]byte(line), &event); err != nil {
@@ -325,6 +338,7 @@ func (p *Proxy) NonStreamResponse(w http.ResponseWriter, ccResp *http.Response,
325338
if line == "" {
326339
continue
327340
}
341+
log.Printf("[DEBUG] CommandCode stream line: %s", truncateLog(line))
328342

329343
var event api.CCStreamEvent
330344
if err := json.Unmarshal([]byte(line), &event); err != nil {

0 commit comments

Comments
 (0)