Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/llm/provider/anthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/charmbracelet/catwalk/pkg/catwalk"
"github.com/charmbracelet/crush/internal/config"
"github.com/charmbracelet/crush/internal/llm/tools"
"github.com/charmbracelet/crush/internal/log"
"github.com/charmbracelet/crush/internal/message"
)

Expand Down Expand Up @@ -272,6 +273,12 @@ func (a *anthropicClient) send(ctx context.Context, messages []message.Message,
if a.isThinkingEnabled() {
opts = append(opts, option.WithHeaderAdd("anthropic-beta", "interleaved-thinking-2025-05-14"))
}

slog.Debug("Making Anthropic API request",
"api_key", log.MaskAPIKey(a.providerOptions.apiKey),
"model", preparedMessages.Model,
"attempt", attempts)

anthropicResponse, err := a.client.Messages.New(
ctx,
preparedMessages,
Expand Down Expand Up @@ -330,6 +337,11 @@ func (a *anthropicClient) stream(ctx context.Context, messages []message.Message
opts = append(opts, option.WithHeaderAdd("anthropic-beta", "interleaved-thinking-2025-05-14"))
}

slog.Debug("Making Anthropic streaming API request",
"api_key", log.MaskAPIKey(a.providerOptions.apiKey),
"model", preparedMessages.Model,
"attempt", attempts)

anthropicStream := a.client.Messages.NewStreaming(
ctx,
preparedMessages,
Expand Down
11 changes: 11 additions & 0 deletions internal/llm/provider/gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/charmbracelet/catwalk/pkg/catwalk"
"github.com/charmbracelet/crush/internal/config"
"github.com/charmbracelet/crush/internal/llm/tools"
"github.com/charmbracelet/crush/internal/log"
"github.com/charmbracelet/crush/internal/message"
"github.com/google/uuid"
"google.golang.org/genai"
Expand Down Expand Up @@ -198,6 +199,11 @@ func (g *geminiClient) send(ctx context.Context, messages []message.Message, too
attempts := 0
for {
attempts++
slog.Debug("Making Gemini API request",
"api_key", log.MaskAPIKey(g.providerOptions.apiKey),
"model", model.ID,
"attempt", attempts)

var toolCalls []message.ToolCall

var lastMsgParts []genai.Part
Expand Down Expand Up @@ -307,6 +313,11 @@ func (g *geminiClient) stream(ctx context.Context, messages []message.Message, t

for {
attempts++

slog.Debug("Making Gemini streaming API request",
"api_key", log.MaskAPIKey(g.providerOptions.apiKey),
"model", model.ID,
"attempt", attempts)

currentContent := ""
toolCalls := []message.ToolCall{}
Expand Down
12 changes: 12 additions & 0 deletions internal/llm/provider/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/charmbracelet/catwalk/pkg/catwalk"
"github.com/charmbracelet/crush/internal/config"
"github.com/charmbracelet/crush/internal/llm/tools"
"github.com/charmbracelet/crush/internal/log"
"github.com/charmbracelet/crush/internal/message"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
Expand Down Expand Up @@ -258,6 +259,11 @@ func (o *openaiClient) send(ctx context.Context, messages []message.Message, too
attempts := 0
for {
attempts++
slog.Debug("Making OpenAI API request",
"api_key", log.MaskAPIKey(o.providerOptions.apiKey),
"model", params.Model,
"attempt", attempts)

openaiResponse, err := o.client.Chat.Completions.New(
ctx,
params,
Expand Down Expand Up @@ -327,6 +333,12 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t
if len(params.Tools) == 0 {
params.Tools = nil
}

slog.Debug("Making OpenAI streaming API request",
"api_key", log.MaskAPIKey(o.providerOptions.apiKey),
"model", params.Model,
"attempt", attempts)

openaiStream := o.client.Chat.Completions.NewStreaming(
ctx,
params,
Expand Down
23 changes: 23 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log/slog"
"os"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -46,6 +47,28 @@ func Initialized() bool {
return initialized.Load()
}

// MaskAPIKey masks an API key by showing only the first and last 5 characters.
// For keys shorter than 10 characters, it shows first 2 and last 2 characters.
// Returns "***EMPTY***" for empty strings.
func MaskAPIKey(apiKey string) string {
if apiKey == "" {
return "***EMPTY***"
}

// Remove common prefixes
key := strings.TrimPrefix(apiKey, "Bearer ")
key = strings.TrimPrefix(key, "sk-")

keyLen := len(key)
if keyLen <= 4 {
return strings.Repeat("*", keyLen)
} else if keyLen <= 10 {
return key[:2] + strings.Repeat("*", keyLen-4) + key[keyLen-2:]
} else {
return key[:5] + strings.Repeat("*", keyLen-10) + key[keyLen-5:]
}
}

func RecoverPanic(name string, cleanup func()) {
if r := recover(); r != nil {
// Create a timestamped panic log file
Expand Down
Loading