Coverage for .claude/hooks/moai/lib/user.py: 0.00%
28 statements
« prev ^ index » next coverage.py v7.11.3, created at 2025-11-19 08:00 +0900
« prev ^ index » next coverage.py v7.11.3, created at 2025-11-19 08:00 +0900
1#!/usr/bin/env python3
2"""User interaction handlers
4Handling the UserPromptSubmit event with enhanced agent delegation and skills JIT context
5"""
7from datetime import datetime
8from pathlib import Path
9from typing import List, Tuple
11from lib import HookPayload, HookResult
12from lib.agent_context import get_enhanced_jit_context
15def handle_user_prompt_submit(payload: HookPayload) -> HookResult:
16 """Enhanced UserPromptSubmit event handler
18 Analyze user prompts and automatically add relevant documents into context.
19 Features expert agent delegation and skills JIT context loading.
21 Args:
22 payload: Claude Code event payload
23 (includes userPrompt, cwd keys)
25 Returns:
26 HookResult(
27 system_message=Enhanced context and agent recommendation message,
28 context_files=Recommended document path list including skill references
29 )
31 Enhanced Features:
32 - Expert agent delegation based on prompt intent analysis
33 - Skills JIT context loading with agent-specific skill recommendations
34 - Traditional document context loading (backward compatible)
35 - Agent collaboration suggestions for complex tasks
37 TDD History:
38 - RED: JIT document loading scenario testing
39 - GREEN: Recommend documents by calling get_jit_context()
40 - REFACTOR: Message conditional display (only when there is a file)
41 - UPDATE: Migrated to Claude Code standard Hook schema with snake_case fields
42 - FEATURE: Command execution logging for tracking double-run debugging
43 - ENHANCE: Expert agent delegation and skills JIT context loading
44 """
45 user_prompt = payload.get("userPrompt", "")
46 cwd = payload.get("cwd", ".")
48 # Enhanced context with agent delegation and skills JIT loading
49 context_files, system_message = get_enhanced_jit_context(user_prompt, cwd)
51 # Command execution logging (DEBUG feature for tracking invocations)
52 if user_prompt.startswith("/alfred:"):
53 try:
54 log_dir = Path(cwd) / ".moai" / "logs"
55 log_dir.mkdir(parents=True, exist_ok=True)
57 log_file = log_dir / "command-invocations.log"
58 timestamp = datetime.now().isoformat()
60 # Enhanced logging with agent information
61 log_entry = f"{timestamp} | {user_prompt}"
62 if system_message and "Expert Agent" in system_message:
63 log_entry += " | AGENT_DELEGATION"
64 if context_files and any("skills/" in str(f) for f in context_files):
65 log_entry += " | SKILLS_JIT"
67 with open(log_file, "a", encoding="utf-8") as f:
68 f.write(f"{log_entry}\n")
69 except Exception:
70 # Silently fail if logging fails (don't interrupt main flow)
71 pass
73 # Fallback system message for backward compatibility
74 if not system_message and context_files:
75 system_message = f"📎 Loaded {len(context_files)} context file(s)"
77 return HookResult(system_message=system_message, context_files=context_files)
80__all__ = ["handle_user_prompt_submit"]