Skip to content

Commit d78b1d0

Browse files
committed
kling
1 parent d666449 commit d78b1d0

2 files changed

Lines changed: 38 additions & 20 deletions

File tree

spec/geno/geno.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package geno
1919
import (
2020
"context"
2121
"encoding/json"
22+
"errors"
2223
"net/http"
2324

2425
xai "github.com/goplus/xai/spec"
@@ -244,7 +245,7 @@ type responseAdapter interface {
244245
Done(action xai.Action, body map[string]any) bool
245246
Sleep(action xai.Action, body map[string]any)
246247
Results(action xai.Action, body map[string]any) xai.Results
247-
QueryOpInfo(action xai.Action, body map[string]any) QueryOpInfo
248+
QueryOpInfo(action xai.Action, body map[string]any) (QueryOpInfo, error)
248249
}
249250

250251
type OperationResponse[T responseAdapter] struct {
@@ -266,12 +267,21 @@ func (p *OperationResponse[T]) Sleep() {
266267
p.adapter.Sleep(p.action, p.body)
267268
}
268269

270+
var (
271+
ErrMissingOperationID = errors.New("missing operation ID in response body")
272+
)
273+
269274
func (p *OperationResponse[T]) Retry(ctx context.Context, svc xai.Service, opts xai.OptionBuilder) (resp xai.OperationResponse, err error) {
270-
qoi := p.adapter.QueryOpInfo(p.action, p.body)
275+
qoi, err := p.adapter.QueryOpInfo(p.action, p.body)
276+
if err != nil {
277+
return
278+
}
271279
req, err := p.c.NewRequest(http.MethodGet, qoi.Path)
272280
if err != nil {
273281
return
274282
}
283+
// query operation has no body,
284+
// so we can directly call it without setting body
275285
return call(ctx, req, qoi.NewResponse, opts)
276286
}
277287

spec/kling/kling.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ import (
2929

3030
// -----------------------------------------------------------------------------
3131

32+
func newGenImageResponse(c *geno.Client, body map[string]any) (xai.OperationResponse, error) {
33+
return geno.NewOperationResponse[adapter](c, xai.GenImage, body), nil
34+
}
35+
36+
// -----------------------------------------------------------------------------
37+
3238
type adapter struct{}
3339

3440
func (adapter) Actions(model xai.Model) []xai.Action {
@@ -49,50 +55,52 @@ func (adapter) ActionInfo(action xai.Action) geno.ActionInfo {
4955
}
5056
}
5157

52-
func (adapter) QueryOpInfo(action xai.Action, body map[string]any) geno.QueryOpInfo {
58+
func (adapter) QueryOpInfo(action xai.Action, body map[string]any) (ret geno.QueryOpInfo, err error) {
5359
switch action {
5460
case xai.GenImage:
5561
data, _ := body["data"].(map[string]any)
56-
id, _ := data["task_id"].(string)
57-
return geno.QueryOpInfo{
58-
Path: "/v1/images/generations/" + id,
59-
NewResponse: newGenImageResponse,
62+
if id, ok := data["task_id"].(string); ok {
63+
ret.Path = "/v1/images/generations/" + id
64+
ret.NewResponse = newGenImageResponse
65+
} else {
66+
err = geno.ErrMissingOperationID
6067
}
6168
default:
6269
panic("unexpected action: " + action)
6370
}
71+
return
6472
}
6573

6674
func (adapter) Results(action xai.Action, body map[string]any) xai.Results {
67-
data, _ := body["task_result"].(map[string]any)
75+
result, _ := body["task_result"].(map[string]any)
6876
switch action {
6977
case xai.GenImage:
70-
return geno.NewImageResults[adapter](data, "images")
78+
return geno.NewImageResults[adapter](result, "images")
7179
default:
7280
panic("unexpected action: " + action)
7381
}
7482
}
7583

84+
func (adapter) Sleep(action xai.Action, body map[string]any) {
85+
switch action {
86+
case xai.GenVideo:
87+
// sleep 10s for video operations
88+
time.Sleep(10 * time.Second)
89+
default:
90+
// sleep 0.5s for image operations
91+
time.Sleep(time.Second / 2)
92+
}
93+
}
94+
7695
func (adapter) Done(action xai.Action, body map[string]any) bool {
7796
data, _ := body["data"].(map[string]any)
7897
return data["task_status"] == "succeed" // submitted, processing, succeed, failed
7998
}
8099

81-
func (adapter) Sleep(action xai.Action, body map[string]any) {
82-
if action == xai.GenVideo {
83-
time.Sleep(10 * time.Second) // sleep 10s for video operations
84-
}
85-
time.Sleep(time.Second / 2) // sleep 0.5s for image operations
86-
}
87-
88100
func (adapter) OutputImage(item any) *xai.OutputImage {
89101
panic("todo")
90102
}
91103

92-
func newGenImageResponse(c *geno.Client, body map[string]any) (xai.OperationResponse, error) {
93-
return geno.NewOperationResponse[adapter](c, xai.GenImage, body), nil
94-
}
95-
96104
// -----------------------------------------------------------------------------
97105

98106
const (

0 commit comments

Comments
 (0)