-
Notifications
You must be signed in to change notification settings - Fork 442
Expand file tree
/
Copy pathadapter.go
More file actions
168 lines (150 loc) · 4.93 KB
/
Copy pathadapter.go
File metadata and controls
168 lines (150 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package anthropic
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/packages/ssestream"
"github.com/anthropics/anthropic-sdk-go/shared"
"github.com/docker/docker-agent/pkg/chat"
"github.com/docker/docker-agent/pkg/tools"
)
// streamAdapter adapts the Anthropic stream to our interface
type streamAdapter struct {
retryableStream[anthropic.MessageStreamEventUnion]
trackUsage bool
toolCall bool
// toolIDByBlock maps a content block index to its tool_use block ID.
// Anthropic emits each tool_use in its own content block; subsequent
// input_json_delta events carry the block index (not the tool ID), so
// we must remember the ID per block to route partial JSON correctly
// when multiple tool calls stream in parallel.
toolIDByBlock map[int64]string
}
func (c *Client) newStreamAdapter(stream *ssestream.Stream[anthropic.MessageStreamEventUnion], trackUsage bool) *streamAdapter {
return &streamAdapter{
retryableStream: retryableStream[anthropic.MessageStreamEventUnion]{stream: stream},
trackUsage: trackUsage,
toolIDByBlock: map[int64]string{},
}
}
// isContextLengthError checks if the error indicates context window exceeded.
// Anthropic returns HTTP 400 with type "invalid_request_error" for context length issues.
// Unfortunately there's no specific error code - we must check the message.
func isContextLengthError(err error) bool {
if err == nil {
return false
}
apiErr, ok := errors.AsType[*anthropic.Error](err)
if !ok || apiErr.StatusCode != http.StatusBadRequest {
return false
}
// Parse the error response to get the structured error object
var errResp struct {
Error shared.ErrorObjectUnion `json:"error"`
}
if json.Unmarshal([]byte(apiErr.RawJSON()), &errResp) != nil {
return false
}
// Check if it's an invalid_request_error with a context-length message
if errResp.Error.Type != "invalid_request_error" {
return false
}
msg := errResp.Error.Message
return strings.Contains(msg, "prompt is too long") ||
strings.Contains(msg, "too many tokens") ||
strings.Contains(msg, "context length") ||
strings.Contains(msg, "maximum context")
}
// Recv gets the next completion chunk
func (a *streamAdapter) Recv() (chat.MessageStreamResponse, error) {
ok, err := a.next()
if !ok {
return chat.MessageStreamResponse{}, wrapAnthropicError(err)
}
event := a.stream.Current()
response := chat.MessageStreamResponse{
ID: event.Message.ID,
Object: "chat.completion.chunk",
Model: event.Message.Model,
Choices: []chat.MessageStreamChoice{
{
Index: 0,
Delta: chat.MessageDelta{
Role: string(chat.MessageRoleAssistant),
},
},
},
}
// Handle different event types
switch eventVariant := event.AsAny().(type) {
case anthropic.ContentBlockStartEvent:
switch block := eventVariant.ContentBlock.AsAny().(type) {
case anthropic.ToolUseBlock:
if a.toolIDByBlock == nil {
a.toolIDByBlock = map[int64]string{}
}
a.toolIDByBlock[eventVariant.Index] = block.ID
a.toolCall = true
toolCall := tools.ToolCall{
ID: block.ID,
Type: "function",
Function: tools.FunctionCall{
Name: block.Name,
},
}
response.Choices[0].Delta.ToolCalls = []tools.ToolCall{toolCall}
case anthropic.ThinkingBlock:
// Emit initial thinking content and signature
if block.Thinking != "" {
response.Choices[0].Delta.ReasoningContent = block.Thinking
}
if block.Signature != "" {
response.Choices[0].Delta.ThinkingSignature = block.Signature
}
}
case anthropic.ContentBlockDeltaEvent:
switch deltaVariant := eventVariant.Delta.AsAny().(type) {
case anthropic.TextDelta:
response.Choices[0].Delta.Content = deltaVariant.Text
case anthropic.ThinkingDelta:
response.Choices[0].Delta.ReasoningContent = deltaVariant.Thinking
case anthropic.SignatureDelta:
response.Choices[0].Delta.ThinkingSignature = deltaVariant.Signature
case anthropic.InputJSONDelta:
inputBytes := deltaVariant.PartialJSON
toolCall := tools.ToolCall{
ID: a.toolIDByBlock[eventVariant.Index],
Type: "function",
Function: tools.FunctionCall{
Arguments: inputBytes,
},
}
response.Choices[0].Delta.ToolCalls = []tools.ToolCall{toolCall}
default:
return response, fmt.Errorf("unknown delta type: %T", deltaVariant)
}
case anthropic.MessageDeltaEvent:
if a.trackUsage {
response.Usage = &chat.Usage{
InputTokens: eventVariant.Usage.InputTokens,
OutputTokens: eventVariant.Usage.OutputTokens,
CachedInputTokens: eventVariant.Usage.CacheReadInputTokens,
CacheWriteTokens: eventVariant.Usage.CacheCreationInputTokens,
}
}
case anthropic.MessageStopEvent:
if a.toolCall {
response.Choices[0].FinishReason = chat.FinishReasonToolCalls
} else {
response.Choices[0].FinishReason = chat.FinishReasonStop
}
}
return response, nil
}
// Close closes the stream
func (a *streamAdapter) Close() {
a.stream.Close()
}