fix: clients sync to server terminals instead of creating new ones
- Remove auto-creation of terminal sessions from init/selectSession/switchAgent - Clients only connect to existing alive terminals from server registry - Remove localStorage persistence (agent/sessionId) — state derived from server - Refine session-state types: new AgentStatus values, LastError interface - UI improvements: AgentBadge, ChatContainer, UserInput, BashCard updates - Simplify claude-hook routes, update session-state service
This commit is contained in:
@@ -2,56 +2,7 @@ import { jsonResponse, errorResponse } from '../utils/cors'
|
||||
import { PORT_TERMINAL } from '../config'
|
||||
import { existsSync, readFileSync } from 'fs'
|
||||
import { setActiveSession, getIncrementalMessages } from '../services/transcript-engine'
|
||||
|
||||
type ClaudeStatus = 'idle' | 'processing' | 'toolUse' | 'toolDone' | 'reading' | 'writing' | 'sessionStart' | 'subagentStart' | 'subagentStop' | 'notification' | 'permissionRequest' | 'thinking'
|
||||
|
||||
interface HookPayload {
|
||||
hook_event_name?: string
|
||||
session_id?: string
|
||||
tool_name?: string
|
||||
tool_input?: unknown
|
||||
tool_response?: unknown
|
||||
prompt?: string
|
||||
cwd?: string
|
||||
model?: string
|
||||
source?: string
|
||||
transcript_path?: string
|
||||
message?: string
|
||||
notification_type?: string
|
||||
stop_hook_active?: boolean
|
||||
tool_use_id?: string
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
function deriveStatus(payload: HookPayload): { status: ClaudeStatus, tool?: string } {
|
||||
const event = payload.hook_event_name
|
||||
const toolName = payload.tool_name
|
||||
|
||||
switch (event) {
|
||||
case 'SessionStart':
|
||||
return { status: 'sessionStart' }
|
||||
case 'UserPromptSubmit':
|
||||
return { status: 'processing' }
|
||||
case 'PreToolUse':
|
||||
if (toolName && /^(Read|Glob|Grep)$/.test(toolName)) {
|
||||
return { status: 'reading', tool: toolName }
|
||||
}
|
||||
if (toolName && /^(Edit|Write)$/.test(toolName)) {
|
||||
return { status: 'writing', tool: toolName }
|
||||
}
|
||||
return { status: 'toolUse', tool: toolName }
|
||||
case 'PostToolUse':
|
||||
return { status: 'toolDone', tool: toolName }
|
||||
case 'PermissionRequest':
|
||||
return { status: 'permissionRequest', tool: toolName }
|
||||
case 'Notification':
|
||||
return { status: 'notification' }
|
||||
case 'Stop':
|
||||
return { status: 'idle' }
|
||||
default:
|
||||
return { status: 'processing' }
|
||||
}
|
||||
}
|
||||
import { deriveStatus, type HookPayload } from '../services/session-state'
|
||||
|
||||
export async function handleClaudeHook(req: Request): Promise<Response | null> {
|
||||
if (req.method !== 'POST') return null
|
||||
@@ -121,15 +72,17 @@ export async function handleClaudeHook(req: Request): Promise<Response | null> {
|
||||
}
|
||||
|
||||
// 3. Derive status and broadcast via WebSocket
|
||||
const { status, tool } = deriveStatus(body)
|
||||
try {
|
||||
await fetch(`http://localhost:${PORT_TERMINAL}/claude-status`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ status, tool, agent: agent || 'main' })
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('[claude-hook] Failed to forward status to terminal server:', e)
|
||||
const derived = deriveStatus(body)
|
||||
if (derived) {
|
||||
try {
|
||||
await fetch(`http://localhost:${PORT_TERMINAL}/claude-status`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ status: derived.status, tool: derived.tool, agent: agent || 'main' })
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('[claude-hook] Failed to forward status to terminal server:', e)
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Incremental transcript reading for real-time chat
|
||||
|
||||
@@ -3,17 +3,15 @@ import { PORT_TERMINAL } from '../config'
|
||||
|
||||
type ClaudeStatus =
|
||||
| 'idle'
|
||||
| 'processing' // UserPromptSubmit - Claude is processing user input
|
||||
| 'toolUse' // PreToolUse - Using a tool (generic)
|
||||
| 'toolDone' // PostToolUse - Tool finished
|
||||
| 'reading' // PreToolUse(Read/Glob/Grep) - Reading files
|
||||
| 'writing' // PreToolUse(Edit/Write) - Writing files
|
||||
| 'sessionStart' // SessionStart - Session just started
|
||||
| 'subagentStart' // SubagentStart - Spawning subagent
|
||||
| 'subagentStop' // SubagentStop - Subagent finished
|
||||
| 'notification' // Notification - Claude sent notification
|
||||
| 'permissionRequest' // PermissionRequest - Waiting for user permission
|
||||
| 'thinking' // Legacy support
|
||||
| 'thinking' // UserPromptSubmit / PostToolUse - Claude is thinking
|
||||
| 'toolUse' // PreToolUse - Using a tool (generic)
|
||||
| 'reading' // PreToolUse(Read/Glob/Grep) - Reading files
|
||||
| 'writing' // PreToolUse(Edit/Write) - Writing files
|
||||
| 'sessionStart' // SessionStart - Session just started
|
||||
| 'sessionEnd' // SessionEnd - Session ended
|
||||
| 'permissionRequest' // PermissionRequest - Waiting for user permission
|
||||
| 'interrupted' // PostToolUseFailure with is_interrupt
|
||||
| 'error' // PostToolUseFailure without is_interrupt
|
||||
|
||||
interface ClaudeStatusPayload {
|
||||
status: ClaudeStatus
|
||||
@@ -28,9 +26,9 @@ export async function handleClaudeStatus(req: Request): Promise<Response | null>
|
||||
const body = await req.json() as ClaudeStatusPayload
|
||||
|
||||
const validStatuses: ClaudeStatus[] = [
|
||||
'idle', 'processing', 'toolUse', 'toolDone', 'reading', 'writing',
|
||||
'sessionStart', 'subagentStart', 'subagentStop', 'notification',
|
||||
'permissionRequest', 'thinking'
|
||||
'idle', 'thinking', 'toolUse', 'reading', 'writing',
|
||||
'sessionStart', 'sessionEnd', 'permissionRequest',
|
||||
'interrupted', 'error'
|
||||
]
|
||||
if (!body.status || !validStatuses.includes(body.status)) {
|
||||
return errorResponse(`Invalid status. Must be one of: ${validStatuses.join(', ')}`, 400)
|
||||
|
||||
Reference in New Issue
Block a user