feat: Add realtime git status updates via WebSocket

- Add file watcher on .git directory in terminal server
- Broadcast git-change events to connected clients
- Frontend auto-refreshes when changes detected
- Visual indicator shows realtime connection status
This commit is contained in:
2026-02-14 11:20:55 -06:00
parent 3c401c4c2b
commit 8daf07819b
3 changed files with 166 additions and 7 deletions

View File

@@ -1,4 +1,6 @@
import { spawn, type IPty } from '@skitee3000/bun-pty'
import { watch, type FSWatcher } from 'fs'
import { join } from 'path'
import { PORT_TERMINAL, WORKING_DIR, SHELL, SHELL_ARGS, DEFAULT_SESSION_ID, MAX_BUFFER_LINES } from '../config'
interface TerminalSession {
@@ -239,3 +241,66 @@ export function broadcastClaudeStatus(status: ClaudeStatus, tool?: string) {
console.log(`[Terminal] Claude status broadcast: ${status}${tool ? ` (${tool})` : ''}${clientCount} clients`)
}
// Git watcher
let gitWatcher: FSWatcher | null = null
let gitDebounceTimer: Timer | null = null
const GIT_DEBOUNCE_MS = 300
function broadcastGitChange() {
const message = JSON.stringify({
type: 'git-change',
timestamp: Date.now()
})
let clientCount = 0
for (const [, session] of sessions) {
for (const ws of session.clients) {
try {
ws.send(message)
clientCount++
} catch {
// Client disconnected
}
}
}
if (clientCount > 0) {
console.log(`[Git] Change detected → ${clientCount} clients notified`)
}
}
export function startGitWatcher() {
const gitDir = join(WORKING_DIR, '.git')
try {
// Watch .git directory recursively
gitWatcher = watch(gitDir, { recursive: true }, (eventType, filename) => {
// Ignore some noisy files
if (filename?.includes('FETCH_HEAD') || filename?.includes('gc.log')) {
return
}
// Debounce to avoid flooding
if (gitDebounceTimer) {
clearTimeout(gitDebounceTimer)
}
gitDebounceTimer = setTimeout(() => {
broadcastGitChange()
}, GIT_DEBOUNCE_MS)
})
console.log(`[Git] Watching ${gitDir} for changes`)
} catch (e: any) {
console.error(`[Git] Failed to watch .git directory: ${e.message}`)
}
}
export function stopGitWatcher() {
if (gitWatcher) {
gitWatcher.close()
gitWatcher = null
console.log('[Git] Watcher stopped')
}
}