Spaces:
Running

File size: 2,921 Bytes
fd5dd94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Graph state shared by the main graph and every pack subgraph."""

from __future__ import annotations

import operator
from typing import Annotated, Any, TypedDict

from pydantic import BaseModel, Field


class Event(BaseModel):
    """Normalized input that starts a run (Phase 5 persists these in an inbox table)."""

    type: str  # "chat.message", "demo.refund_request", "schedule.tick", ...
    source: str = "chat"  # chat | webhook | email | whatsapp | cron | manual
    text: str | None = None  # free text; trusted only when ``trusted`` is True
    payload: dict[str, Any] = Field(default_factory=dict)
    trusted: bool = False  # True only for authenticated operator input
    # Phase 13 (filled by core.i18n.pipeline at run start; ``text`` keeps the original):
    language: str | None = None  # en | ur | roman_ur | other | und
    language_confidence: float | None = None
    normalized_text: str | None = None  # spelling/digit-normalized ``text``; still untrusted


class RunState(TypedDict, total=False):
    tenant_id: str
    run_id: str
    thread_id: str
    event: dict[str, Any]
    intent: str | None
    plan: list[dict[str, Any]]  # supervisor steps: [{"intent": ..., "instruction": ...}]
    cursor: int
    extracted: dict[str, Any]  # values from the quarantine extractor (all tainted)
    facts: dict[str, Any]  # values read from the system of record (trusted)
    log: Annotated[list[str], operator.add]  # operator-facing progress lines
    actions: Annotated[list[dict[str, Any]], operator.add]  # gateway outcomes, in order
    status: str  # running | completed | rejected | failed | unsupported
    output: str | None
    error: str | None
    # Phase 15: read-only, non-secret tenant snapshot set by the router node (ADR-0009 G5;
    # see core.agent.tenant_config) and heuristic signals on the inbound text (routing hints
    # only, never permissions; see core.observability.injection).
    tenant_config: dict[str, Any]
    signals: dict[str, Any]


class StepInput(TypedDict, total=False):
    """What a pack subgraph receives: RunState without the accumulated ``log``/``actions``.



    Subgraphs then return only the entries they added, so the parent's ``operator.add``

    reducers never double-count earlier steps.

    """

    tenant_id: str
    run_id: str
    thread_id: str
    event: dict[str, Any]
    intent: str | None
    plan: list[dict[str, Any]]
    cursor: int
    extracted: dict[str, Any]
    facts: dict[str, Any]
    status: str
    output: str | None
    error: str | None
    tenant_config: dict[str, Any]  # Phase 15 (G5)
    signals: dict[str, Any]  # Phase 15


def subgraph_builder() -> Any:
    """``StateGraph`` preconfigured for pack subgraphs. Always use this in packs."""
    from langgraph.graph import StateGraph

    return StateGraph(RunState, input_schema=StepInput)