From 54e6b87c9135c368b7ed612ee7d66c46ff945cb5 Mon Sep 17 00:00:00 2001 From: Savas Neto Date: Tue, 3 Nov 2026 19:44:00 -0300 Subject: [PATCH 1/3] feat(providers): offer a Verboo model when the user's own account runs out MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a model reached with the user's own account stops because the plan or balance ran out (ChatGPT plan via Codex "usage_limit_reached", OpenAI "insufficient_quota", Anthropic "credit balance is too low"), the error message says so — with the renewal time when the provider gives it — and the REPL opens a dialog right away offering a Verboo model, last used first. Choosing one switches the session model and resends the last prompt; "keep" leaves everything as it was. Temporary rate limits keep their normal retry, and the ChatGPT usage limit is no longer retried. Co-Authored-By: Claude Opus 5.5 --- src/commands.ts | 2 + src/commands/limit-switch/index.ts | 11 ++ .../limit-switch/limit-switch.test.ts | 39 +++++++ src/commands/limit-switch/limit-switch.tsx | 87 ++++++++++++++ src/screens/REPL.tsx | 20 ++++ src/services/api/errors.ts | 17 +++ src/services/api/providerLimitErrors.test.ts | 73 ++++++++++++ src/services/api/providerLimitErrors.ts | 108 ++++++++++++++++++ src/services/api/withRetry.ts | 5 +- 9 files changed, 361 insertions(+), 1 deletion(-) create mode 100644 src/commands/limit-switch/index.ts create mode 100644 src/commands/limit-switch/limit-switch.test.ts create mode 100644 src/commands/limit-switch/limit-switch.tsx create mode 100644 src/services/api/providerLimitErrors.test.ts create mode 100644 src/services/api/providerLimitErrors.ts diff --git a/src/commands.ts b/src/commands.ts index a5d3bb3d72..c7f740a122 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -17,6 +17,7 @@ import compact from './commands/compact/index.js' import codex from './commands/codex/index.js' import claude from './commands/claude/index.js' import openai from './commands/openai/index.js' +import limitSwitch from './commands/limit-switch/index.js' import config from './commands/config/index.js' import { context, contextNonInteractive } from './commands/context/index.js' import cost from './commands/cost/index.js' @@ -293,6 +294,7 @@ const COMMANDS = memoize((): Command[] => [ compact, claude, openai, + limitSwitch, codex, commitMessage, config, diff --git a/src/commands/limit-switch/index.ts b/src/commands/limit-switch/index.ts new file mode 100644 index 0000000000..b542497fa9 --- /dev/null +++ b/src/commands/limit-switch/index.ts @@ -0,0 +1,11 @@ +import type { Command } from '../../commands.js' + +// Opened by the REPL when the user's own account (ChatGPT plan, OpenAI or +// Anthropic key) runs out; offers to continue on a Verboo model. +export default { + type: 'local-jsx', + name: 'limit-switch', + description: 'Trocar para um modelo Verboo quando o limite da sua conta acaba', + isHidden: true, + load: () => import('./limit-switch.js'), +} satisfies Command diff --git a/src/commands/limit-switch/limit-switch.test.ts b/src/commands/limit-switch/limit-switch.test.ts new file mode 100644 index 0000000000..927f6b0b5c --- /dev/null +++ b/src/commands/limit-switch/limit-switch.test.ts @@ -0,0 +1,39 @@ +import { expect, test } from 'bun:test' + +import { lastUserPromptText, limitSwitchModelOptions } from './limit-switch.js' + +test('offers Verboo models, last used first, plus keeping the current one', () => { + const options = limitSwitchModelOptions( + [ + { id: 'ultra/a', displayName: 'A' }, + { id: 'ultra/b', displayName: 'B' }, + { id: 'ultra/c' }, + ], + 'ultra/b', + ) + expect(options.map(option => option.value)).toEqual([ + 'ultra/b', + 'ultra/a', + 'ultra/c', + '__keep_current_model__', + ]) + expect(options[2]?.label).toBe('ultra/c') +}) + +test('caps the list at five models', () => { + const models = Array.from({ length: 8 }, (_, i) => ({ id: `m${i}` })) + expect(limitSwitchModelOptions(models, undefined)).toHaveLength(6) +}) + +test('finds the last prompt the user typed, skipping meta and tool results', () => { + const messages = [ + { type: 'user', message: { content: 'first request' } }, + { type: 'assistant', message: { content: [{ type: 'text', text: 'ok' }] } }, + { type: 'user', message: { content: 'refactor the parser' } }, + { type: 'user', isMeta: true, message: { content: 'system reminder' } }, + { type: 'user', message: { content: [{ type: 'tool_result', tool_use_id: 't', content: 'out' }] } }, + { type: 'assistant', isApiErrorMessage: true, message: { content: [{ type: 'text', text: 'limit' }] } }, + ] as never + expect(lastUserPromptText(messages)).toBe('refactor the parser') + expect(lastUserPromptText([] as never)).toBeNull() +}) diff --git a/src/commands/limit-switch/limit-switch.tsx b/src/commands/limit-switch/limit-switch.tsx new file mode 100644 index 0000000000..16a71eb707 --- /dev/null +++ b/src/commands/limit-switch/limit-switch.tsx @@ -0,0 +1,87 @@ +import * as React from 'react' + +import { Select } from '../../components/CustomSelect/select.js' +import { Dialog } from '../../components/design-system/Dialog.js' +import { Box, Text } from '../../ink.js' +import { getCachedVerbooModels } from '../../services/api/verbooModels.js' +import type { LocalJSXCommandCall } from '../../types/command.js' +import type { Message, UserMessage } from '../../types/message.js' +import { getGlobalConfig } from '../../utils/config.js' +import { textForResubmit } from '../../utils/messages.js' +import { saveLastVerbooModel } from '../../utils/model/model.js' + +const KEEP = '__keep_current_model__' +const MAX_MODELS = 5 + +/** Verboo models to offer, the last Verboo model used first. */ +export function limitSwitchModelOptions( + models: ReadonlyArray<{ id: string; displayName?: string }>, + lastVerbooModel: string | undefined, +): Array<{ label: string; value: string }> { + const ordered = [...models].sort((a, b) => + a.id === lastVerbooModel ? -1 : b.id === lastVerbooModel ? 1 : 0, + ) + return [ + ...ordered.slice(0, MAX_MODELS).map(model => ({ + label: model.displayName ?? model.id, + value: model.id, + })), + { label: 'Manter o modelo atual', value: KEEP }, + ] +} + +/** Text of the last prompt the user typed, to send again on the new model. */ +export function lastUserPromptText(messages: readonly Message[]): string | null { + for (let i = messages.length - 1; i >= 0; i--) { + const message = messages[i]! + if (message.type !== 'user' || message.isMeta) continue + const resubmit = textForResubmit(message as UserMessage) + if (resubmit?.mode === 'prompt' && resubmit.text.trim()) return resubmit.text + } + return null +} + +export const call: LocalJSXCommandCall = async (onDone, context, args) => { + const models = getCachedVerbooModels() ?? [] + if (models.length === 0) { + onDone('Nenhum modelo Verboo disponível na sua conta. Use /model para ver as opções.', { + display: 'system', + }) + return + } + const reason = args.trim() || 'o limite da sua conta acabou.' + const options = limitSwitchModelOptions(models, getGlobalConfig().lastVerbooModel) + const prompt = lastUserPromptText(context.messages) + const keep = () => + onDone('Modelo mantido. Use /model quando quiser trocar.', { display: 'system' }) + + return ( + + + {reason.charAt(0).toUpperCase() + reason.slice(1)} + Trocar para um modelo Verboo?{prompt ? ' Sua última mensagem será reenviada.' : ''} + (value === KEEP ? onKeep() : onSwitch(value))} onCancel={onKeep} /> diff --git a/src/screens/REPL.tsx b/src/screens/REPL.tsx index 4cc5e8e797..ce1467b6c7 100644 --- a/src/screens/REPL.tsx +++ b/src/screens/REPL.tsx @@ -580,6 +580,12 @@ export type Props = { thinkingConfig: ThinkingConfig; }; export type Screen = 'prompt' | 'transcript'; +/** What ran out, when this message is the provider-limit error of a user's own account. */ +function providerLimitReason(message: MessageType): string | null { + if (message.type !== 'assistant' || !message.isApiErrorMessage) return null; + const block = message.message.content[0]; + return limitSwitchReasonFromMessage(block && block.type === 'text' ? block.text : ''); +} export function REPL({ commands: initialCommands, debug, @@ -3820,10 +3826,8 @@ export function REPL({ // submitted as a command: the prompt draft, history and transcript stay intact. useEffect(() => { const last = messages.at(-1); - if (!last || last.type !== 'assistant' || !last.isApiErrorMessage) return; - if (handledProviderLimitRef.current === last.uuid) return; - const block = last.message.content[0]; - const reason = limitSwitchReasonFromMessage(block && block.type === 'text' ? block.text : ''); + if (!last || handledProviderLimitRef.current === last.uuid) return; + const reason = providerLimitReason(last); if (reason === null) return; handledProviderLimitRef.current = last.uuid; const models = getCachedVerbooModels() ?? []; @@ -4130,9 +4134,13 @@ export function REPL({ queuedCommands }); }, [queryGuard, commands, setToolJSX, getToolUseContext, messages, mainLoopModel, ideSelection, setUserInputOnProcessing, canUseTool, setAbortController, onQuery, addNotification, setAppState, onBeforeQuery, awaitPendingHooks]); + // VERBOO-BRAND: a queued message waits for the choice on the limit dialog; it + // must not hit the account that just ran out while the dialog is opening. + const lastMessage = messages.at(-1); + const isLimitSwitchPending = limitResendPrompt !== null || (lastMessage !== undefined && handledProviderLimitRef.current !== lastMessage.uuid && providerLimitReason(lastMessage) !== null); useQueueProcessor({ executeQueuedInput, - hasActiveLocalJsxUI: isShowingLocalJSXCommand, + hasActiveLocalJsxUI: isShowingLocalJSXCommand || isLimitSwitchPending, queryGuard });