|
| 1 | +import os from 'os' |
| 2 | +import path from 'path' |
| 3 | + |
| 4 | +import { useMemo, useState } from 'react' |
| 5 | + |
| 6 | +import { Chat } from './chat' |
| 7 | +import { TerminalLink } from './components/terminal-link' |
| 8 | +import { ToolCallItem } from './components/tools/tool-call-item' |
| 9 | +import { useLogo } from './hooks/use-logo' |
| 10 | +import { useTerminalDimensions } from './hooks/use-terminal-dimensions' |
| 11 | +import { useTheme } from './hooks/use-theme' |
| 12 | +import { createValidationErrorBlocks } from './utils/create-validation-error-blocks' |
| 13 | +import { openFileAtPath } from './utils/open-file' |
| 14 | +import { pluralize } from '@codebuff/common/util/string' |
| 15 | + |
| 16 | +interface AppProps { |
| 17 | + initialPrompt: string | null |
| 18 | + agentId?: string |
| 19 | + requireAuth: boolean | null |
| 20 | + hasInvalidCredentials: boolean |
| 21 | + loadedAgentsData: { |
| 22 | + agents: Array<{ id: string; displayName: string }> |
| 23 | + agentsDir: string |
| 24 | + } | null |
| 25 | + validationErrors: Array<{ id: string; message: string }> |
| 26 | +} |
| 27 | + |
| 28 | +export const App = ({ |
| 29 | + initialPrompt, |
| 30 | + agentId, |
| 31 | + requireAuth, |
| 32 | + hasInvalidCredentials, |
| 33 | + loadedAgentsData, |
| 34 | + validationErrors, |
| 35 | +}: AppProps) => { |
| 36 | + const { contentMaxWidth, separatorWidth } = useTerminalDimensions() |
| 37 | + const theme = useTheme() |
| 38 | + const { textBlock: logoBlock } = useLogo({ availableWidth: contentMaxWidth }) |
| 39 | + |
| 40 | + const [isAgentListCollapsed, setIsAgentListCollapsed] = useState(true) |
| 41 | + |
| 42 | + const headerContent = useMemo(() => { |
| 43 | + if (!loadedAgentsData) { |
| 44 | + return null |
| 45 | + } |
| 46 | + |
| 47 | + const homeDir = os.homedir() |
| 48 | + const repoRoot = path.dirname(loadedAgentsData.agentsDir) |
| 49 | + const relativePath = path.relative(homeDir, repoRoot) |
| 50 | + const displayPath = relativePath.startsWith('..') |
| 51 | + ? repoRoot |
| 52 | + : `~/${relativePath}` |
| 53 | + |
| 54 | + const sortedAgents = [...loadedAgentsData.agents].sort((a, b) => { |
| 55 | + const displayNameComparison = (a.displayName || '') |
| 56 | + .toLowerCase() |
| 57 | + .localeCompare((b.displayName || '').toLowerCase()) |
| 58 | + |
| 59 | + return ( |
| 60 | + displayNameComparison || |
| 61 | + a.id.toLowerCase().localeCompare(b.id.toLowerCase()) |
| 62 | + ) |
| 63 | + }) |
| 64 | + |
| 65 | + const agentCount = sortedAgents.length |
| 66 | + |
| 67 | + const formatIdentifier = (agent: { id: string; displayName: string }) => |
| 68 | + agent.displayName && agent.displayName !== agent.id |
| 69 | + ? `${agent.displayName} (${agent.id})` |
| 70 | + : agent.displayName || agent.id |
| 71 | + |
| 72 | + const renderAgentListItem = ( |
| 73 | + agent: { id: string; displayName: string }, |
| 74 | + idx: number, |
| 75 | + ) => { |
| 76 | + const identifier = formatIdentifier(agent) |
| 77 | + return ( |
| 78 | + <text |
| 79 | + key={`agent-${idx}`} |
| 80 | + style={{ wrapMode: 'word', fg: theme.foreground }} |
| 81 | + > |
| 82 | + {`• ${identifier}`} |
| 83 | + </text> |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + const agentListContent = ( |
| 88 | + <box style={{ flexDirection: 'column', gap: 0 }}> |
| 89 | + {sortedAgents.map(renderAgentListItem)} |
| 90 | + </box> |
| 91 | + ) |
| 92 | + |
| 93 | + const headerText = pluralize(agentCount, 'local agent') |
| 94 | + |
| 95 | + return ( |
| 96 | + <box |
| 97 | + style={{ |
| 98 | + flexDirection: 'column', |
| 99 | + gap: 0, |
| 100 | + paddingLeft: 1, |
| 101 | + paddingRight: 1, |
| 102 | + }} |
| 103 | + > |
| 104 | + <text |
| 105 | + style={{ |
| 106 | + wrapMode: 'word', |
| 107 | + marginBottom: 1, |
| 108 | + marginTop: 2, |
| 109 | + fg: theme.foreground, |
| 110 | + }} |
| 111 | + > |
| 112 | + {logoBlock} |
| 113 | + </text> |
| 114 | + <text |
| 115 | + style={{ wrapMode: 'word', marginBottom: 1, fg: theme.foreground }} |
| 116 | + > |
| 117 | + Codebuff will run commands on your behalf to help you build. |
| 118 | + </text> |
| 119 | + <text |
| 120 | + style={{ wrapMode: 'word', marginBottom: 1, fg: theme.foreground }} |
| 121 | + > |
| 122 | + Directory{' '} |
| 123 | + <TerminalLink |
| 124 | + text={displayPath} |
| 125 | + inline={true} |
| 126 | + underlineOnHover={true} |
| 127 | + onActivate={() => openFileAtPath(repoRoot)} |
| 128 | + /> |
| 129 | + </text> |
| 130 | + <box style={{ marginBottom: 1 }}> |
| 131 | + <ToolCallItem |
| 132 | + name={headerText} |
| 133 | + content={agentListContent} |
| 134 | + isCollapsed={isAgentListCollapsed} |
| 135 | + isStreaming={false} |
| 136 | + streamingPreview="" |
| 137 | + finishedPreview="" |
| 138 | + onToggle={() => setIsAgentListCollapsed(!isAgentListCollapsed)} |
| 139 | + dense |
| 140 | + /> |
| 141 | + </box> |
| 142 | + {validationErrors.length > 0 && ( |
| 143 | + <box style={{ flexDirection: 'column', gap: 0 }}> |
| 144 | + {createValidationErrorBlocks({ |
| 145 | + errors: validationErrors, |
| 146 | + loadedAgentsData, |
| 147 | + availableWidth: separatorWidth, |
| 148 | + }).map((block, idx) => { |
| 149 | + if (block.type === 'html') { |
| 150 | + return ( |
| 151 | + <box key={`validation-error-${idx}`}> |
| 152 | + {block.render({ textColor: theme.foreground, theme })} |
| 153 | + </box> |
| 154 | + ) |
| 155 | + } |
| 156 | + return null |
| 157 | + })} |
| 158 | + </box> |
| 159 | + )} |
| 160 | + </box> |
| 161 | + ) |
| 162 | + }, [ |
| 163 | + loadedAgentsData, |
| 164 | + logoBlock, |
| 165 | + theme, |
| 166 | + isAgentListCollapsed, |
| 167 | + validationErrors, |
| 168 | + separatorWidth, |
| 169 | + ]) |
| 170 | + |
| 171 | + return ( |
| 172 | + <box style={{ flexDirection: 'column', gap: 0, flexGrow: 1 }}> |
| 173 | + <Chat |
| 174 | + headerContent={headerContent} |
| 175 | + initialPrompt={initialPrompt} |
| 176 | + agentId={agentId} |
| 177 | + requireAuth={requireAuth} |
| 178 | + hasInvalidCredentials={hasInvalidCredentials} |
| 179 | + loadedAgentsData={loadedAgentsData} |
| 180 | + validationErrors={validationErrors} |
| 181 | + /> |
| 182 | + </box> |
| 183 | + ) |
| 184 | +} |
0 commit comments