-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage.go
More file actions
472 lines (423 loc) · 15.4 KB
/
Copy pathimage.go
File metadata and controls
472 lines (423 loc) · 15.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package blockrun
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
const (
// DefaultImageModel is the default image generation model.
DefaultImageModel = "google/nano-banana"
// Available image models (pass as ImageGenerateOptions.Model):
// openai/dall-e-3 $0.04-0.08/image
// openai/gpt-image-1 $0.02-0.04/image (also supports edit)
// openai/gpt-image-2 $0.06-0.12/image (ChatGPT Images 2.0, also supports edit)
// google/nano-banana $0.05/image
// google/nano-banana-pro $0.10-0.15/image
// black-forest/flux-1.1-pro $0.04/image
// xai/grok-imagine-image $0.02/image
// xai/grok-imagine-image-pro $0.07/image
// zai/cogview-4 $0.015/image
// DefaultImageSize is the default image size.
DefaultImageSize = "1024x1024"
// DefaultImageTimeout is the default timeout for image generation (images take longer).
DefaultImageTimeout = 120 * time.Second
// imagePollInterval is the wait between poll attempts on the async path.
imagePollInterval = 3 * time.Second
// imagePollBudget is the overall wall-clock budget for submit + polling on
// the async path. If upstream runs past this, the call returns an error
// without charging.
imagePollBudget = 300 * time.Second
// imageMaxTimeoutSeconds is the signed-auth window floor — bumped above
// the server default so the PAYMENT-SIGNATURE stays valid across the poll
// window of a slow generation.
imageMaxTimeoutSeconds = 600
)
// ImageClient is the BlockRun Image Generation client.
//
// SECURITY: Your private key is used ONLY for local EIP-712 signing.
// The key NEVER leaves your machine - only signatures are transmitted.
type ImageClient struct {
*baseClient
// pollInterval is the wait between poll attempts on the async path.
// Defaults to imagePollInterval; overridable (mainly for tests).
pollInterval time.Duration
}
// ImageClientOption is a function that configures an ImageClient.
type ImageClientOption func(*ImageClient)
// WithImageAPIURL sets a custom API URL for the image client.
func WithImageAPIURL(url string) ImageClientOption {
return func(c *ImageClient) {
c.apiURL = strings.TrimSuffix(url, "/")
}
}
// WithImageTimeout sets the HTTP timeout for the image client.
func WithImageTimeout(timeout time.Duration) ImageClientOption {
return func(c *ImageClient) {
c.httpClient.Timeout = timeout
}
}
// WithImageHTTPClient sets a custom HTTP client for the image client.
func WithImageHTTPClient(client *http.Client) ImageClientOption {
return func(c *ImageClient) {
c.httpClient = client
}
}
// NewImageClient creates a new BlockRun Image client.
//
// If privateKey is empty, it will be read from the BLOCKRUN_WALLET_KEY
// or BASE_CHAIN_WALLET_KEY environment variable.
func NewImageClient(privateKey string, opts ...ImageClientOption) (*ImageClient, error) {
bc, err := newBaseClient(privateKey, "", DefaultImageTimeout)
if err != nil {
return nil, err
}
client := &ImageClient{baseClient: bc, pollInterval: imagePollInterval}
// Apply options
for _, opt := range opts {
opt(client)
}
// Check for custom API URL in environment (after options so user-set URLs win)
bc.checkEnvAPIURL()
return client, nil
}
// ImageGenerateOptions contains optional parameters for image generation.
type ImageGenerateOptions struct {
Model string `json:"model,omitempty"`
Size string `json:"size,omitempty"`
N int `json:"n,omitempty"`
Quality string `json:"quality,omitempty"`
}
// ImageData represents a single generated image.
type ImageData struct {
URL string `json:"url"`
// SourceURL is the original upstream URL (e.g. imgen.x.ai). Omitted for data URIs.
SourceURL string `json:"source_url,omitempty"`
// BackedUp is true when the gateway mirrored the image to its GCS bucket.
BackedUp bool `json:"backed_up,omitempty"`
RevisedPrompt string `json:"revised_prompt,omitempty"`
B64JSON string `json:"b64_json,omitempty"`
}
// ImageResponse represents the API response for image generation.
type ImageResponse struct {
Created int64 `json:"created"`
Data []ImageData `json:"data"`
// TxHash is the on-chain USDC settlement transaction for this call (from
// the gateway's X-Payment-Receipt header), when available.
TxHash string `json:"tx_hash,omitempty"`
}
// ImageModel represents an available image model from the API.
type ImageModel struct {
ID string `json:"id"`
Name string `json:"name"`
Provider string `json:"provider"`
Description string `json:"description"`
PricePerImage float64 `json:"pricePerImage"`
SupportedSizes []string `json:"supportedSizes,omitempty"`
MaxPromptLength int `json:"maxPromptLength,omitempty"`
Available bool `json:"available"`
}
// Generate generates an image from a text prompt.
func (c *ImageClient) Generate(ctx context.Context, prompt string, opts *ImageGenerateOptions) (*ImageResponse, error) {
// Build request body
body := map[string]any{
"prompt": prompt,
"model": DefaultImageModel,
"size": DefaultImageSize,
"n": 1,
}
if opts != nil {
if opts.Model != "" {
body["model"] = opts.Model
}
if opts.Size != "" {
body["size"] = opts.Size
}
if opts.N > 0 {
body["n"] = opts.N
}
if opts.Quality != "" {
body["quality"] = opts.Quality
}
}
return c.submitImageAndMaybePoll(ctx, "/v1/images/generations", body)
}
// DefaultImageEditModel is the default model for image editing / fusion.
const DefaultImageEditModel = "openai/gpt-image-2"
// ImageEditOptions contains optional parameters for image editing.
type ImageEditOptions struct {
// Model is the edit-capable model ID. Defaults to DefaultImageEditModel.
// Edit-supported: openai/gpt-image-1, openai/gpt-image-2,
// google/nano-banana, google/nano-banana-pro.
Model string `json:"model,omitempty"`
// Mask is an optional base64 data URI marking the region to edit.
// Cannot be combined with multiple source images.
Mask string `json:"mask,omitempty"`
Size string `json:"size,omitempty"`
N int `json:"n,omitempty"`
}
// Edit edits or fuses images using img2img.
//
// Pass one source image for a standard edit, or multiple (up to the
// provider's limit, typically 4) to fuse them — e.g. a reference photo
// plus a brand logo. Each image must be a base64 data URI (data:image/...).
//
// Example (single):
//
// resp, err := client.Edit(ctx, "make the sky purple",
// []string{"data:image/png;base64,..."}, nil)
//
// Example (fusion):
//
// resp, err := client.Edit(ctx, "place the logo on the shirt",
// []string{photo, logo}, &blockrun.ImageEditOptions{Model: "google/nano-banana"})
func (c *ImageClient) Edit(ctx context.Context, prompt string, images []string, opts *ImageEditOptions) (*ImageResponse, error) {
if len(images) == 0 {
return nil, fmt.Errorf("at least one source image is required")
}
body := map[string]any{
"prompt": prompt,
"model": DefaultImageEditModel,
"size": DefaultImageSize,
"n": 1,
}
// Single image is sent as a string (OpenAI-compatible); multiple images
// are sent as an array for fusion. The gateway accepts both.
if len(images) == 1 {
body["image"] = images[0]
} else {
body["image"] = images
}
if opts != nil {
if opts.Model != "" {
body["model"] = opts.Model
}
if opts.Size != "" {
body["size"] = opts.Size
}
if opts.N > 0 {
body["n"] = opts.N
}
if opts.Mask != "" {
body["mask"] = opts.Mask
}
}
return c.submitImageAndMaybePoll(ctx, "/v1/images/image2image", body)
}
// submitImageAndMaybePoll runs the gateway's hybrid image pipeline shared by
// Generate and Edit. Fast models complete inline: POST (402 → sign → retry)
// returns 200 with image data and payment settled in the same call. Slow
// models return 202 { id, poll_url } instead; the gateway settles USDC only
// on the first poll that observes status=completed, so an upstream failure or
// a caller giving up costs nothing. This client GET-polls poll_url with the
// same wallet's PAYMENT-SIGNATURE until the job reaches a terminal state,
// then returns the same ImageResponse shape as the fast path — callers never
// see the async envelope.
func (c *ImageClient) submitImageAndMaybePoll(ctx context.Context, endpoint string, body map[string]any) (*ImageResponse, error) {
submitURL := c.apiURL + endpoint
jsonBody, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to encode request body: %w", err)
}
// Step 1: unauth POST → 402 with payment requirements.
req1, err := http.NewRequestWithContext(ctx, "POST", submitURL, bytes.NewReader(jsonBody))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req1.Header.Set("Content-Type", "application/json")
resp1, err := c.httpClient.Do(req1)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
paymentHeader := resp1.Header.Get("payment-required")
body1, _ := io.ReadAll(resp1.Body)
resp1.Body.Close()
// Free/proxy path: the server answered without requiring payment.
if resp1.StatusCode == http.StatusOK {
return decodeImageResponse(body1, resp1.Header)
}
if resp1.StatusCode != http.StatusPaymentRequired {
return nil, &APIError{
StatusCode: resp1.StatusCode,
Message: fmt.Sprintf("API error: %s", string(body1)),
}
}
if paymentHeader == "" {
return nil, &PaymentError{Message: "402 response but no payment requirements found"}
}
// Step 2: sign the payment authorization. Floor the validity window at
// imageMaxTimeoutSeconds so the same signature survives the poll window.
paymentReq, err := ParsePaymentRequired(paymentHeader)
if err != nil {
return nil, &PaymentError{Message: fmt.Sprintf("Failed to parse payment requirements: %v", err)}
}
paymentOption, err := ExtractPaymentDetails(paymentReq)
if err != nil {
return nil, &PaymentError{Message: fmt.Sprintf("Failed to extract payment details: %v", err)}
}
resourceURL := paymentReq.Resource.URL
if resourceURL == "" {
resourceURL = submitURL
}
maxTimeout := paymentOption.MaxTimeoutSeconds
if maxTimeout < imageMaxTimeoutSeconds {
maxTimeout = imageMaxTimeoutSeconds
}
paymentPayload, err := CreatePaymentPayload(
c.privateKey,
paymentOption.PayTo,
paymentOption.Amount,
paymentOption.Network,
resourceURL,
paymentReq.Resource.Description,
maxTimeout,
paymentOption.Extra,
paymentReq.Extensions,
)
if err != nil {
return nil, &PaymentError{Message: fmt.Sprintf("Failed to create payment: %v", err)}
}
// Step 3: retry with payment → 200 image data (fast path) or
// 202 { id, poll_url } (slow path).
req2, err := http.NewRequestWithContext(ctx, "POST", submitURL, bytes.NewReader(jsonBody))
if err != nil {
return nil, fmt.Errorf("failed to create submit request: %w", err)
}
req2.Header.Set("Content-Type", "application/json")
req2.Header.Set("PAYMENT-SIGNATURE", paymentPayload)
resp2, err := c.httpClient.Do(req2)
if err != nil {
return nil, fmt.Errorf("submit request failed: %w", err)
}
body2, _ := io.ReadAll(resp2.Body)
resp2.Body.Close()
switch resp2.StatusCode {
case http.StatusPaymentRequired:
return nil, &PaymentError{Message: "Payment was rejected. Check your wallet balance."}
case http.StatusOK:
// Fast path: generated and settled inline.
c.recordSettledCost(paymentOption.Amount, endpoint)
return decodeImageResponse(body2, resp2.Header)
case http.StatusAccepted:
// Slow path: async envelope — fall through to the poll loop below.
default:
return nil, &APIError{
StatusCode: resp2.StatusCode,
Message: fmt.Sprintf("API error after payment: %s", string(body2)),
}
}
var submitData struct {
ID string `json:"id"`
PollURL string `json:"poll_url"`
Status string `json:"status"`
}
if err := json.Unmarshal(body2, &submitData); err != nil {
return nil, fmt.Errorf("failed to decode submit response: %w", err)
}
if submitData.ID == "" || submitData.PollURL == "" {
return nil, &APIError{
StatusCode: resp2.StatusCode,
Message: fmt.Sprintf("submit response missing id/poll_url: %s", string(body2)),
}
}
pollURL := c.resolvePollURL(submitData.PollURL)
// Step 4: poll with the same PAYMENT-SIGNATURE until terminal. The
// gateway enforces wallet binding (not signature equality), so reusing
// the submit signature is valid and settles exactly once, on the first
// poll that observes "completed".
deadline := time.Now().Add(imagePollBudget)
lastStatus := submitData.Status
if lastStatus == "" {
lastStatus = "queued"
}
for time.Now().Before(deadline) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(c.pollInterval):
}
pollReq, err := http.NewRequestWithContext(ctx, "GET", pollURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create poll request: %w", err)
}
pollReq.Header.Set("PAYMENT-SIGNATURE", paymentPayload)
pollResp, err := c.httpClient.Do(pollReq)
if err != nil {
return nil, fmt.Errorf("poll request failed: %w", err)
}
pollBytes, _ := io.ReadAll(pollResp.Body)
pollResp.Body.Close()
var pollData map[string]any
_ = json.Unmarshal(pollBytes, &pollData)
if s, ok := pollData["status"].(string); ok && s != "" {
lastStatus = s
}
if pollResp.StatusCode == http.StatusAccepted && (lastStatus == "queued" || lastStatus == "in_progress") {
continue
}
if lastStatus == "failed" {
return nil, &APIError{
StatusCode: pollResp.StatusCode,
Message: fmt.Sprintf("Upstream generation failed (no payment was taken): %s", string(pollBytes)),
}
}
// Terminal success is keyed on status, NOT the HTTP code — the
// gateway settles on-chain the moment a poll reports "completed", so
// the charge is irreversible at that point. Record the cost as soon
// as completion is observed, then decode.
if lastStatus == "completed" {
c.recordSettledCost(paymentOption.Amount, endpoint)
return decodeImageResponse(pollBytes, pollResp.Header)
}
// 504 on a poll = transient upstream hiccup; keep polling. Any other
// non-2xx is a hard failure.
if pollResp.StatusCode != http.StatusOK &&
pollResp.StatusCode != http.StatusAccepted &&
pollResp.StatusCode != http.StatusGatewayTimeout {
return nil, &APIError{
StatusCode: pollResp.StatusCode,
Message: fmt.Sprintf("Poll failed: %s", string(pollBytes)),
}
}
}
return nil, &APIError{
StatusCode: http.StatusGatewayTimeout,
Message: fmt.Sprintf(
"Image generation did not complete within %.0fs (last status: %s). No payment was taken.",
imagePollBudget.Seconds(), lastStatus,
),
}
}
// decodeImageResponse unmarshals a gateway image payload (the synchronous
// shape or a completed poll, both carry data: [...]) and attaches the
// settlement receipt from the X-Payment-Receipt header when present.
func decodeImageResponse(body []byte, header http.Header) (*ImageResponse, error) {
var imageResp ImageResponse
if err := json.Unmarshal(body, &imageResp); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
if header != nil {
if tx := header.Get("x-payment-receipt"); tx != "" {
imageResp.TxHash = tx
}
}
return &imageResp, nil
}
// ListImageModels returns the list of available image models with pricing.
func (c *ImageClient) ListImageModels(ctx context.Context) ([]ImageModel, error) {
respBytes, err := c.doGet(ctx, "/v1/images/models")
if err != nil {
return nil, fmt.Errorf("failed to list image models: %w", err)
}
var result struct {
Data []ImageModel `json:"data"`
}
if err := json.Unmarshal(respBytes, &result); err != nil {
return nil, fmt.Errorf("failed to decode image models response: %w", err)
}
return result.Data, nil
}