File size: 1,728 Bytes
b336134
 
 
24b94ea
b336134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24b94ea
b336134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Fallback parsing logic.
Attempts fast regex-based parsing first, falling back to local heuristic/embedding parser,
and finally to the cloud Gemini LLM parser.
"""
from __future__ import annotations

from core.intent_parser import parse_intent
from core.parser.local_parser.parser import LocalIntentParser

_local_parser: LocalIntentParser | None = None

def get_local_parser() -> LocalIntentParser:
    """Lazily load the local heuristic & embedding parser."""
    global _local_parser
    if _local_parser is None:
        _local_parser = LocalIntentParser()
    return _local_parser

def parse_intent_hybrid(session_id: str, command: str) -> dict | None:
    """Parse user command using fast regex path, local orchestrator parser, or cloud Gemini."""
    # 1. Fast Path: Regex / keyword resolution
    fast_result = parse_intent(session_id, command)
    if fast_result is not None:
        print(f"[Hybrid Parser] Fast path success: {fast_result}")
        return fast_result

    # 2. Local Heuristic / Spelling / Synonym / Embeddings Parser
    try:
        local_parser = get_local_parser()
        local_result = local_parser.parse_intent(session_id, command)
        if local_result is not None:
            confidence = local_result.pop("confidence", "low")
            if confidence == "high":
                print(f"[Hybrid Parser] Local parser high-confidence success: {local_result}")
                return local_result
            else:
                print(f"[Hybrid Parser] Local parser yielded low confidence: {local_result}")
    except Exception as e:
        print(f"[Hybrid Parser] Exception in local parser: {e}")

    print("[Hybrid Parser] All local paths failed. Returning None.")
    return None