-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.go
More file actions
286 lines (247 loc) · 7.4 KB
/
stream.go
File metadata and controls
286 lines (247 loc) · 7.4 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package blockrun
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
// ChatCompletionChunk represents a single SSE chunk from a streaming response.
type ChatCompletionChunk struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []ChunkChoice `json:"choices"`
}
// ChunkChoice represents a single choice within a streaming chunk.
type ChunkChoice struct {
Index int `json:"index"`
Delta ChunkDelta `json:"delta"`
FinishReason string `json:"finish_reason,omitempty"`
}
// ChunkDelta represents the incremental content in a streaming chunk.
type ChunkDelta struct {
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
}
// Stream reads SSE chunks from an active connection.
type Stream struct {
scanner *bufio.Scanner
body io.ReadCloser
done bool
}
// Next returns the next chunk. Returns nil, nil when the stream is complete.
func (s *Stream) Next() (*ChatCompletionChunk, error) {
if s.done {
return nil, nil
}
for s.scanner.Scan() {
line := s.scanner.Text()
// Skip empty lines
if line == "" {
continue
}
// Skip lines that don't start with "data: "
if !strings.HasPrefix(line, "data: ") {
continue
}
data := strings.TrimPrefix(line, "data: ")
// Check for stream termination
if data == "[DONE]" {
s.done = true
return nil, nil
}
// Unmarshal the JSON chunk
var chunk ChatCompletionChunk
if err := json.Unmarshal([]byte(data), &chunk); err != nil {
return nil, fmt.Errorf("failed to unmarshal chunk: %w", err)
}
return &chunk, nil
}
if err := s.scanner.Err(); err != nil {
return nil, fmt.Errorf("scanner error: %w", err)
}
// Scanner exhausted without [DONE]
s.done = true
return nil, nil
}
// Close closes the underlying connection.
func (s *Stream) Close() error {
s.done = true
return s.body.Close()
}
// ChatCompletionStream sends a streaming chat completion request and returns a Stream.
//
// The caller must call Stream.Close() when done reading to release the connection.
func (c *LLMClient) ChatCompletionStream(ctx context.Context, model string, messages []ChatMessage, opts *ChatCompletionOptions) (*Stream, error) {
// Validate inputs
if model == "" {
return nil, &ValidationError{Field: "model", Message: "Model is required"}
}
if len(messages) == 0 {
return nil, &ValidationError{Field: "messages", Message: "At least one message is required"}
}
// Build request body
body := map[string]any{
"model": model,
"messages": messages,
"stream": true,
}
// Apply options
maxTokens := DefaultMaxTokens
if opts != nil {
if opts.MaxTokens > 0 {
maxTokens = opts.MaxTokens
}
if opts.Temperature > 0 {
body["temperature"] = opts.Temperature
}
if opts.TopP > 0 {
body["top_p"] = opts.TopP
}
if opts.SearchParameters != nil {
body["search_parameters"] = opts.SearchParameters
} else if opts.Search {
body["search_parameters"] = map[string]string{"mode": "on"}
}
if opts.Tools != nil {
body["tools"] = opts.Tools
}
if opts.ToolChoice != nil {
body["tool_choice"] = opts.ToolChoice
}
}
body["max_tokens"] = maxTokens
url := c.apiURL + "/v1/chat/completions"
jsonBody, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to encode request body: %w", err)
}
// First attempt
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonBody))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "text/event-stream")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
// Handle 402 Payment Required
if resp.StatusCode == http.StatusPaymentRequired {
defer resp.Body.Close()
return c.handleStreamPaymentAndRetry(ctx, url, jsonBody, resp)
}
// Handle other errors
if resp.StatusCode != http.StatusOK {
defer resp.Body.Close()
bodyBytes, _ := io.ReadAll(resp.Body)
return nil, &APIError{
StatusCode: resp.StatusCode,
Message: fmt.Sprintf("API error: %s", string(bodyBytes)),
}
}
return &Stream{
scanner: bufio.NewScanner(resp.Body),
body: resp.Body,
}, nil
}
// handleStreamPaymentAndRetry handles a 402 response for streaming requests.
func (c *LLMClient) handleStreamPaymentAndRetry(ctx context.Context, url string, jsonBody []byte, resp *http.Response) (*Stream, error) {
// Get payment required header
paymentHeader := resp.Header.Get("payment-required")
if paymentHeader == "" {
// Try to get from response body
var respBody map[string]any
if err := json.NewDecoder(resp.Body).Decode(&respBody); err == nil {
if _, ok := respBody["x402"]; ok {
jsonBytes, _ := json.Marshal(respBody)
paymentHeader = string(jsonBytes)
}
}
}
if paymentHeader == "" {
return nil, &PaymentError{Message: "402 response but no payment requirements found"}
}
// Parse payment requirements
paymentReq, err := ParsePaymentRequired(paymentHeader)
if err != nil {
return nil, &PaymentError{Message: fmt.Sprintf("Failed to parse payment requirements: %v", err)}
}
// Extract payment details
paymentOption, err := ExtractPaymentDetails(paymentReq)
if err != nil {
return nil, &PaymentError{Message: fmt.Sprintf("Failed to extract payment details: %v", err)}
}
// Determine resource URL
resourceURL := paymentReq.Resource.URL
if resourceURL == "" {
resourceURL = url
}
// Create signed payment payload
paymentPayload, err := CreatePaymentPayload(
c.privateKey,
paymentOption.PayTo,
paymentOption.Amount,
paymentOption.Network,
resourceURL,
paymentReq.Resource.Description,
paymentOption.MaxTimeoutSeconds,
paymentOption.Extra,
paymentReq.Extensions,
)
if err != nil {
return nil, &PaymentError{Message: fmt.Sprintf("Failed to create payment: %v", err)}
}
// Retry with payment signature
retryReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonBody))
if err != nil {
return nil, fmt.Errorf("failed to create retry request: %w", err)
}
retryReq.Header.Set("Content-Type", "application/json")
retryReq.Header.Set("Accept", "text/event-stream")
retryReq.Header.Set("PAYMENT-SIGNATURE", paymentPayload)
retryResp, err := c.httpClient.Do(retryReq)
if err != nil {
return nil, fmt.Errorf("retry request failed: %w", err)
}
// Check for payment rejection
if retryResp.StatusCode == http.StatusPaymentRequired {
retryResp.Body.Close()
return nil, &PaymentError{Message: "Payment was rejected. Check your wallet balance."}
}
// Handle other errors
if retryResp.StatusCode != http.StatusOK {
defer retryResp.Body.Close()
bodyBytes, _ := io.ReadAll(retryResp.Body)
return nil, &APIError{
StatusCode: retryResp.StatusCode,
Message: fmt.Sprintf("API error after payment: %s", string(bodyBytes)),
}
}
// Track spending
c.mu.Lock()
c.sessionCalls++
var costUSD float64
if amountStr := paymentOption.Amount; amountStr != "" {
var amountMicro float64
if _, err := fmt.Sscanf(amountStr, "%f", &amountMicro); err == nil {
costUSD = amountMicro / 1_000_000
c.sessionTotalUSD += costUSD
}
}
c.mu.Unlock()
if c.costLog != nil && costUSD > 0 {
endpoint := strings.TrimPrefix(url, c.apiURL)
c.costLog.Append(endpoint, costUSD)
}
return &Stream{
scanner: bufio.NewScanner(retryResp.Body),
body: retryResp.Body,
}, nil
}