# ./core/command_parser.py """ def parse_command(message: str): if not message.startswith("/"): return None, message parts = message.strip().split() command = parts[0][1:] # remove "/" args = parts[1:] return { "command": command, "args": args }, " ".join(args) """ # ./core/command_parser.py """ System command parser. Handles: - /general client jung - /analysis analyst bpsy - returning users - agent routing """ from dataclasses import dataclass from uuid import uuid4 # ----------------------------------- # VALID CONFIGURATION # ----------------------------------- VALID_ROUTES = [ "general", "analysis" ] VALID_AGENTS = [ "jung", "bpsy" ] VALID_ROLES = [ "client", "analyst" ] # ----------------------------------- # PARSED COMMAND OBJECT # ----------------------------------- @dataclass class ParsedCommand: mode: str role: str agent: str analyst_id: str | None = None client_id: str | None = None # ----------------------------------- # ID GENERATORS # ----------------------------------- def generate_client_id(): return f"c{uuid4().hex[:8]}" def generate_analyst_id(): return f"a{uuid4().hex[:8]}" # ----------------------------------- # MAIN PARSER # ----------------------------------- def parse_command(command: str) -> ParsedCommand: """ Supported formats: NEW CLIENT /general client jung /analysis client bpsy NEW ANALYST /general analyst jung /analysis analyst bpsy RETURNING CLIENT /analysis c12345678 jung RETURNING ANALYST /analysis a12345678 bpsy """ command = command.strip() # ----------------------------------- # ENSURE THIS IS A COMMAND # ----------------------------------- if not command.startswith("/"): raise ValueError( "Not a slash command." ) tokens = command.split() # ----------------------------------- # VALIDATE TOKEN LENGTH # ----------------------------------- if len(tokens) != 3: raise ValueError( ( "Invalid command format.\n" "Expected:\n" "/analysis client jung" ) ) # ----------------------------------- # EXTRACT PARTS # ----------------------------------- mode = tokens[0].replace("/", "").lower() identifier = tokens[1].lower() agent = tokens[2].lower() # ----------------------------------- # VALIDATE MODE # ----------------------------------- if mode not in VALID_ROUTES: raise ValueError( f"Invalid mode: {mode}" ) # ----------------------------------- # VALIDATE AGENT # ----------------------------------- if agent not in VALID_AGENTS: raise ValueError( f"Invalid agent: {agent}" ) # ----------------------------------- # NEW CLIENT # ----------------------------------- if identifier == "client": client_id = generate_client_id() return ParsedCommand( mode=mode, role="client", agent=agent, client_id=client_id ) # ----------------------------------- # NEW ANALYST # ----------------------------------- if identifier == "analyst": analyst_id = generate_analyst_id() client_id = generate_client_id() return ParsedCommand( mode=mode, role="analyst", agent=agent, analyst_id=analyst_id, client_id=client_id ) # ----------------------------------- # RETURNING CLIENT # ----------------------------------- if identifier.startswith("c"): return ParsedCommand( mode=mode, role="client", agent=agent, client_id=identifier ) # ----------------------------------- # RETURNING ANALYST # ----------------------------------- if identifier.startswith("a"): return ParsedCommand( mode=mode, role="analyst", agent=agent, analyst_id=identifier ) # ----------------------------------- # INVALID IDENTIFIER # ----------------------------------- raise ValueError( ( "Unknown identifier.\n" "Use:\n" "client\n" "analyst\n" "cXXXXX\n" "aXXXXX" ) )