Skip to content
Open
78 changes: 78 additions & 0 deletions src/components/LimitSwitchDialog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { expect, test } from 'bun:test'

import { describeFailedTurn, limitSwitchDoneMessage, limitSwitchModelOptions } from './LimitSwitchDialog.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)
})

const limitError = { type: 'assistant', isApiErrorMessage: true, message: { content: [{ type: 'text', text: 'limit' }] } }
const reply = { type: 'assistant', message: { content: [{ type: 'text', text: 'ok' }] } }

test('a turn where nothing ran can be sent again', () => {
const turn = describeFailedTurn([
{ type: 'user', uuid: 'old', message: { content: 'first request' } },
reply,
{ type: 'user', uuid: 'new', message: { content: 'refactor the parser' } },
{ type: 'user', isMeta: true, message: { content: 'system reminder' } },
limitError,
] as never)
expect(turn).toEqual({ prompt: 'refactor the parser', promptUuid: 'new', toolsRan: false })
expect(describeFailedTurn([] as never)).toEqual({ prompt: null, toolsRan: false })
})

test('a turn that already ran tools is reported, so it is not repeated', () => {
const turn = describeFailedTurn([
{ type: 'user', uuid: 'new', message: { content: 'append to the file' } },
{ type: 'assistant', message: { content: [{ type: 'tool_use', id: 't', name: 'Bash', input: {} }] } },
{ type: 'user', message: { content: [{ type: 'tool_result', tool_use_id: 't', content: 'out' }] } },
limitError,
] as never)
expect(turn.toolsRan).toBe(true)
expect(limitSwitchDoneMessage('m', turn)).toContain('nada foi reenviado')
})

test('the compaction summary is not mistaken for what the user typed', () => {
const turn = describeFailedTurn([
{ type: 'user', uuid: 'typed', message: { content: 'next step' } },
{ type: 'system', subtype: 'compact_boundary' },
{ type: 'user', isCompactSummary: true, isVisibleInTranscriptOnly: true, message: { content: 'This session is being continued' } },
limitError,
] as never)
expect(turn.prompt).toBe('next step')
})

test('a turn not started by typed text never falls back to an older prompt', () => {
for (const content of [
[{ type: 'image', source: {} }, { type: 'text', text: 'see this' }],
'<local-command-stdout>done</local-command-stdout>',
'<task-notification>agent finished</task-notification>',
]) {
const turn = describeFailedTurn([
{ type: 'user', uuid: 'old', message: { content: 'older prompt' } },
reply,
{ type: 'user', uuid: 'new', message: { content } },
limitError,
] as never)
expect(turn.prompt).toBeNull()
}
})
128 changes: 128 additions & 0 deletions src/components/LimitSwitchDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import * as React from 'react'

import { getUserMessageText, textForResubmit } from '../utils/messages.js'
import type { Message, UserMessage } from '../types/message.js'
import { Select } from './CustomSelect/select.js'
import { Dialog } from './design-system/Dialog.js'
import { Box, Text } from '../ink.js'

const KEEP = '__keep_current_model__'
const MAX_MODELS = 5
// Text the CLI itself placed in a user message; the user did not type it.
const NOT_TYPED = /^\s*<(local-command-|bash-|task-notification)/

/** Verboo models to offer, the last Verboo model used first. */
export function limitSwitchModelOptions(
models: ReadonlyArray<{ id: string; displayName?: string }>,
lastRouterModel: string | undefined,
): Array<{ label: string; value: string }> {
const ordered = [...models].sort((a, b) =>
a.id === lastRouterModel ? -1 : b.id === lastRouterModel ? 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 },
]
}

export type FailedTurn = {
/** The prompt that started the failed turn, when it can be sent again as is. */
prompt: string | null
promptUuid?: string
/** Tools already ran in this turn: sending the prompt again would repeat them. */
toolsRan: boolean
}

/** Looks back from the limit error to the message that started that turn. */
export function describeFailedTurn(messages: readonly Message[]): FailedTurn {
let toolsRan = false
for (let i = messages.length - 1; i >= 0; i--) {
const message = messages[i]!
if (message.type === 'assistant') {
const content = message.message.content
if (Array.isArray(content) && content.some(block => block.type === 'tool_use')) {
toolsRan = true
}
continue
}
if (
message.type !== 'user' ||
message.isMeta ||
message.isCompactSummary ||
message.isVisibleInTranscriptOnly
) {
continue
}
const content = message.message.content
if (Array.isArray(content) && content.some(block => block.type === 'tool_result')) {
continue
}
// Images and other attachments cannot be sent again from the text alone.
const hasOnlyText = !Array.isArray(content) || content.every(block => block.type === 'text')
const resubmit = hasOnlyText ? textForResubmit(message as UserMessage) : null
const text = resubmit?.mode === 'prompt' ? resubmit.text : ''
return {
prompt: text.trim() && !NOT_TYPED.test(getUserMessageText(message as UserMessage) ?? '') ? text : null,
promptUuid: message.uuid,
toolsRan,
}
}
return { prompt: null, toolsRan }
}

export function limitSwitchDoneMessage(model: string, turn: FailedTurn): string {
if (turn.toolsRan) {
return `Modelo alterado para ${model}. A resposta anterior parou depois de executar ferramentas, então nada foi reenviado para não repetir ações. Diga como continuar.`
}
if (turn.prompt === null) {
return `Modelo alterado para ${model}. Envie sua mensagem novamente para continuar.`
}
return `Modelo alterado para ${model}.`
}

type Props = {
reason: string
models: ReadonlyArray<{ id: string; displayName?: string }>
lastRouterModel: string | undefined
turn: FailedTurn
onSwitch: (model: string) => void
onKeep: () => void
}

/**
* 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 function LimitSwitchDialog({
reason,
models,
lastRouterModel,
turn,
onSwitch,
onKeep,
}: Props): React.ReactNode {
const options = limitSwitchModelOptions(models, lastRouterModel)
const consequence = turn.toolsRan
? ' Sua última mensagem não será reenviada, porque ela já executou ferramentas.'
: turn.prompt !== null
? ' Sua última mensagem será reenviada.'
: ''
return (
<Dialog title="Limite da sua conta atingido" color="warning" onCancel={onKeep}>
<Box flexDirection="column" gap={1}>
<Text>{reason.charAt(0).toUpperCase() + reason.slice(1)}</Text>
<Text>Trocar para um modelo Verboo?{consequence}</Text>
<Select
defaultFocusValue={options[0]!.value}
options={options}
visibleOptionCount={options.length}
onChange={value => (value === KEEP ? onKeep() : onSwitch(value))}
onCancel={onKeep}
/>
</Box>
</Dialog>
)
}
82 changes: 81 additions & 1 deletion src/screens/REPL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ import { useIDEStatusIndicator } from 'src/hooks/notifs/useIDEStatusIndicator.js
import { useModelMigrationNotifications } from 'src/hooks/notifs/useModelMigrationNotifications.js';
import { useCanSwitchToExistingSubscription } from 'src/hooks/notifs/useCanSwitchToExistingSubscription.js';
import { useClaudeLoginRetiredNotification } from 'src/hooks/notifs/useClaudeLoginRetiredNotification.js';
import { limitSwitchReasonFromMessage } from 'src/services/api/providerLimitErrors.js';
import { describeFailedTurn, limitSwitchDoneMessage, LimitSwitchDialog } from 'src/components/LimitSwitchDialog.js';
import { getCachedVerbooModels } from 'src/services/api/verbooModels.js';
import { saveLastVerbooModel } from 'src/utils/model/model.js';
import { useTeammateLifecycleNotification } from 'src/hooks/notifs/useTeammateShutdownNotification.js';
import { useFastModeNotification } from 'src/hooks/notifs/useFastModeNotification.js';
import type { HookProgress } from '../types/hooks.js';
Expand Down Expand Up @@ -576,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,
Expand Down Expand Up @@ -1815,6 +1825,7 @@ export function REPL({
...prev,
fileHistory: fileHistoryState
})));
const handledProviderLimitRef = useRef<string | undefined>(messages.at(-1)?.uuid);
const resume = useCallback(async (sessionId: UUID, log: LogOption, entrypoint: ResumeEntrypoint) => {
const resumeStart = performance.now();
try {
Expand Down Expand Up @@ -2015,6 +2026,9 @@ export function REPL({
}
const hydratedMessages = contentReplacementStateRef.current ? applyToolResultReplacementsToMessages(messages, contentReplacementStateRef.current.replacements) : messages;

// VERBOO-BRAND: a limit error saved in that session is old news.
handledProviderLimitRef.current = hydratedMessages.at(-1)?.uuid;

// Reset messages to the provided initial messages
// Use a callback to ensure we're not dependent on stale state
setMessages(() => hydratedMessages);
Expand Down Expand Up @@ -3790,6 +3804,68 @@ export function REPL({
resetHistory: () => { }
});
}, []);
// VERBOO-BRAND: the resend waits for the next render. onSubmit reads the model
// from its closure, so calling it right after the switch would send the prompt
// back to the account that just ran out.
const [limitResendPrompt, setLimitResendPrompt] = useState<string | null>(null);
useEffect(() => {
if (limitResendPrompt === null) return;
setLimitResendPrompt(null);
void onSubmitRef.current(limitResendPrompt, {
setCursorOffset: () => { },
clearBuffer: () => { },
resetHistory: () => { }
}, undefined, {
fromKeybinding: true
});
}, [limitResendPrompt]);
// VERBOO-BRAND: when the user's own account (ChatGPT plan, OpenAI or
// Anthropic key) runs out, open the switch to a Verboo model right away
// instead of making them type /model. Errors that came with a resumed session
// are ignored (see handledProviderLimitRef). The dialog is rendered here, not
// submitted as a command: the prompt draft, history and transcript stay intact.
useEffect(() => {
const last = messages.at(-1);
if (!last || handledProviderLimitRef.current === last.uuid) return;
const reason = providerLimitReason(last);
if (reason === null) return;
handledProviderLimitRef.current = last.uuid;
const models = getCachedVerbooModels() ?? [];
if (models.length === 0) {
setMessages(prev => [...prev, createSystemMessage('Nenhum modelo Verboo disponível na sua conta. Use /model para ver as opções.', 'warning')]);
return;
}
const turn = describeFailedTurn(messages);
const close = (text: string) => {
setToolJSX({
jsx: null,
shouldHidePromptInput: false,
clearLocalJSX: true
});
// Not 'info': that level only shows in verbose mode.
setMessages(prev => [...prev, createSystemMessage(text, 'suggestion')]);
};
setToolJSX({
jsx: <LimitSwitchDialog reason={reason} models={models} lastRouterModel={getGlobalConfig().lastRouterModel} turn={turn} onKeep={() => close('Modelo mantido. Use /model quando quiser trocar.')} onSwitch={model => {
saveLastVerbooModel(model);
setAppState(prev => ({
...prev,
mainLoopModel: model,
mainLoopModelForSession: null
}));
close(limitSwitchDoneMessage(model, turn));
if (turn.prompt === null || turn.toolsRan) return;
// Nothing ran: the failed attempt leaves history, so the model gets the
// request once. fromKeybinding keeps the draft and skips adding to history.
const prompt = turn.prompt;
setMessages(prev => prev.filter(message => message.uuid !== turn.promptUuid && message.uuid !== last.uuid));
setLimitResendPrompt(prompt);
}} />,
shouldHidePromptInput: true,
showSpinner: false,
isLocalJSXCommand: true
});
}, [messages]);
const handleExit = useCallback(async () => {
setIsExiting(true);
// In bg sessions, always detach instead of kill — even when a worktree is
Expand Down Expand Up @@ -4058,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
});

Expand Down
22 changes: 21 additions & 1 deletion src/services/api/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ import {
} from '../claudeAiLimits.js'
import { shouldProcessRateLimits } from '../rateLimitMocking.js' // Used for /mock-limits command
import { extractConnectionErrorDetails, formatAPIError } from './errorUtils.js'
import {
classifyProviderLimitError,
formatProviderLimitMessage,
PROVIDER_LIMIT_MESSAGE_PREFIX,
} from './providerLimitErrors.js'
import { getActiveModelIdentity } from '../../utils/model/activeModelIdentity.js'
import {
extractOpenAICategoryHost,
extractOpenAICategoryMarker,
Expand Down Expand Up @@ -207,7 +213,9 @@ function mapOpenAICompatibilityFailureToAssistantMessage(options: {
export function startsWithApiErrorPrefix(text: string): boolean {
return (
text.startsWith(API_ERROR_MESSAGE_PREFIX) ||
text.startsWith(`Please run /login · ${API_ERROR_MESSAGE_PREFIX}`)
text.startsWith(`Please run /login · ${API_ERROR_MESSAGE_PREFIX}`) ||
// VERBOO-BRAND: also a failed request; /compact must not keep it as a summary.
text.startsWith(PROVIDER_LIMIT_MESSAGE_PREFIX)
)
}
export const PROMPT_TOO_LONG_ERROR_MESSAGE = 'Prompt is too long'
Expand Down Expand Up @@ -647,6 +655,18 @@ export function getAssistantMessageFromError(
})
}
}
// VERBOO-BRAND: the user's own account (ChatGPT plan, OpenAI or Anthropic
// key) ran out. Retrying will not help; the REPL offers a Verboo model.
const providerLimit = classifyProviderLimitError(
error,
getActiveModelIdentity(model).provider,
)
if (providerLimit) {
return createAssistantAPIErrorMessage({
content: formatProviderLimitMessage(providerLimit),
error: 'rate_limit',
})
}
// Check for SDK timeout errors
if (
error instanceof APIConnectionTimeoutError ||
Expand Down
Loading
Loading