-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathpolling.go
More file actions
157 lines (142 loc) · 5.17 KB
/
Copy pathpolling.go
File metadata and controls
157 lines (142 loc) · 5.17 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
package openai
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"github.com/openai/openai-go/v3/internal/requestconfig"
"github.com/openai/openai-go/v3/option"
)
const maxPollInterval = time.Duration(1<<63 - 1)
func mkPollingOptions(pollIntervalMs int) []option.RequestOption {
options := []option.RequestOption{option.WithHeader("X-Stainless-Poll-Helper", "true")}
if pollIntervalMs > 0 {
options = append(options, option.WithHeader("X-Stainless-Poll-Interval", fmt.Sprintf("%d", pollIntervalMs)))
}
return options
}
func getPollInterval(raw *http.Response) time.Duration {
const defaultPollInterval = time.Second
if raw == nil {
return defaultPollInterval
}
ms, err := strconv.ParseInt(raw.Header.Get("openai-poll-after-ms"), 10, 64)
if err != nil {
if errors.Is(err, strconv.ErrRange) && ms > 0 {
return requestconfig.DefaultMaxServerDelay
}
return defaultPollInterval
}
if ms <= 0 {
return defaultPollInterval
}
maxServerMilliseconds := int64(requestconfig.DefaultMaxServerDelay / time.Millisecond)
return time.Duration(min(ms, maxServerMilliseconds)) * time.Millisecond
}
func pollInterval(pollIntervalMs int, raw *http.Response) time.Duration {
if pollIntervalMs <= 0 {
return getPollInterval(raw)
}
ms := int64(pollIntervalMs)
if ms > int64(maxPollInterval/time.Millisecond) {
return maxPollInterval
}
return time.Duration(ms) * time.Millisecond
}
// PollStatus waits until a VectorStoreFile is no longer in an incomplete state and returns it.
// Pass 0 as pollIntervalMs to use the default polling interval of 1 second.
// Server-suggested intervals are limited to 8 seconds; pass a positive value to
// explicitly use a longer interval.
func (r *VectorStoreFileService) PollStatus(ctx context.Context, vectorStoreID string, fileID string, pollIntervalMs int, opts ...option.RequestOption) (*VectorStoreFile, error) {
var raw *http.Response
var interval time.Duration
opts = append(opts, mkPollingOptions(pollIntervalMs)...)
opts = append(opts, option.WithResponseInto(&raw))
for {
file, err := r.Get(ctx, vectorStoreID, fileID, opts...)
if err != nil {
return nil, fmt.Errorf("vector store file poll: received %w", err)
}
switch file.Status {
case VectorStoreFileStatusInProgress:
if interval == 0 {
interval = pollInterval(pollIntervalMs, raw)
}
if err := requestconfig.WaitForDelay(ctx, interval); err != nil {
return nil, err
}
case VectorStoreFileStatusCancelled,
VectorStoreFileStatusCompleted,
VectorStoreFileStatusFailed:
return file, nil
default:
return nil, fmt.Errorf("invalid vector store file status during polling: received %s", file.Status)
}
}
}
// PollStatus waits until a BetaVectorStoreFileBatch is no longer in an incomplete state and returns it.
// Pass 0 as pollIntervalMs to use the default polling interval of 1 second.
// Server-suggested intervals are limited to 8 seconds; pass a positive value to
// explicitly use a longer interval.
func (r *VectorStoreFileBatchService) PollStatus(ctx context.Context, vectorStoreID string, batchID string, pollIntervalMs int, opts ...option.RequestOption) (*VectorStoreFileBatch, error) {
var raw *http.Response
var interval time.Duration
opts = append(opts, option.WithResponseInto(&raw))
opts = append(opts, mkPollingOptions(pollIntervalMs)...)
for {
batch, err := r.Get(ctx, vectorStoreID, batchID, opts...)
if err != nil {
return nil, fmt.Errorf("vector store file batch poll: received %w", err)
}
switch batch.Status {
case VectorStoreFileBatchStatusInProgress:
if interval == 0 {
interval = pollInterval(pollIntervalMs, raw)
}
if err := requestconfig.WaitForDelay(ctx, interval); err != nil {
return nil, err
}
case VectorStoreFileBatchStatusCancelled,
VectorStoreFileBatchStatusCompleted,
VectorStoreFileBatchStatusFailed:
return batch, nil
default:
return nil, fmt.Errorf("invalid vector store file batch status during polling: received %s", batch.Status)
}
}
}
// PollStatus waits until a VectorStoreFile is no longer in an incomplete state and returns it.
// Pass 0 as pollIntervalMs to use the default polling interval of 1 second.
// Server-suggested intervals are limited to 8 seconds; pass a positive value to
// explicitly use a longer interval.
//
// Deprecated: The Sora API is scheduled to permanently shut down on September 24,
// 2026.
func (r *VideoService) PollStatus(ctx context.Context, videoID string, pollIntervalMs int, opts ...option.RequestOption) (*Video, error) {
var raw *http.Response
var interval time.Duration
opts = append(opts, mkPollingOptions(pollIntervalMs)...)
opts = append(opts, option.WithResponseInto(&raw))
for {
video, err := r.Get(ctx, videoID, opts...)
if err != nil {
return nil, fmt.Errorf("error running video poll: received %w", err)
}
switch video.Status {
case VideoStatusQueued, VideoStatusInProgress:
if interval == 0 {
interval = pollInterval(pollIntervalMs, raw)
}
if err := requestconfig.WaitForDelay(ctx, interval); err != nil {
return nil, err
}
case VideoStatusCompleted,
VideoStatusFailed:
return video, nil
default:
return nil, fmt.Errorf("invalid video status during polling: received %s", video.Status)
}
}
}