import { TextBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'
import React from 'react'
import { AssistantBashOutputMessage } from './AssistantBashOutputMessage'
import { AssistantLocalCommandOutputMessage } from './AssistantLocalCommandOutputMessage'
import { getTheme } from '../../utils/theme'
import { Box, Text } from 'ink'
import { Cost } from '../Cost'
import {
API_ERROR_MESSAGE_PREFIX,
CREDIT_BALANCE_TOO_LOW_ERROR_MESSAGE,
INVALID_API_KEY_ERROR_MESSAGE,
PROMPT_TOO_LONG_ERROR_MESSAGE,
} from '../../services/claude.js'
import {
CANCEL_MESSAGE,
INTERRUPT_MESSAGE,
INTERRUPT_MESSAGE_FOR_TOOL_USE,
isEmptyMessageText,
NO_RESPONSE_REQUESTED,
} from '../../utils/messages.js'
import { BLACK_CIRCLE } from '../../constants/figures'
import { applyMarkdown } from '../../utils/markdown'
import { useTerminalSize } from '../../hooks/useTerminalSize'
type Props = {
param: TextBlockParam
costUSD: number
durationMs: number
debug: boolean
addMargin: boolean
shouldShowDot: boolean
verbose?: boolean
width?: number | string
}
export function AssistantTextMessage({
param: { text },
costUSD,
durationMs,
debug,
addMargin,
shouldShowDot,
verbose,
}: Props): React.ReactNode {
const { columns } = useTerminalSize()
if (isEmptyMessageText(text)) {
return null
}
// Show bash output
if (text.startsWith('
}
// Show command output
if (
text.startsWith('
}
if (text.startsWith(API_ERROR_MESSAGE_PREFIX)) {
return (
⎿
{text === API_ERROR_MESSAGE_PREFIX
? `${API_ERROR_MESSAGE_PREFIX}: Please wait a moment and try again.`
: text}
)
}
switch (text) {
// Local JSX commands don't need a response, but we still want Claude to see them
// Tool results render their own interrupt messages
case NO_RESPONSE_REQUESTED:
case INTERRUPT_MESSAGE_FOR_TOOL_USE:
return null
case INTERRUPT_MESSAGE:
case CANCEL_MESSAGE:
return (
⎿
Interrupted by user
)
case PROMPT_TOO_LONG_ERROR_MESSAGE:
return (
⎿
Context low · Run /compact to compact & continue
)
case CREDIT_BALANCE_TOO_LOW_ERROR_MESSAGE:
return (
⎿
Credit balance too low · Add funds:
https://console.anthropic.com/settings/billing
)
case INVALID_API_KEY_ERROR_MESSAGE:
return (
⎿
{INVALID_API_KEY_ERROR_MESSAGE}
)
default:
return (
{shouldShowDot && (
{BLACK_CIRCLE}
)}
{applyMarkdown(text)}
)
}
}