Spaces:
Runtime error

File size: 2,263 Bytes
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Workflow Trigger — calls the backend to match an utterance against active workflows.

The backend holds the embedding model and the active workflow cache.
The worker sends the utterance text and receives back the best-matching workflow
(name, id, entry_node_id) or null.  No sentence-transformers in the worker.
"""
from __future__ import annotations

import logging
from typing import Optional, TYPE_CHECKING

import httpx
from config import settings

if TYPE_CHECKING:
    from workflow.node_schema import WorkflowDefinition

logger = logging.getLogger(__name__)


async def detect_workflow_trigger(
    text: str,
    active_workflows: list,   # kept for API compatibility; backend owns the actual list
) -> Optional["WorkflowDefinition"]:
    """Ask the backend which workflow (if any) matches this utterance.

    Returns a lightweight WorkflowDefinition-like object with enough fields
    for the orchestrator to enter workflow mode, or None to stay in GENERAL.

    Never raises — caller continues in GENERAL mode on any error.
    """
    try:
        url = f"{settings.backend_internal_url}/api/workflows/internal/match"
        async with httpx.AsyncClient(timeout=3.0) as client:
            resp = await client.post(url, json={"text": text})
            resp.raise_for_status()
            data = resp.json()

        if not data or not data.get("matched"):
            return None

        # Return a minimal object the orchestrator can use directly
        from workflow.node_schema import WorkflowDefinition
        return WorkflowDefinition(
            id               = data["id"],
            name             = data["name"],
            description      = data.get("description", ""),
            intent_definition= data.get("intent_definition", ""),
            few_shot_examples= [],
            intent_embedding = None,   # not needed at runtime
            intent_threshold = data.get("intent_threshold", 0.70),
            entry_node_id    = data["entry_node_id"],
            nodes            = {},     # node detail fetched separately by node_executor
            is_active        = True,
        )
    except Exception:
        logger.exception("workflow_trigger: backend call failed; staying in GENERAL")
        return None