MCPcopy Create free account
hub / github.com/anomalyco/opencode / delay

Function delay

packages/opencode/src/session/retry.ts:35–66  ·  view source on GitHub ↗
(attempt: number, error?: SessionV1.APIError)

Source from the content-addressed store, hash-verified

33}
34
35export function delay(attempt: number, error?: SessionV1.APIError) {
36 if (error) {
37 const headers = error.data.responseHeaders
38 if (headers) {
39 const retryAfterMs = headers["retry-after-ms"]
40 if (retryAfterMs) {
41 const parsedMs = Number.parseFloat(retryAfterMs)
42 if (!Number.isNaN(parsedMs)) {
43 return cap(parsedMs)
44 }
45 }
46
47 const retryAfter = headers["retry-after"]
48 if (retryAfter) {
49 const parsedSeconds = Number.parseFloat(retryAfter)
50 if (!Number.isNaN(parsedSeconds)) {
51 // convert seconds to milliseconds
52 return cap(Math.ceil(parsedSeconds * 1000))
53 }
54 // Try parsing as HTTP date format
55 const parsed = Date.parse(retryAfter) - Date.now()
56 if (!Number.isNaN(parsed) && parsed > 0) {
57 return cap(Math.ceil(parsed))
58 }
59 }
60
61 return cap(RETRY_INITIAL_DELAY * Math.pow(RETRY_BACKOFF_FACTOR, attempt - 1))
62 }
63 }
64
65 return cap(Math.min(RETRY_INITIAL_DELAY * Math.pow(RETRY_BACKOFF_FACTOR, attempt - 1), RETRY_MAX_DELAY_NO_HEADERS))
66}
67
68export function retryable(error: Err, provider: string) {
69 // context overflow errors should not be retried

Callers 1

policyFunction · 0.70

Calls 1

capFunction · 0.85

Tested by

no test coverage detected