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:
@@ -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')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* even when the main server restarts due to code changes.
|
||||
*/
|
||||
|
||||
import { startTerminalServer } from './services/terminal'
|
||||
import { startTerminalServer, startGitWatcher } from './services/terminal'
|
||||
import { WORKING_DIR } from './config'
|
||||
|
||||
console.log('')
|
||||
@@ -13,6 +13,7 @@ console.log('='.repeat(50))
|
||||
console.log('Terminal Server (Independent Process)')
|
||||
console.log(` WebSocket: ws://localhost:4103`)
|
||||
console.log(` Working Dir: ${WORKING_DIR}`)
|
||||
console.log(` Git Watcher: enabled`)
|
||||
console.log('')
|
||||
console.log('This process is stable and won\'t restart')
|
||||
console.log('when the main server reloads.')
|
||||
@@ -20,3 +21,4 @@ console.log('='.repeat(50))
|
||||
console.log('')
|
||||
|
||||
startTerminalServer()
|
||||
startGitWatcher()
|
||||
|
||||
Reference in New Issue
Block a user