Răsfoiți Sursa

fix: anthropic non-empty blocks

adamdottv 9 luni în urmă
părinte
comite
f004a0b8c3
2 a modificat fișierele cu 20 adăugiri și 12 ștergeri
  1. 13 8
      internal/llm/provider/anthropic.go
  2. 7 4
      internal/message/content.go

+ 13 - 8
internal/llm/provider/anthropic.go

@@ -74,14 +74,19 @@ func (a *anthropicClient) convertMessages(messages []message.Message) (anthropic
 
 		case message.Assistant:
 			blocks := []anthropic.ContentBlockParamUnion{}
-			if msg.Content().String() != "" {
-				content := anthropic.NewTextBlock(msg.Content().String())
+
+			if msg.Content() != nil {
+				content := msg.Content().String()
+				if strings.TrimSpace(content) == "" {
+					content = " "
+				}
+				block := anthropic.NewTextBlock(content)
 				if cache && !a.options.disableCache {
-					content.OfRequestTextBlock.CacheControl = anthropic.CacheControlEphemeralParam{
+					block.OfRequestTextBlock.CacheControl = anthropic.CacheControlEphemeralParam{
 						Type: "ephemeral",
 					}
 				}
-				blocks = append(blocks, content)
+				blocks = append(blocks, block)
 			}
 
 			for _, toolCall := range msg.ToolCalls() {
@@ -196,8 +201,8 @@ func (a *anthropicClient) send(ctx context.Context, messages []message.Message,
 	preparedMessages := a.preparedMessages(a.convertMessages(messages), a.convertTools(tools))
 	cfg := config.Get()
 	if cfg.Debug {
-		// jsonData, _ := json.Marshal(preparedMessages)
-		// logging.Debug("Prepared messages", "messages", string(jsonData))
+		jsonData, _ := json.Marshal(preparedMessages)
+		logging.Debug("Prepared messages", "messages", string(jsonData))
 	}
 	attempts := 0
 	for {
@@ -243,8 +248,8 @@ func (a *anthropicClient) stream(ctx context.Context, messages []message.Message
 	preparedMessages := a.preparedMessages(a.convertMessages(messages), a.convertTools(tools))
 	cfg := config.Get()
 	if cfg.Debug {
-		// jsonData, _ := json.Marshal(preparedMessages)
-		// logging.Debug("Prepared messages", "messages", string(jsonData))
+		jsonData, _ := json.Marshal(preparedMessages)
+		logging.Debug("Prepared messages", "messages", string(jsonData))
 	}
 	attempts := 0
 	eventChan := make(chan ProviderEvent)

+ 7 - 4
internal/message/content.go

@@ -48,7 +48,10 @@ type TextContent struct {
 	Text string `json:"text"`
 }
 
-func (tc TextContent) String() string {
+func (tc *TextContent) String() string {
+	if tc == nil {
+		return ""
+	}
 	return tc.Text
 }
 
@@ -115,13 +118,13 @@ type Message struct {
 	UpdatedAt int64
 }
 
-func (m *Message) Content() TextContent {
+func (m *Message) Content() *TextContent {
 	for _, part := range m.Parts {
 		if c, ok := part.(TextContent); ok {
-			return c
+			return &c
 		}
 	}
-	return TextContent{}
+	return nil
 }
 
 func (m *Message) ReasoningContent() ReasoningContent {