Skip to content

Commit d9b8bc3

Browse files
committed
chore: refactor Send method to optimize string building
### CHANGES - Add `sourcesHeader` constant for citation section title. - Use `strings.Builder` to construct result efficiently. - Append sources header and citations in result builder. - Update `ret` to use constructed string from builder.
1 parent da29b8e commit d9b8bc3

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

plugins/ai/anthropic/anthropic.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const defaultBaseUrl = "https://api.anthropic.com/"
1616

1717
const webSearchToolName = "web_search"
1818
const webSearchToolType = "web_search_20250305"
19+
const sourcesHeader = "## Sources"
1920

2021
func NewClient() (ret *Client) {
2122
vendorName := "Anthropic"
@@ -183,12 +184,17 @@ func (an *Client) Send(ctx context.Context, msgs []*chat.ChatCompletionMessage,
183184
}
184185
}
185186

186-
ret = strings.Join(textParts, "")
187+
var resultBuilder strings.Builder
188+
resultBuilder.WriteString(strings.Join(textParts, ""))
187189

188190
// Append citations if any were found
189191
if len(citations) > 0 {
190-
ret += "\n\n## Sources\n\n" + strings.Join(citations, "\n")
192+
resultBuilder.WriteString("\n\n")
193+
resultBuilder.WriteString(sourcesHeader)
194+
resultBuilder.WriteString("\n\n")
195+
resultBuilder.WriteString(strings.Join(citations, "\n"))
191196
}
197+
ret = resultBuilder.String()
192198

193199
return
194200
}

0 commit comments

Comments
 (0)