feat: unified hook notifier, agent auto-detection, terminal transition UI
- Add hooks/notify.ps1 as single hook handler for all events - Refactor settings.local.json to use notify.ps1 instead of inline PS - Add Notification hook, auto-detect agent from session_id/transcript - Rename agent 'main' to 'claude' across server routes and terminal - Add loading overlay and error state for terminal switching transitions - Add transitionError ref to useTranscriptDebug composable
This commit is contained in:
@@ -2,16 +2,26 @@ import { jsonResponse, errorResponse } from '../utils/cors'
|
||||
import { PORT_TERMINAL } from '../config'
|
||||
import { existsSync, readFileSync } from 'fs'
|
||||
import { setActiveSession, getIncrementalMessages } from '../services/transcript-engine'
|
||||
import { deriveStatus, type HookPayload } from '../services/session-state'
|
||||
import { deriveStatus, sessionState, type HookPayload } from '../services/session-state'
|
||||
|
||||
export async function handleClaudeHook(req: Request): Promise<Response | null> {
|
||||
if (req.method !== 'POST') return null
|
||||
|
||||
try {
|
||||
const url = new URL(req.url)
|
||||
const agent = url.searchParams.get('agent') || ''
|
||||
let agent = url.searchParams.get('agent') || ''
|
||||
const body = await req.json() as HookPayload
|
||||
|
||||
// Auto-detect agent from session_id or transcript_path
|
||||
if (!agent && body.session_id) {
|
||||
agent = sessionState.findAgentBySessionId(body.session_id) || ''
|
||||
}
|
||||
if (!agent && body.transcript_path) {
|
||||
const matches = sessionState.findAgentsByTranscript(body.transcript_path as string)
|
||||
if (matches.length === 1) agent = matches[0]
|
||||
}
|
||||
if (!agent) agent = 'claude'
|
||||
|
||||
// On Stop events, extract last assistant response from transcript
|
||||
if (body.hook_event_name === 'Stop' && body.transcript_path) {
|
||||
try {
|
||||
@@ -45,7 +55,7 @@ export async function handleClaudeHook(req: Request): Promise<Response | null> {
|
||||
}
|
||||
|
||||
// Inject agent name into hook data for WS consumers
|
||||
const hookData = { ...body, agent_name: agent || 'main' }
|
||||
const hookData = { ...body, agent_name: agent }
|
||||
|
||||
// 1. Broadcast full hook data via WebSocket (always, even for subagents)
|
||||
try {
|
||||
@@ -78,7 +88,7 @@ export async function handleClaudeHook(req: Request): Promise<Response | null> {
|
||||
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' })
|
||||
body: JSON.stringify({ status: derived.status, tool: derived.tool, agent })
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('[claude-hook] Failed to forward status to terminal server:', e)
|
||||
@@ -87,8 +97,7 @@ export async function handleClaudeHook(req: Request): Promise<Response | null> {
|
||||
|
||||
// 4. Incremental transcript reading for real-time chat
|
||||
if (body.session_id && body.transcript_path) {
|
||||
const agentName = agent || 'main'
|
||||
setActiveSession(agentName, body.session_id, body.transcript_path as string)
|
||||
setActiveSession(agent, body.session_id, body.transcript_path as string)
|
||||
|
||||
const newMessages = getIncrementalMessages(body.session_id, body.transcript_path as string)
|
||||
if (newMessages.length > 0) {
|
||||
@@ -98,7 +107,7 @@ export async function handleClaudeHook(req: Request): Promise<Response | null> {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
sessionId: body.session_id,
|
||||
agent: agentName,
|
||||
agent,
|
||||
messages: newMessages,
|
||||
hookEvent: body.hook_event_name
|
||||
})
|
||||
@@ -109,7 +118,7 @@ export async function handleClaudeHook(req: Request): Promise<Response | null> {
|
||||
}
|
||||
}
|
||||
|
||||
return jsonResponse({ success: true, event: body.hook_event_name, agent: agent || 'main' })
|
||||
return jsonResponse({ success: true, event: body.hook_event_name, agent })
|
||||
} catch (e) {
|
||||
return errorResponse('Invalid JSON body', 400)
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ export async function handleHooksApprovalPermission(req: Request): Promise<Respo
|
||||
}))
|
||||
|
||||
// Track in centralized session state → broadcasts patch to all clients
|
||||
notifyAddApproval(body.agent_name || 'main', {
|
||||
notifyAddApproval(body.agent_name || 'claude', {
|
||||
requestId,
|
||||
type: 'permission',
|
||||
toolName: body.tool_name,
|
||||
@@ -189,7 +189,7 @@ export async function handleHooksApprovalPlan(req: Request): Promise<Response |
|
||||
}))
|
||||
|
||||
// Track in centralized session state → broadcasts patch to all clients
|
||||
notifyAddApproval('main', {
|
||||
notifyAddApproval(body.agent_name || 'claude', {
|
||||
requestId,
|
||||
type: 'plan',
|
||||
lastAssistantText,
|
||||
|
||||
@@ -22,7 +22,7 @@ export function handleTranscriptSessions(): Response {
|
||||
export function handleTranscriptActive(req: Request, url: URL): Response {
|
||||
if (req.method !== 'GET') return errorResponse('Method not allowed', 405)
|
||||
|
||||
const agent = url.searchParams.get('agent') || 'main'
|
||||
const agent = url.searchParams.get('agent') || 'claude'
|
||||
const activeSession = getActiveSession(agent)
|
||||
|
||||
const sessionId = activeSession?.sessionId
|
||||
|
||||
@@ -23,7 +23,7 @@ interface AgentTerminalState {
|
||||
export const agentSessions = new Map<string, AgentTerminalState>()
|
||||
|
||||
const AGENT_COMMANDS: Record<string, string> = {
|
||||
'main': 'claude',
|
||||
'claude': 'claude',
|
||||
'ejecutor': 'ejecutor',
|
||||
'nucleo000': 'nucleo000'
|
||||
}
|
||||
@@ -695,7 +695,7 @@ type ClaudeStatus = 'idle' | 'thinking' | 'toolUse' | 'reading' | 'writing' | 's
|
||||
|
||||
// Broadcast Claude status to ALL clients across ALL sessions
|
||||
export function broadcastClaudeStatus(status: ClaudeStatus, tool?: string, agent?: string) {
|
||||
const agentName = agent || 'main'
|
||||
const agentName = agent || 'claude'
|
||||
|
||||
// Track agent running state
|
||||
if (status === 'sessionStart') {
|
||||
|
||||
Reference in New Issue
Block a user