Skip to content

Commit e5bb9c0

Browse files
committed
fix: block on validate failure
1 parent 8d1bc67 commit e5bb9c0

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

cli/src/chat.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import type { User } from './utils/auth'
5858
import type { ToolName } from '@codebuff/sdk'
5959
import type { ScrollBoxRenderable } from '@opentui/core'
6060

61-
type ChatVariant = 'ai' | 'user' | 'agent'
61+
type ChatVariant = 'ai' | 'user' | 'agent' | 'error'
6262

6363
const MAX_VIRTUALIZED_TOP_LEVEL = 60
6464
const VIRTUAL_OVERSCAN = 12

cli/src/hooks/use-agent-validation.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type ValidationError = {
1313
type UseAgentValidationResult = {
1414
validationErrors: ValidationError[]
1515
isValidating: boolean
16-
validate: () => Promise<void>
16+
validate: () => Promise<boolean>
1717
}
1818

1919
/**
@@ -28,14 +28,15 @@ export const useAgentValidation = (
2828
const [isValidating, setIsValidating] = useState(false)
2929

3030
// Validate agents and update state
31-
const validate = useCallback(async () => {
31+
// Returns true if validation passes, false if it fails
32+
const validate = useCallback(async (): Promise<boolean> => {
3233
setIsValidating(true)
3334

3435
try {
3536
const agentDefinitions = loadAgentDefinitions()
3637
logger.debug(
3738
{ agentCount: agentDefinitions.length },
38-
'Validating agents...',
39+
'Validating agents before message send...',
3940
)
4041

4142
const validationResult = await validateAgents(agentDefinitions, {
@@ -45,16 +46,20 @@ export const useAgentValidation = (
4546
if (validationResult.success) {
4647
logger.debug('Agent validation passed')
4748
setValidationErrors([])
49+
return true
4850
} else {
4951
logger.debug(
5052
{ errorCount: validationResult.validationErrors.length },
5153
'Agent validation found errors',
5254
)
5355
setValidationErrors(validationResult.validationErrors)
56+
return false
5457
}
5558
} catch (error) {
5659
logger.error({ error }, 'Agent validation failed with exception')
5760
// Don't update validation errors on exception - keep previous state
61+
// Return false to block message sending on validation errors
62+
return false
5863
} finally {
5964
setIsValidating(false)
6065
}

cli/src/hooks/use-message-renderer.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,10 @@ export const useMessageRenderer = (
270270

271271
const isAi = message.variant === 'ai'
272272
const isUser = message.variant === 'user'
273-
const lineColor = isAi ? theme.aiLine : theme.userLine
274-
const textColor = isAi ? theme.messageAiText : theme.messageUserText
275-
const timestampColor = isAi ? theme.timestampAi : theme.timestampUser
273+
const isError = message.variant === 'error'
274+
const lineColor = isError ? 'red' : isAi ? theme.aiLine : theme.userLine
275+
const textColor = isError ? 'red' : isAi ? theme.messageAiText : theme.messageUserText
276+
const timestampColor = isError ? 'red' : isAi ? theme.timestampAi : theme.timestampUser
276277
const estimatedMessageWidth = availableWidth
277278
const codeBlockWidth = Math.max(10, estimatedMessageWidth - 8)
278279
const paletteForMessage: MarkdownPalette = {

cli/src/hooks/use-send-message.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ interface UseSendMessageOptions {
9797
setCanProcessQueue: (can: boolean) => void
9898
abortControllerRef: React.MutableRefObject<AbortController | null>
9999
agentId?: string
100-
onBeforeMessageSend?: () => Promise<void>
100+
onBeforeMessageSend?: () => Promise<boolean>
101101
setMainAgentStreamStartTime: (time: number | null) => void
102102
}
103103

@@ -247,11 +247,43 @@ export const useSendMessage = ({
247247

248248
const sendMessage = useCallback(
249249
async (content: string, params: { agentMode: 'FAST' | 'MAX' }) => {
250-
// Trigger validation before sending message (non-blocking)
250+
// Validate agents before sending message (blocking)
251251
if (onBeforeMessageSend) {
252-
onBeforeMessageSend().catch((error) => {
253-
logger.error({ error }, 'Validation before message send failed')
254-
})
252+
try {
253+
const validationPassed = await onBeforeMessageSend()
254+
255+
if (!validationPassed) {
256+
logger.warn('Message send blocked due to agent validation errors')
257+
258+
// Add an error message to the chat
259+
const errorMessage: ChatMessage = {
260+
id: `error-${Date.now()}`,
261+
variant: 'error',
262+
content: 'Cannot send message: Please fix agent validation errors first. Check the validation errors displayed above.',
263+
timestamp: formatTimestamp(),
264+
}
265+
266+
applyMessageUpdate((prev) => [...prev, errorMessage])
267+
await yieldToEventLoop()
268+
269+
return
270+
}
271+
} catch (error) {
272+
logger.error({ error }, 'Validation before message send failed with exception')
273+
274+
// Add an error message to the chat
275+
const errorMessage: ChatMessage = {
276+
id: `error-${Date.now()}`,
277+
variant: 'error',
278+
content: 'Cannot send message: Agent validation failed unexpectedly.',
279+
timestamp: formatTimestamp(),
280+
}
281+
282+
applyMessageUpdate((prev) => [...prev, errorMessage])
283+
await yieldToEventLoop()
284+
285+
return
286+
}
255287
}
256288

257289
const { agentMode } = params

0 commit comments

Comments
 (0)