anthropic: remove redundant comments
Remove obvious comments that don't add value (e.g., "// Convert messages", "// Handle done"). Keep godoc comments and those explaining API mappings.
This commit is contained in:
parent
ed1e17bb35
commit
90cf232df2
|
|
@ -232,7 +232,6 @@ type StreamErrorEvent struct {
|
||||||
func FromMessagesRequest(r MessagesRequest) (*api.ChatRequest, error) {
|
func FromMessagesRequest(r MessagesRequest) (*api.ChatRequest, error) {
|
||||||
var messages []api.Message
|
var messages []api.Message
|
||||||
|
|
||||||
// Handle system prompt
|
|
||||||
if r.System != nil {
|
if r.System != nil {
|
||||||
switch sys := r.System.(type) {
|
switch sys := r.System.(type) {
|
||||||
case string:
|
case string:
|
||||||
|
|
@ -257,7 +256,6 @@ func FromMessagesRequest(r MessagesRequest) (*api.ChatRequest, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert messages
|
|
||||||
for _, msg := range r.Messages {
|
for _, msg := range r.Messages {
|
||||||
converted, err := convertMessage(msg)
|
converted, err := convertMessage(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -266,7 +264,6 @@ func FromMessagesRequest(r MessagesRequest) (*api.ChatRequest, error) {
|
||||||
messages = append(messages, converted...)
|
messages = append(messages, converted...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build options
|
|
||||||
options := make(map[string]any)
|
options := make(map[string]any)
|
||||||
|
|
||||||
options["num_predict"] = r.MaxTokens
|
options["num_predict"] = r.MaxTokens
|
||||||
|
|
@ -287,7 +284,6 @@ func FromMessagesRequest(r MessagesRequest) (*api.ChatRequest, error) {
|
||||||
options["stop"] = r.StopSequences
|
options["stop"] = r.StopSequences
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert tools
|
|
||||||
var tools api.Tools
|
var tools api.Tools
|
||||||
for _, t := range r.Tools {
|
for _, t := range r.Tools {
|
||||||
tool, err := convertTool(t)
|
tool, err := convertTool(t)
|
||||||
|
|
@ -297,7 +293,6 @@ func FromMessagesRequest(r MessagesRequest) (*api.ChatRequest, error) {
|
||||||
tools = append(tools, tool)
|
tools = append(tools, tool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle thinking
|
|
||||||
var think *api.ThinkValue
|
var think *api.ThinkValue
|
||||||
if r.Thinking != nil && r.Thinking.Type == "enabled" {
|
if r.Thinking != nil && r.Thinking.Type == "enabled" {
|
||||||
think = &api.ThinkValue{Value: true}
|
think = &api.ThinkValue{Value: true}
|
||||||
|
|
@ -325,7 +320,6 @@ func convertMessage(msg MessageParam) ([]api.Message, error) {
|
||||||
messages = append(messages, api.Message{Role: role, Content: content})
|
messages = append(messages, api.Message{Role: role, Content: content})
|
||||||
|
|
||||||
case []any:
|
case []any:
|
||||||
// Handle array of content blocks
|
|
||||||
var textContent strings.Builder
|
var textContent strings.Builder
|
||||||
var images []api.ImageData
|
var images []api.ImageData
|
||||||
var toolCalls []api.ToolCall
|
var toolCalls []api.ToolCall
|
||||||
|
|
@ -360,6 +354,8 @@ func convertMessage(msg MessageParam) ([]api.Message, error) {
|
||||||
return nil, fmt.Errorf("invalid base64 image data: %w", err)
|
return nil, fmt.Errorf("invalid base64 image data: %w", err)
|
||||||
}
|
}
|
||||||
images = append(images, decoded)
|
images = append(images, decoded)
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("invalid image source type: %s. Only base64 images are supported.", sourceType)
|
||||||
}
|
}
|
||||||
// URL images would need to be fetched - skip for now
|
// URL images would need to be fetched - skip for now
|
||||||
|
|
||||||
|
|
@ -391,7 +387,6 @@ func convertMessage(msg MessageParam) ([]api.Message, error) {
|
||||||
case string:
|
case string:
|
||||||
resultContent = c
|
resultContent = c
|
||||||
case []any:
|
case []any:
|
||||||
// Extract text from content blocks
|
|
||||||
for _, cb := range c {
|
for _, cb := range c {
|
||||||
if cbMap, ok := cb.(map[string]any); ok {
|
if cbMap, ok := cb.(map[string]any); ok {
|
||||||
if cbMap["type"] == "text" {
|
if cbMap["type"] == "text" {
|
||||||
|
|
@ -416,7 +411,6 @@ func convertMessage(msg MessageParam) ([]api.Message, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the main message
|
|
||||||
if textContent.Len() > 0 || len(images) > 0 || len(toolCalls) > 0 {
|
if textContent.Len() > 0 || len(images) > 0 || len(toolCalls) > 0 {
|
||||||
m := api.Message{
|
m := api.Message{
|
||||||
Role: role,
|
Role: role,
|
||||||
|
|
@ -461,7 +455,6 @@ func convertTool(t Tool) (api.Tool, error) {
|
||||||
func ToMessagesResponse(id string, r api.ChatResponse) MessagesResponse {
|
func ToMessagesResponse(id string, r api.ChatResponse) MessagesResponse {
|
||||||
var content []ContentBlock
|
var content []ContentBlock
|
||||||
|
|
||||||
// Add thinking block if present
|
|
||||||
if r.Message.Thinking != "" {
|
if r.Message.Thinking != "" {
|
||||||
content = append(content, ContentBlock{
|
content = append(content, ContentBlock{
|
||||||
Type: "thinking",
|
Type: "thinking",
|
||||||
|
|
@ -469,7 +462,6 @@ func ToMessagesResponse(id string, r api.ChatResponse) MessagesResponse {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add text content if present
|
|
||||||
if r.Message.Content != "" {
|
if r.Message.Content != "" {
|
||||||
content = append(content, ContentBlock{
|
content = append(content, ContentBlock{
|
||||||
Type: "text",
|
Type: "text",
|
||||||
|
|
@ -477,7 +469,6 @@ func ToMessagesResponse(id string, r api.ChatResponse) MessagesResponse {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add tool use blocks
|
|
||||||
for _, tc := range r.Message.ToolCalls {
|
for _, tc := range r.Message.ToolCalls {
|
||||||
content = append(content, ContentBlock{
|
content = append(content, ContentBlock{
|
||||||
Type: "tool_use",
|
Type: "tool_use",
|
||||||
|
|
@ -487,7 +478,6 @@ func ToMessagesResponse(id string, r api.ChatResponse) MessagesResponse {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map stop reason
|
|
||||||
stopReason := mapStopReason(r.DoneReason, len(r.Message.ToolCalls) > 0)
|
stopReason := mapStopReason(r.DoneReason, len(r.Message.ToolCalls) > 0)
|
||||||
|
|
||||||
return MessagesResponse{
|
return MessagesResponse{
|
||||||
|
|
@ -537,7 +527,6 @@ type StreamConverter struct {
|
||||||
toolCallsSent map[string]bool
|
toolCallsSent map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStreamConverter creates a new StreamConverter
|
|
||||||
func NewStreamConverter(id, model string) *StreamConverter {
|
func NewStreamConverter(id, model string) *StreamConverter {
|
||||||
return &StreamConverter{
|
return &StreamConverter{
|
||||||
ID: id,
|
ID: id,
|
||||||
|
|
@ -557,7 +546,6 @@ type StreamEvent struct {
|
||||||
func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
|
func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
|
||||||
var events []StreamEvent
|
var events []StreamEvent
|
||||||
|
|
||||||
// First write: emit message_start
|
|
||||||
if c.firstWrite {
|
if c.firstWrite {
|
||||||
c.firstWrite = false
|
c.firstWrite = false
|
||||||
c.inputTokens = r.Metrics.PromptEvalCount
|
c.inputTokens = r.Metrics.PromptEvalCount
|
||||||
|
|
@ -581,7 +569,6 @@ func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle thinking content
|
|
||||||
if r.Message.Thinking != "" && !c.thinkingDone {
|
if r.Message.Thinking != "" && !c.thinkingDone {
|
||||||
if !c.thinkingStarted {
|
if !c.thinkingStarted {
|
||||||
c.thinkingStarted = true
|
c.thinkingStarted = true
|
||||||
|
|
@ -611,9 +598,7 @@ func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle text content
|
|
||||||
if r.Message.Content != "" {
|
if r.Message.Content != "" {
|
||||||
// Close thinking block if it was open
|
|
||||||
if c.thinkingStarted && !c.thinkingDone {
|
if c.thinkingStarted && !c.thinkingDone {
|
||||||
c.thinkingDone = true
|
c.thinkingDone = true
|
||||||
events = append(events, StreamEvent{
|
events = append(events, StreamEvent{
|
||||||
|
|
@ -654,13 +639,11 @@ func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle tool calls
|
|
||||||
for _, tc := range r.Message.ToolCalls {
|
for _, tc := range r.Message.ToolCalls {
|
||||||
if c.toolCallsSent[tc.ID] {
|
if c.toolCallsSent[tc.ID] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close any previous block
|
|
||||||
if c.textStarted {
|
if c.textStarted {
|
||||||
events = append(events, StreamEvent{
|
events = append(events, StreamEvent{
|
||||||
Event: "content_block_stop",
|
Event: "content_block_stop",
|
||||||
|
|
@ -673,14 +656,12 @@ func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
|
||||||
c.textStarted = false
|
c.textStarted = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal arguments first to check for errors before starting block
|
|
||||||
argsJSON, err := json.Marshal(tc.Function.Arguments)
|
argsJSON, err := json.Marshal(tc.Function.Arguments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("failed to marshal tool arguments", "error", err, "tool_id", tc.ID)
|
slog.Error("failed to marshal tool arguments", "error", err, "tool_id", tc.ID)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start tool use block
|
|
||||||
events = append(events, StreamEvent{
|
events = append(events, StreamEvent{
|
||||||
Event: "content_block_start",
|
Event: "content_block_start",
|
||||||
Data: ContentBlockStartEvent{
|
Data: ContentBlockStartEvent{
|
||||||
|
|
@ -695,7 +676,6 @@ func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
// Send input as JSON delta
|
|
||||||
events = append(events, StreamEvent{
|
events = append(events, StreamEvent{
|
||||||
Event: "content_block_delta",
|
Event: "content_block_delta",
|
||||||
Data: ContentBlockDeltaEvent{
|
Data: ContentBlockDeltaEvent{
|
||||||
|
|
@ -708,7 +688,6 @@ func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
// Close tool use block
|
|
||||||
events = append(events, StreamEvent{
|
events = append(events, StreamEvent{
|
||||||
Event: "content_block_stop",
|
Event: "content_block_stop",
|
||||||
Data: ContentBlockStopEvent{
|
Data: ContentBlockStopEvent{
|
||||||
|
|
@ -721,9 +700,7 @@ func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
|
||||||
c.contentIndex++
|
c.contentIndex++
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle done
|
|
||||||
if r.Done {
|
if r.Done {
|
||||||
// Close any open block
|
|
||||||
if c.textStarted {
|
if c.textStarted {
|
||||||
events = append(events, StreamEvent{
|
events = append(events, StreamEvent{
|
||||||
Event: "content_block_stop",
|
Event: "content_block_stop",
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,6 @@ func (w *AnthropicWriter) writeResponse(data []byte) (int, error) {
|
||||||
return len(data), nil
|
return len(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Non-streaming response
|
|
||||||
w.ResponseWriter.Header().Set("Content-Type", "application/json")
|
w.ResponseWriter.Header().Set("Content-Type", "application/json")
|
||||||
response := anthropic.ToMessagesResponse(w.id, chatResponse)
|
response := anthropic.ToMessagesResponse(w.id, chatResponse)
|
||||||
return len(data), json.NewEncoder(w.ResponseWriter).Encode(response)
|
return len(data), json.NewEncoder(w.ResponseWriter).Encode(response)
|
||||||
|
|
@ -97,7 +96,6 @@ func AnthropicMessagesMiddleware() gin.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate required fields
|
|
||||||
if req.Model == "" {
|
if req.Model == "" {
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, anthropic.NewError(http.StatusBadRequest, "model is required"))
|
c.AbortWithStatusJSON(http.StatusBadRequest, anthropic.NewError(http.StatusBadRequest, "model is required"))
|
||||||
return
|
return
|
||||||
|
|
@ -113,7 +111,6 @@ func AnthropicMessagesMiddleware() gin.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert to internal format
|
|
||||||
chatReq, err := anthropic.FromMessagesRequest(req)
|
chatReq, err := anthropic.FromMessagesRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, anthropic.NewError(http.StatusBadRequest, err.Error()))
|
c.AbortWithStatusJSON(http.StatusBadRequest, anthropic.NewError(http.StatusBadRequest, err.Error()))
|
||||||
|
|
@ -138,7 +135,6 @@ func AnthropicMessagesMiddleware() gin.HandlerFunc {
|
||||||
converter: anthropic.NewStreamConverter(messageID, req.Model),
|
converter: anthropic.NewStreamConverter(messageID, req.Model),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set headers based on streaming mode
|
|
||||||
if req.Stream {
|
if req.Stream {
|
||||||
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
||||||
c.Writer.Header().Set("Cache-Control", "no-cache")
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue