This commit is contained in:
HugeFrog24
2026-05-25 22:56:12 +02:00
parent 8c699ab70a
commit b22b8b98fe
4 changed files with 95 additions and 19 deletions
+15 -9
View File
@@ -17,7 +17,7 @@ import (
// actionable message to admins/owners while keeping the response vague for regular users.
var ErrModelNotFound = errors.New("model not found or deprecated")
func (b *Bot) getAnthropicResponse(ctx context.Context, messages []anthropic.BetaMessageParam, isNewChat, isOwner, isEmojiOnly bool, username string, firstName string, lastName string, isPremium bool, languageCode string, messageTime int) (string, error) {
func (b *Bot) getAnthropicResponse(ctx context.Context, messages []anthropic.BetaMessageParam, isNewChat, isOwner, isEmojiOnly bool, username string, firstName string, lastName string, isPremium bool, languageCode string, messageTime int) ([]string, error) {
// Use prompts from config
var systemMessage string
if isNewChat {
@@ -143,16 +143,23 @@ func (b *Bot) getAnthropicResponse(ctx context.Context, messages []anthropic.Bet
if err != nil {
var apiErr *anthropic.Error
if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound {
return "", fmt.Errorf("%w: %s", ErrModelNotFound, b.config.Model)
return nil, fmt.Errorf("%w: %s", ErrModelNotFound, b.config.Model)
}
return "", fmt.Errorf("error creating Anthropic message: %w", err)
return nil, fmt.Errorf("error creating Anthropic message: %w", err)
}
var sb strings.Builder
// Collect text blocks as separate segments so the Telegram delivery layer
// can render each as its own message (matches the conversational rhythm
// Claude uses around tool calls). Callers that want one joined string
// (voice TTS, stickers) do strings.Join themselves.
var segments []string
for _, block := range resp.Content {
switch block.Type {
case "text":
sb.WriteString(block.Text)
t := strings.TrimSpace(block.Text)
if t != "" {
segments = append(segments, t)
}
case "mcp_tool_use":
InfoLogger.Printf("[mcp] tool_use server=%q name=%q id=%q input=%s",
block.ServerName, block.Name, block.ID, string(block.Input))
@@ -167,9 +174,8 @@ func (b *Bot) getAnthropicResponse(ctx context.Context, messages []anthropic.Bet
InfoLogger.Printf("[mcp] block type=%q (unhandled)", block.Type)
}
}
out := sb.String()
if out == "" {
return "", fmt.Errorf("unexpected response format from Anthropic")
if len(segments) == 0 {
return nil, fmt.Errorf("unexpected response format from Anthropic")
}
return out, nil
return segments, nil
}