|
| 1 | +import { memo, useState } from 'react'; |
| 2 | +import { workbenchStore } from '~/lib/stores/workbench'; |
| 3 | +import { WORK_DIR } from '~/utils/constants'; |
| 4 | +import WithTooltip from '~/components/ui/Tooltip'; |
| 5 | +import { motion, AnimatePresence } from 'framer-motion'; |
| 6 | + |
| 7 | +interface ContextIndicatorProps { |
| 8 | + files?: string[]; |
| 9 | + summary?: string; |
| 10 | + tokenCount?: { |
| 11 | + prompt: number; |
| 12 | + completion: number; |
| 13 | + total: number; |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +function normalizedFilePath(path: string) { |
| 18 | + let normalizedPath = path; |
| 19 | + |
| 20 | + if (normalizedPath.startsWith(WORK_DIR)) { |
| 21 | + normalizedPath = path.replace(WORK_DIR, ''); |
| 22 | + } |
| 23 | + |
| 24 | + if (normalizedPath.startsWith('/')) { |
| 25 | + normalizedPath = normalizedPath.slice(1); |
| 26 | + } |
| 27 | + |
| 28 | + return normalizedPath; |
| 29 | +} |
| 30 | + |
| 31 | +function openFileInWorkbench(filePath: string) { |
| 32 | + const normalized = normalizedFilePath(filePath); |
| 33 | + |
| 34 | + if (workbenchStore.currentView.get() !== 'code') { |
| 35 | + workbenchStore.currentView.set('code'); |
| 36 | + } |
| 37 | + |
| 38 | + workbenchStore.setSelectedFile(`${WORK_DIR}/${normalized}`); |
| 39 | +} |
| 40 | + |
| 41 | +function getFileIcon(filePath: string): string { |
| 42 | + const ext = filePath.split('.').pop()?.toLowerCase(); |
| 43 | + |
| 44 | + switch (ext) { |
| 45 | + case 'ts': |
| 46 | + case 'tsx': |
| 47 | + return 'i-vscode-icons:file-type-typescript'; |
| 48 | + case 'js': |
| 49 | + case 'jsx': |
| 50 | + return 'i-vscode-icons:file-type-js'; |
| 51 | + case 'css': |
| 52 | + return 'i-vscode-icons:file-type-css'; |
| 53 | + case 'html': |
| 54 | + return 'i-vscode-icons:file-type-html'; |
| 55 | + case 'json': |
| 56 | + return 'i-vscode-icons:file-type-json'; |
| 57 | + case 'md': |
| 58 | + return 'i-vscode-icons:file-type-markdown'; |
| 59 | + case 'sql': |
| 60 | + return 'i-vscode-icons:file-type-sql'; |
| 61 | + default: |
| 62 | + return 'i-ph:file'; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +export const ContextIndicator = memo(({ files, summary, tokenCount }: ContextIndicatorProps) => { |
| 67 | + const [isExpanded, setIsExpanded] = useState(false); |
| 68 | + |
| 69 | + if (!files?.length && !summary) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + const fileCount = files?.length || 0; |
| 74 | + |
| 75 | + return ( |
| 76 | + <div className="context-indicator mb-3"> |
| 77 | + <button |
| 78 | + onClick={() => setIsExpanded(!isExpanded)} |
| 79 | + className="flex items-center gap-2 text-xs transition-colors" |
| 80 | + style={{ color: 'var(--codinit-elements-textSecondary)' }} |
| 81 | + onMouseEnter={(e) => (e.currentTarget.style.color = 'var(--codinit-elements-textPrimary)')} |
| 82 | + onMouseLeave={(e) => (e.currentTarget.style.color = 'var(--codinit-elements-textSecondary)')} |
| 83 | + > |
| 84 | + <span className={`i-ph:caret-${isExpanded ? 'down' : 'right'} text-sm`} style={{ color: 'inherit' }} /> |
| 85 | + <span className="i-ph:files text-sm" style={{ color: 'inherit' }} /> |
| 86 | + <span> |
| 87 | + {fileCount} file{fileCount !== 1 ? 's' : ''} in context |
| 88 | + </span> |
| 89 | + {summary && ( |
| 90 | + <> |
| 91 | + <span className="i-ph:chat-circle-text text-sm" style={{ color: 'inherit' }} /> |
| 92 | + <span>Summarized</span> |
| 93 | + </> |
| 94 | + )} |
| 95 | + {tokenCount && ( |
| 96 | + <> |
| 97 | + <span style={{ color: 'var(--codinit-elements-borderColor)' }}>•</span> |
| 98 | + <span className="i-ph:coins text-sm" style={{ color: 'inherit' }} /> |
| 99 | + <span>{tokenCount.total.toLocaleString()} tokens</span> |
| 100 | + </> |
| 101 | + )} |
| 102 | + </button> |
| 103 | + |
| 104 | + <AnimatePresence> |
| 105 | + {isExpanded && ( |
| 106 | + <motion.div |
| 107 | + initial={{ height: 0, opacity: 0 }} |
| 108 | + animate={{ height: 'auto', opacity: 1 }} |
| 109 | + exit={{ height: 0, opacity: 0 }} |
| 110 | + transition={{ duration: 0.2 }} |
| 111 | + className="overflow-hidden" |
| 112 | + > |
| 113 | + <div |
| 114 | + className="mt-2 p-3 rounded-lg border border-codinit-elements-borderColor" |
| 115 | + style={{ backgroundColor: 'var(--codinit-elements-bg-depth-2)' }} |
| 116 | + > |
| 117 | + {files && files.length > 0 && ( |
| 118 | + <div className="mb-3"> |
| 119 | + <div className="text-xs font-medium text-codinit-elements-textSecondary mb-2">Files included:</div> |
| 120 | + <div className="flex flex-wrap gap-1.5"> |
| 121 | + {files.map((file) => { |
| 122 | + const normalized = normalizedFilePath(file); |
| 123 | + const fileName = normalized.split('/').pop() || normalized; |
| 124 | + |
| 125 | + return ( |
| 126 | + <WithTooltip key={normalized} tooltip={normalized}> |
| 127 | + <button |
| 128 | + onClick={() => openFileInWorkbench(file)} |
| 129 | + className="inline-flex items-center gap-1 px-2 py-1 text-xs text-codinit-elements-textPrimary rounded hover:opacity-80 transition-colors" |
| 130 | + style={{ backgroundColor: 'var(--codinit-elements-bg-depth-3)' }} |
| 131 | + > |
| 132 | + <span className={`${getFileIcon(normalized)} text-sm`} /> |
| 133 | + <span className="max-w-32 truncate">{fileName}</span> |
| 134 | + </button> |
| 135 | + </WithTooltip> |
| 136 | + ); |
| 137 | + })} |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + )} |
| 141 | + |
| 142 | + {summary && ( |
| 143 | + <div> |
| 144 | + <div className="text-xs font-medium text-codinit-elements-textSecondary mb-2"> |
| 145 | + Conversation summary: |
| 146 | + </div> |
| 147 | + <div |
| 148 | + className="text-xs text-codinit-elements-textPrimary p-2 rounded max-h-32 overflow-y-auto" |
| 149 | + style={{ backgroundColor: 'var(--codinit-elements-bg-depth-3)' }} |
| 150 | + > |
| 151 | + {summary.length > 300 ? `${summary.substring(0, 300)}...` : summary} |
| 152 | + </div> |
| 153 | + </div> |
| 154 | + )} |
| 155 | + |
| 156 | + {tokenCount && ( |
| 157 | + <div className="mt-3 pt-3 border-t border-codinit-elements-borderColor"> |
| 158 | + <div className="flex flex-wrap gap-x-4 gap-y-1 text-xs text-codinit-elements-textSecondary"> |
| 159 | + <span>Prompt: {tokenCount.prompt.toLocaleString()}</span> |
| 160 | + <span>Completion: {tokenCount.completion.toLocaleString()}</span> |
| 161 | + <span className="font-medium text-codinit-elements-textPrimary"> |
| 162 | + Total: {tokenCount.total.toLocaleString()} |
| 163 | + </span> |
| 164 | + </div> |
| 165 | + </div> |
| 166 | + )} |
| 167 | + </div> |
| 168 | + </motion.div> |
| 169 | + )} |
| 170 | + </AnimatePresence> |
| 171 | + </div> |
| 172 | + ); |
| 173 | +}); |
0 commit comments