GeezerVison is showing github.com — fix: adjust tui retry dialog logic to be more provider specific and e… · jarkkojs/opencode@799996d
File treeExpand file treeCollapse file tree Open diff view settings cli/cmd/tui/routes/session
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/routes/session/index.tsx +29-6Lines changed: 29 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -93,9 +93,29 @@ import { useBindings, useCommandShortcut } from "../../keymap" | 93 | 93 |
| 94 | 94 | addDefaultParsers(parsers.parsers) | 95 | 95 |
| 96 | | -const GO_UPSELL_LAST_SEEN_AT = "go_upsell_last_seen_at" | 97 | | -const GO_UPSELL_DONT_SHOW = "go_upsell_dont_show" | | 96 | +const GO_UPSELL_FREE_TIER_LAST_SEEN_AT = "go_upsell_last_seen_at" | | 97 | +const GO_UPSELL_FREE_TIER_DONT_SHOW = "go_upsell_dont_show" | | 98 | +const GO_UPSELL_ACCOUNT_RATE_LIMIT_LAST_SEEN_AT = "go_upsell_account_rate_limit_last_seen_at" | | 99 | +const GO_UPSELL_ACCOUNT_RATE_LIMIT_DONT_SHOW = "go_upsell_account_rate_limit_dont_show" | 98 | 100 | const GO_UPSELL_WINDOW = 86_400_000 // 24 hrs | | 101 | +const GO_UPSELL_PROVIDERS = new Set(["opencode", "opencode-go"]) | | 102 | + | | 103 | +function goUpsellKeys(action: SessionRetry.Retryable["action"]) { | | 104 | + if (!action) return | | 105 | + if (!GO_UPSELL_PROVIDERS.has(action.provider)) return | | 106 | + if (action.reason === "free_tier_limit") { | | 107 | + return { | | 108 | + lastSeenAt: GO_UPSELL_FREE_TIER_LAST_SEEN_AT, | | 109 | + dontShow: GO_UPSELL_FREE_TIER_DONT_SHOW, | | 110 | + } | | 111 | + } | | 112 | + if (action.reason === "account_rate_limit") { | | 113 | + return { | | 114 | + lastSeenAt: GO_UPSELL_ACCOUNT_RATE_LIMIT_LAST_SEEN_AT, | | 115 | + dontShow: GO_UPSELL_ACCOUNT_RATE_LIMIT_DONT_SHOW, | | 116 | + } | | 117 | + } | | 118 | +} | 99 | 119 |
| 100 | 120 | const context = createContext<{ | 101 | 121 | width: number | @@ -263,14 +283,17 @@ export function Session() { | 263 | 283 | if (!evt.properties.status.action) return | 264 | 284 | if (dialog.stack.length > 0) return | 265 | 285 |
| 266 | | - const seen = kv.get(GO_UPSELL_LAST_SEEN_AT) | | 286 | + const keys = goUpsellKeys(evt.properties.status.action) | | 287 | + if (!keys) return | | 288 | + | | 289 | + const seen = kv.get(keys.lastSeenAt) | 267 | 290 | if (typeof seen === "number" && Date.now() - seen < GO_UPSELL_WINDOW) return | 268 | 291 |
| 269 | | - if (kv.get(GO_UPSELL_DONT_SHOW)) return | | 292 | + if (kv.get(keys.dontShow)) return | 270 | 293 |
| 271 | 294 | void DialogRetryAction.show(dialog, evt.properties.status.action).then((dontShowAgain) => { | 272 | | - if (dontShowAgain) kv.set(GO_UPSELL_DONT_SHOW, true) | 273 | | - kv.set(GO_UPSELL_LAST_SEEN_AT, Date.now()) | | 295 | + if (dontShowAgain) kv.set(keys.dontShow, true) | | 296 | + kv.set(keys.lastSeenAt, Date.now()) | 274 | 297 | }) | 275 | 298 | }) | 276 | 299 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/processor.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -701,6 +701,7 @@ export const layer: Layer.Layer< | 701 | 701 | ), | 702 | 702 | Effect.retry( | 703 | 703 | SessionRetry.policy({ | | 704 | + provider: input.model.providerID, | 704 | 705 | parse, | 705 | 706 | set: (info) => { | 706 | 707 | // TODO(v2): Temporary dual-write while migrating session messages to v2 events. | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/retry.ts +11-3Lines changed: 11 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,10 +7,13 @@ export type Err = ReturnType<NamedError["toObject"]> | 7 | 7 |
| 8 | 8 | export const GO_UPSELL_MESSAGE = "Free usage exceeded, subscribe to Go" | 9 | 9 | export const GO_UPSELL_URL = "https://opencode.ai/go" | | 10 | +export type RetryReason = "free_tier_limit" | "account_rate_limit" | (string & {}) | 10 | 11 |
| 11 | 12 | export type Retryable = { | 12 | 13 | message: string | 13 | 14 | action?: { | | 15 | + reason: RetryReason | | 16 | + provider: string | 14 | 17 | title: string | 15 | 18 | message: string | 16 | 19 | label: string | @@ -60,7 +63,7 @@ export function delay(attempt: number, error?: MessageV2.APIError) { | 60 | 63 | return cap(Math.min(RETRY_INITIAL_DELAY * Math.pow(RETRY_BACKOFF_FACTOR, attempt - 1), RETRY_MAX_DELAY_NO_HEADERS)) | 61 | 64 | } | 62 | 65 |
| 63 | | -export function retryable(error: Err) { | | 66 | +export function retryable(error: Err, provider: string) { | 64 | 67 | // context overflow errors should not be retried | 65 | 68 | if (MessageV2.ContextOverflowError.isInstance(error)) return undefined | 66 | 69 | if (MessageV2.APIError.isInstance(error)) { | @@ -72,6 +75,8 @@ export function retryable(error: Err) { | 72 | 75 | return { | 73 | 76 | message: GO_UPSELL_MESSAGE, | 74 | 77 | action: { | | 78 | + reason: "free_tier_limit", | | 79 | + provider, | 75 | 80 | title: "Free limit reached", | 76 | 81 | message: "Subscribe to OpenCode Go for reliable access to the best open-source models, starting at $5/month.", | 77 | 82 | label: "subscribe", | @@ -97,12 +102,14 @@ export function retryable(error: Err) { | 97 | 102 | return minutes > 0 ? unit(minutes, "minute") : "less than a minute" | 98 | 103 | }) | 99 | 104 |
| 100 | | - const message = `${limitName} usage limit reached. It will reset in ${resetIn}. To continue using this model now, enable usage from your available balance` | | 105 | + const message = `${limitName ? `${limitName} usage limit` : "Usage limit"} reached. It will reset in ${resetIn}. To continue using this model now, enable usage from your available balance` | 101 | 106 |
| 102 | 107 | const link = `https://opencode.ai/workspace/${workspace}/go` | 103 | 108 | return { | 104 | 109 | message: `${message} - ${link}`, | 105 | 110 | action: { | | 111 | + reason: "account_rate_limit", | | 112 | + provider, | 106 | 113 | title: "Go limit reached", | 107 | 114 | message, | 108 | 115 | label: "open settings", | @@ -165,13 +172,14 @@ function parseJSON(value: unknown) { | 165 | 172 | } | 166 | 173 |
| 167 | 174 | export function policy(opts: { | | 175 | + provider: string | 168 | 176 | parse: (error: unknown) => Err | 169 | 177 | set: (input: { attempt: number; message: string; action?: Retryable["action"]; next: number }) => Effect.Effect<void> | 170 | 178 | }) { | 171 | 179 | return Schedule.fromStepWithMetadata( | 172 | 180 | Effect.succeed((meta: Schedule.InputMetadata<unknown>) => { | 173 | 181 | const error = opts.parse(meta.input) | 174 | | - const retry = retryable(error) | | 182 | + const retry = retryable(error, opts.provider) | 175 | 183 | if (!retry) return Cause.done(meta.attempt) | 176 | 184 | return Effect.gen(function* () { | 177 | 185 | const wait = delay(meta.attempt, MessageV2.APIError.isInstance(error) ? error : undefined) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/status.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,6 +17,8 @@ export const Info = Schema.Union([ | 17 | 17 | message: Schema.String, | 18 | 18 | action: Schema.optional( | 19 | 19 | Schema.Struct({ | | 20 | + reason: Schema.String, | | 21 | + provider: Schema.String, | 20 | 22 | title: Schema.String, | 21 | 23 | message: Schema.String, | 22 | 24 | label: Schema.String, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/session/retry.test.ts +51-18Lines changed: 51 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ import { provideTmpdirInstance } from "../fixture/fixture" | 13 | 13 | import { testEffect } from "../lib/effect" | 14 | 14 |
| 15 | 15 | const providerID = ProviderID.make("test") | | 16 | +const retryProvider = "test" | 16 | 17 | const it = testEffect(Layer.mergeAll(SessionStatus.defaultLayer, CrossSpawnSpawner.defaultLayer)) | 17 | 18 |
| 18 | 19 | function apiError(headers?: Record<string, string>): MessageV2.APIError { | @@ -92,6 +93,7 @@ describe("session.retry.delay", () => { | 92 | 93 |
| 93 | 94 | const step = yield* Schedule.toStepWithMetadata( | 94 | 95 | SessionRetry.policy({ | | 96 | + provider: "test", | 95 | 97 | parse: (err) => MessageV2.APIError.Schema.parse(err), | 96 | 98 | set: (info) => | 97 | 99 | status.set(sessionID, { | @@ -118,47 +120,47 @@ describe("session.retry.delay", () => { | 118 | 120 | describe("session.retry.retryable", () => { | 119 | 121 | test("maps too_many_requests json messages", () => { | 120 | 122 | const error = wrap(JSON.stringify({ type: "error", error: { type: "too_many_requests" } })) | 121 | | - expect(SessionRetry.retryable(error)).toEqual({ message: "Too Many Requests" }) | | 123 | + expect(SessionRetry.retryable(error, retryProvider)).toEqual({ message: "Too Many Requests" }) | 122 | 124 | }) | 123 | 125 |
| 124 | 126 | test("maps overloaded provider codes", () => { | 125 | 127 | const error = wrap(JSON.stringify({ code: "resource_exhausted" })) | 126 | | - expect(SessionRetry.retryable(error)).toEqual({ message: "Provider is overloaded" }) | | 128 | + expect(SessionRetry.retryable(error, retryProvider)).toEqual({ message: "Provider is overloaded" }) | 127 | 129 | }) | 128 | 130 |
| 129 | 131 | test("does not retry unknown json messages", () => { | 130 | 132 | const error = wrap(JSON.stringify({ error: { message: "no_kv_space" } })) | 131 | | - expect(SessionRetry.retryable(error)).toBeUndefined() | | 133 | + expect(SessionRetry.retryable(error, retryProvider)).toBeUndefined() | 132 | 134 | }) | 133 | 135 |
| 134 | 136 | test("does not throw on numeric error codes", () => { | 135 | 137 | const error = wrap(JSON.stringify({ type: "error", error: { code: 123 } })) | 136 | | - const result = SessionRetry.retryable(error) | | 138 | + const result = SessionRetry.retryable(error, retryProvider) | 137 | 139 | expect(result).toBeUndefined() | 138 | 140 | }) | 139 | 141 |
| 140 | 142 | test("returns undefined for non-json message", () => { | 141 | 143 | const error = wrap("not-json") | 142 | | - expect(SessionRetry.retryable(error)).toBeUndefined() | | 144 | + expect(SessionRetry.retryable(error, retryProvider)).toBeUndefined() | 143 | 145 | }) | 144 | 146 |
| 145 | 147 | test("retries plain text rate limit errors from Alibaba", () => { | 146 | 148 | const msg = | 147 | 149 | "Upstream error from Alibaba: Request rate increased too quickly. To ensure system stability, please adjust your client logic to scale requests more smoothly over time." | 148 | 150 | const error = wrap(msg) | 149 | | - expect(SessionRetry.retryable(error)).toEqual({ message: msg }) | | 151 | + expect(SessionRetry.retryable(error, retryProvider)).toEqual({ message: msg }) | 150 | 152 | }) | 151 | 153 |
| 152 | 154 | test("retries plain text rate limit errors", () => { | 153 | 155 | const msg = "Rate limit exceeded, please try again later" | 154 | 156 | const error = wrap(msg) | 155 | | - expect(SessionRetry.retryable(error)).toEqual({ message: msg }) | | 157 | + expect(SessionRetry.retryable(error, retryProvider)).toEqual({ message: msg }) | 156 | 158 | }) | 157 | 159 |
| 158 | 160 | test("retries too many requests in plain text", () => { | 159 | 161 | const msg = "Too many requests, please slow down" | 160 | 162 | const error = wrap(msg) | 161 | | - expect(SessionRetry.retryable(error)).toEqual({ message: msg }) | | 163 | + expect(SessionRetry.retryable(error, retryProvider)).toEqual({ message: msg }) | 162 | 164 | }) | 163 | 165 |
| 164 | 166 | test("does not retry context overflow errors", () => { | @@ -167,7 +169,7 @@ describe("session.retry.retryable", () => { | 167 | 169 | responseBody: '{"error":{"code":"context_length_exceeded"}}', | 168 | 170 | }).toObject() | 169 | 171 |
| 170 | | - expect(SessionRetry.retryable(error)).toBeUndefined() | | 172 | + expect(SessionRetry.retryable(error, retryProvider)).toBeUndefined() | 171 | 173 | }) | 172 | 174 |
| 173 | 175 | test("retries 500 errors even when isRetryable is false", () => { | @@ -180,7 +182,7 @@ describe("session.retry.retryable", () => { | 180 | 182 | }).toObject(), | 181 | 183 | ) | 182 | 184 |
| 183 | | - expect(SessionRetry.retryable(error)).toEqual({ message: "Internal server error" }) | | 185 | + expect(SessionRetry.retryable(error, retryProvider)).toEqual({ message: "Internal server error" }) | 184 | 186 | }) | 185 | 187 |
| 186 | 188 | test("retries 502 bad gateway errors", () => { | @@ -192,7 +194,7 @@ describe("session.retry.retryable", () => { | 192 | 194 | }).toObject(), | 193 | 195 | ) | 194 | 196 |
| 195 | | - expect(SessionRetry.retryable(error)).toEqual({ message: "Bad gateway" }) | | 197 | + expect(SessionRetry.retryable(error, retryProvider)).toEqual({ message: "Bad gateway" }) | 196 | 198 | }) | 197 | 199 |
| 198 | 200 | test("retries 503 service unavailable errors", () => { | @@ -204,7 +206,7 @@ describe("session.retry.retryable", () => { | 204 | 206 | }).toObject(), | 205 | 207 | ) | 206 | 208 |
| 207 | | - expect(SessionRetry.retryable(error)).toEqual({ message: "Service unavailable" }) | | 209 | + expect(SessionRetry.retryable(error, retryProvider)).toEqual({ message: "Service unavailable" }) | 208 | 210 | }) | 209 | 211 |
| 210 | 212 | test("does not retry 4xx errors when isRetryable is false", () => { | @@ -216,7 +218,7 @@ describe("session.retry.retryable", () => { | 216 | 218 | }).toObject(), | 217 | 219 | ) | 218 | 220 |
| 219 | | - expect(SessionRetry.retryable(error)).toBeUndefined() | | 221 | + expect(SessionRetry.retryable(error, retryProvider)).toBeUndefined() | 220 | 222 | }) | 221 | 223 |
| 222 | 224 | test("retries ZlibError decompression failures", () => { | @@ -228,7 +230,7 @@ describe("session.retry.retryable", () => { | 228 | 230 | }).toObject(), | 229 | 231 | ) | 230 | 232 |
| 231 | | - const retryable = SessionRetry.retryable(error) | | 233 | + const retryable = SessionRetry.retryable(error, retryProvider) | 232 | 234 | expect(retryable).toBeDefined() | 233 | 235 | expect(retryable).toEqual({ message: "Response decompression failed" }) | 234 | 236 | }) | @@ -246,9 +248,11 @@ describe("session.retry.retryable", () => { | 246 | 248 | }).toObject(), | 247 | 249 | ) | 248 | 250 |
| 249 | | - expect(SessionRetry.retryable(error)).toEqual({ | | 251 | + expect(SessionRetry.retryable(error, "opencode")).toEqual({ | 250 | 252 | message: SessionRetry.GO_UPSELL_MESSAGE, | 251 | 253 | action: { | | 254 | + reason: "free_tier_limit", | | 255 | + provider: "opencode", | 252 | 256 | title: "Free limit reached", | 253 | 257 | message: "Subscribe to OpenCode Go for reliable access to the best open-source models, starting at $5/month.", | 254 | 258 | label: "subscribe", | @@ -280,10 +284,12 @@ describe("session.retry.retryable", () => { | 280 | 284 | }).toObject(), | 281 | 285 | ) | 282 | 286 |
| 283 | | - expect(SessionRetry.retryable(error)).toEqual({ | | 287 | + expect(SessionRetry.retryable(error, "opencode-go")).toEqual({ | 284 | 288 | message: | 285 | 289 | "5 hour usage limit reached. It will reset in 5 hours 23 minutes. To continue using this model now, enable usage from your available balance - https://opencode.ai/workspace/wrk_01K6XGM22R6FM8JVABE9XDQXGH/go", | 286 | 290 | action: { | | 291 | + reason: "account_rate_limit", | | 292 | + provider: "opencode-go", | 287 | 293 | title: "Go limit reached", | 288 | 294 | message: | 289 | 295 | "5 hour usage limit reached. It will reset in 5 hours 23 minutes. To continue using this model now, enable usage from your available balance", | @@ -292,6 +298,33 @@ describe("session.retry.retryable", () => { | 292 | 298 | }, | 293 | 299 | }) | 294 | 300 | }) | | 301 | + | | 302 | + test("maps Go subscription limits without limit metadata", () => { | | 303 | + const error = MessageV2.APIError.Schema.parse( | | 304 | + new MessageV2.APIError({ | | 305 | + message: "Subscription quota exceeded. You can continue using free models.", | | 306 | + isRetryable: true, | | 307 | + statusCode: 429, | | 308 | + responseHeaders: { | | 309 | + "retry-after": "900", | | 310 | + }, | | 311 | + responseBody: JSON.stringify({ | | 312 | + type: "error", | | 313 | + error: { | | 314 | + type: "GoUsageLimitError", | | 315 | + message: "Subscription quota exceeded. You can continue using free models.", | | 316 | + }, | | 317 | + metadata: { | | 318 | + workspace: "wrk_01K6XGM22R6FM8JVABE9XDQXGH", | | 319 | + }, | | 320 | + }), | | 321 | + }).toObject(), | | 322 | + ) | | 323 | + | | 324 | + expect(SessionRetry.retryable(error, "opencode-go")?.action?.message).toBe( | | 325 | + "Usage limit reached. It will reset in 15 minutes. To continue using this model now, enable usage from your available balance", | | 326 | + ) | | 327 | + }) | 295 | 328 | }) | 296 | 329 |
| 297 | 330 | describe("session.message-v2.fromError", () => { | @@ -341,7 +374,7 @@ describe("session.message-v2.fromError", () => { | 341 | 374 | }).toObject(), | 342 | 375 | ) | 343 | 376 |
| 344 | | - const retryable = SessionRetry.retryable(error) | | 377 | + const retryable = SessionRetry.retryable(error, retryProvider) | 345 | 378 | expect(retryable).toBeDefined() | 346 | 379 | expect(retryable).toEqual({ message: "Connection reset by server" }) | 347 | 380 | }) | @@ -381,6 +414,6 @@ describe("session.message-v2.fromError", () => { | 381 | 414 | expect(MessageV2.APIError.isInstance(result)).toBe(true) | 382 | 415 | if (!MessageV2.APIError.isInstance(result)) throw new Error("expected APIError") | 383 | 416 | expect(result.data.isRetryable).toBe(true) | 384 | | - expect(SessionRetry.retryable(result)).toEqual({ message: "An error occurred while processing your request." }) | | 417 | + expect(SessionRetry.retryable(result, retryProvider)).toEqual({ message: "An error occurred while processing your request." }) | 385 | 418 | }) | 386 | 419 | }) |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/session/schema-decoding.test.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -236,6 +236,8 @@ describe("SessionStatus.Info", () => { | 236 | 236 | attempt: 1, | 237 | 237 | message: "transient", | 238 | 238 | action: { | | 239 | + reason: "free_tier_limit", | | 240 | + provider: "opencode", | 239 | 241 | title: "Free limit reached", | 240 | 242 | message: "Subscribe to OpenCode Go.", | 241 | 243 | label: "subscribe", | |
Collapse file Copy file name to clipboardExpand all lines: packages/sdk/js/src/v2/gen/types.gen.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -267,6 +267,8 @@ export type SessionStatus = | 267 | 267 | attempt: number | 268 | 268 | message: string | 269 | 269 | action?: { | | 270 | + reason: string | | 271 | + provider: string | 270 | 272 | title: string | 271 | 273 | message: string | 272 | 274 | label: string | |
Collapse file Copy file name to clipboardExpand all lines: script/zen-limit-server.ts -37Lines changed: 0 additions & 37 deletions
|