({ chatId, initialMessages = [], workspace = null, chatMode = null })
| 39 | }); |
| 40 | |
| 41 | export function Chat({ chatId, initialMessages = [], workspace = null, chatMode = null }) { |
| 42 | const [input, setInput] = useState(''); |
| 43 | const [files, setFiles] = useState([]); |
| 44 | const hasNavigated = useRef(false); |
| 45 | const [codeMode, setCodeMode] = useState(chatMode === 'code'); |
| 46 | const [codeModeType, setCodeModeType] = useState(() => { |
| 47 | if (typeof window !== 'undefined') { |
| 48 | const stored = localStorage.getItem(`codeModeType:${chatId}`); |
| 49 | if (stored === 'plan' || stored === 'code') return stored; |
| 50 | } |
| 51 | return 'code'; |
| 52 | }); |
| 53 | |
| 54 | // Persist codeModeType to localStorage per chat |
| 55 | useEffect(() => { |
| 56 | if (typeof window !== 'undefined') { |
| 57 | localStorage.setItem(`codeModeType:${chatId}`, codeModeType); |
| 58 | } |
| 59 | }, [chatId, codeModeType]); |
| 60 | const [defaultRepo, setDefaultRepo] = useState(null); |
| 61 | const [repo, setRepo] = useState(workspace?.repo || ''); |
| 62 | const [branch, setBranch] = useState(workspace?.branch || ''); |
| 63 | const [workspaceState, setWorkspaceState] = useState(workspace); |
| 64 | const [diffStats, setDiffStats] = useState(null); |
| 65 | const [showDiff, setShowDiff] = useState(false); |
| 66 | const [availableAgents, setAvailableAgents] = useState(null); |
| 67 | const [scope, setScope] = useState(workspace?.scope || null); |
| 68 | const [availableScopes, setAvailableScopes] = useState(null); |
| 69 | |
| 70 | // Load available coding agents once on mount (for the right-click agent picker) |
| 71 | useEffect(() => { |
| 72 | import('../actions.js').then(({ getAvailableCodingAgents }) => { |
| 73 | getAvailableCodingAgents().then(agents => setAvailableAgents(agents)).catch(() => {}); |
| 74 | }).catch(() => {}); |
| 75 | }, []); |
| 76 | |
| 77 | // Load available agent scopes for agent mode |
| 78 | useEffect(() => { |
| 79 | if (!codeMode) { |
| 80 | fetch('/chat/scopes') |
| 81 | .then(r => r.json()) |
| 82 | .then(scopes => setAvailableScopes(scopes)) |
| 83 | .catch(() => setAvailableScopes([])); |
| 84 | } |
| 85 | }, [codeMode]); |
| 86 | |
| 87 | // Fetch default repo for agent mode on mount |
| 88 | // Uses fetch instead of server action to avoid Next.js page revalidation |
| 89 | useEffect(() => { |
| 90 | fetch('/code/default-repo') |
| 91 | .then(res => res.json()) |
| 92 | .then(({ repo: r }) => { |
| 93 | if (r) { |
| 94 | setDefaultRepo(r); |
| 95 | if (!workspace && !repo && !codeMode) { |
| 96 | setRepo(r); |
| 97 | fetchDefaultBranch(r).then((b) => { if (b) setBranch(b); }); |
| 98 | } |
nothing calls this directly
no test coverage detected