efficientai-sdk 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- efficientai_sdk/__init__.py +40 -0
- efficientai_sdk/_base_client.py +313 -0
- efficientai_sdk/_client.py +196 -0
- efficientai_sdk/_config.py +139 -0
- efficientai_sdk/_exceptions.py +142 -0
- efficientai_sdk/_version.py +1 -0
- efficientai_sdk/cli.py +99 -0
- efficientai_sdk/integrations/__init__.py +4 -0
- efficientai_sdk/integrations/livekit.py +134 -0
- efficientai_sdk/integrations/pipecat.py +126 -0
- efficientai_sdk/py.typed +0 -0
- efficientai_sdk/simulations/__init__.py +25 -0
- efficientai_sdk/simulations/api.py +682 -0
- efficientai_sdk/simulations/models.py +186 -0
- efficientai_sdk/simulations/polling.py +62 -0
- efficientai_sdk/tracing/__init__.py +110 -0
- efficientai_sdk/tracing/api.py +835 -0
- efficientai_sdk/tracing/context.py +57 -0
- efficientai_sdk/tracing/correlation.py +191 -0
- efficientai_sdk/tracing/handshake.py +42 -0
- efficientai_sdk/tracing/otel.py +535 -0
- efficientai_sdk/tracing/session.py +385 -0
- efficientai_sdk-0.1.0.dist-info/METADATA +168 -0
- efficientai_sdk-0.1.0.dist-info/RECORD +27 -0
- efficientai_sdk-0.1.0.dist-info/WHEEL +4 -0
- efficientai_sdk-0.1.0.dist-info/entry_points.txt +2 -0
- efficientai_sdk-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Per-call correlation context.
|
|
2
|
+
|
|
3
|
+
Each call's ``efficientai.*`` attributes live in a :class:`contextvars.ContextVar`, so
|
|
4
|
+
concurrent calls handled by one process (separate asyncio tasks or threads) each stamp
|
|
5
|
+
their own call id onto their spans. Asyncio tasks inherit the context they were created
|
|
6
|
+
in, so everything a call handler starts after the session opens is tagged correctly.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import contextvars
|
|
12
|
+
from collections.abc import Iterator, Mapping
|
|
13
|
+
from contextlib import contextmanager
|
|
14
|
+
from typing import Optional
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"call_attributes",
|
|
18
|
+
"get_call_attributes",
|
|
19
|
+
"reset_call_attributes",
|
|
20
|
+
"set_call_attributes",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
_CALL_ATTRIBUTES: contextvars.ContextVar[Optional[dict[str, str]]] = contextvars.ContextVar(
|
|
24
|
+
"efficientai_call_attributes", default=None
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def get_call_attributes() -> Optional[dict[str, str]]:
|
|
29
|
+
"""Correlation attributes of the call active in this context, if any."""
|
|
30
|
+
return _CALL_ATTRIBUTES.get()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def set_call_attributes(
|
|
34
|
+
attributes: Mapping[str, str],
|
|
35
|
+
) -> contextvars.Token[Optional[dict[str, str]]]:
|
|
36
|
+
"""Make ``attributes`` the active call for this context. Returns a reset token."""
|
|
37
|
+
return _CALL_ATTRIBUTES.set(dict(attributes))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def reset_call_attributes(token: contextvars.Token[Optional[dict[str, str]]]) -> None:
|
|
41
|
+
"""Undo :func:`set_call_attributes`. Safe to call from a different context."""
|
|
42
|
+
try:
|
|
43
|
+
_CALL_ATTRIBUTES.reset(token)
|
|
44
|
+
except ValueError:
|
|
45
|
+
# Token created in another context (e.g. session opened in one callback and
|
|
46
|
+
# closed in another). That context ends on its own; clear ours instead.
|
|
47
|
+
_CALL_ATTRIBUTES.set(None)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@contextmanager
|
|
51
|
+
def call_attributes(attributes: Mapping[str, str]) -> Iterator[None]:
|
|
52
|
+
"""Activate ``attributes`` for the duration of the ``with`` block."""
|
|
53
|
+
token = set_call_attributes(attributes)
|
|
54
|
+
try:
|
|
55
|
+
yield
|
|
56
|
+
finally:
|
|
57
|
+
reset_call_attributes(token)
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""Correlation ids that tie an agent's spans to an EfficientAI call or simulation result.
|
|
2
|
+
|
|
3
|
+
Pure helpers with no OpenTelemetry dependency: HTTP header names for OTLP export, SIP
|
|
4
|
+
header names used on EfficientAI dial-out, span attribute keys, and parsers for them.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import re
|
|
10
|
+
from collections.abc import Mapping
|
|
11
|
+
from typing import Any, Optional
|
|
12
|
+
|
|
13
|
+
# OTLP HTTP headers (customer → EfficientAI)
|
|
14
|
+
OTLP_HEADER_API_KEY = "X-API-Key"
|
|
15
|
+
OTLP_HEADER_WORKSPACE_ID = "X-Workspace-Id"
|
|
16
|
+
OTLP_HEADER_CALL_SHORT_ID = "X-EfficientAI-Call-Short-Id"
|
|
17
|
+
OTLP_HEADER_RUN_ID = "X-EfficientAI-Run-Id"
|
|
18
|
+
|
|
19
|
+
# SIP headers on outbound INVITE (EfficientAI → customer PSTN/SIP)
|
|
20
|
+
# Vobiz forwards X-VH-* ; Plivo prefixes custom keys with X-PH-
|
|
21
|
+
SIP_VH_CALL_SHORT_ID = "X-VH-EfficientAI-Call-Short-Id"
|
|
22
|
+
SIP_VH_RUN_ID = "X-VH-EfficientAI-Run-Id"
|
|
23
|
+
SIP_PLIVO_CALL_SHORT_ID = "EfficientAI-Call-Short-Id"
|
|
24
|
+
SIP_PLIVO_RUN_ID = "EfficientAI-Run-Id"
|
|
25
|
+
|
|
26
|
+
# Span / resource attributes
|
|
27
|
+
ATTR_CALL_SHORT_ID = "efficientai.call_short_id"
|
|
28
|
+
ATTR_RUN_ID = "efficientai.evaluator_result_id"
|
|
29
|
+
ATTR_AGENT_ID = "efficientai.agent_id"
|
|
30
|
+
ATTR_WORKSPACE_ID = "efficientai.workspace_id"
|
|
31
|
+
ATTR_ORGANIZATION_ID = "efficientai.organization_id"
|
|
32
|
+
ATTR_ENVIRONMENT = "efficientai.environment"
|
|
33
|
+
ATTR_TRANSPORT = "efficientai.transport"
|
|
34
|
+
ATTR_OTLP_EXPORT_ENDPOINT = "efficientai.otlp_export_endpoint"
|
|
35
|
+
|
|
36
|
+
__all__ = [
|
|
37
|
+
"ATTR_AGENT_ID",
|
|
38
|
+
"ATTR_CALL_SHORT_ID",
|
|
39
|
+
"ATTR_ENVIRONMENT",
|
|
40
|
+
"ATTR_ORGANIZATION_ID",
|
|
41
|
+
"ATTR_OTLP_EXPORT_ENDPOINT",
|
|
42
|
+
"ATTR_RUN_ID",
|
|
43
|
+
"ATTR_TRANSPORT",
|
|
44
|
+
"ATTR_WORKSPACE_ID",
|
|
45
|
+
"OTLP_HEADER_API_KEY",
|
|
46
|
+
"OTLP_HEADER_CALL_SHORT_ID",
|
|
47
|
+
"OTLP_HEADER_RUN_ID",
|
|
48
|
+
"OTLP_HEADER_WORKSPACE_ID",
|
|
49
|
+
"SIP_PLIVO_CALL_SHORT_ID",
|
|
50
|
+
"SIP_PLIVO_RUN_ID",
|
|
51
|
+
"SIP_VH_CALL_SHORT_ID",
|
|
52
|
+
"SIP_VH_RUN_ID",
|
|
53
|
+
"build_outbound_sip_headers",
|
|
54
|
+
"extract_call_short_id",
|
|
55
|
+
"format_plivo_sip_headers",
|
|
56
|
+
"otlp_export_headers",
|
|
57
|
+
"span_correlation_attributes",
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
_CALL_SHORT_ID_RE = re.compile(r"^\d{6}$")
|
|
61
|
+
|
|
62
|
+
_HEADER_ALIASES_CALL_SHORT_ID = (
|
|
63
|
+
OTLP_HEADER_CALL_SHORT_ID,
|
|
64
|
+
SIP_VH_CALL_SHORT_ID,
|
|
65
|
+
"X-PH-EfficientAI-Call-Short-Id",
|
|
66
|
+
"SipHeader_X-VH-EfficientAI-Call-Short-Id",
|
|
67
|
+
"SipHeader_X-PH-EfficientAI-Call-Short-Id",
|
|
68
|
+
"X-EfficientAI-Call-Short-Id",
|
|
69
|
+
"EfficientAI-Call-Short-Id",
|
|
70
|
+
"efficientai.call_short_id",
|
|
71
|
+
"Call-Short-Id",
|
|
72
|
+
"call_short_id",
|
|
73
|
+
"X-Custom-Call-Short-Id",
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _normalize_header_key(key: str) -> str:
|
|
78
|
+
return key.strip().lower().replace("_", "-")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _lookup(mapping: Optional[Mapping[str, Any]], *candidates: str) -> Optional[str]:
|
|
82
|
+
if not mapping:
|
|
83
|
+
return None
|
|
84
|
+
normalized = {_normalize_header_key(str(k)): v for k, v in mapping.items()}
|
|
85
|
+
for name in candidates:
|
|
86
|
+
val = normalized.get(_normalize_header_key(name))
|
|
87
|
+
if val is not None and str(val).strip():
|
|
88
|
+
return str(val).strip()
|
|
89
|
+
return None
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _valid_call_short_id(value: Optional[str]) -> Optional[str]:
|
|
93
|
+
if not value:
|
|
94
|
+
return None
|
|
95
|
+
cleaned = str(value).strip()
|
|
96
|
+
if _CALL_SHORT_ID_RE.match(cleaned):
|
|
97
|
+
return cleaned
|
|
98
|
+
return None
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def extract_call_short_id(
|
|
102
|
+
*,
|
|
103
|
+
sip_headers: Optional[Mapping[str, Any]] = None,
|
|
104
|
+
webhook_params: Optional[Mapping[str, Any]] = None,
|
|
105
|
+
custom_params: Optional[Mapping[str, Any]] = None,
|
|
106
|
+
) -> Optional[str]:
|
|
107
|
+
"""Resolve EfficientAI 6-digit call id from inbound telephony metadata."""
|
|
108
|
+
for mapping in (sip_headers, webhook_params, custom_params):
|
|
109
|
+
found = _lookup(mapping, *_HEADER_ALIASES_CALL_SHORT_ID)
|
|
110
|
+
valid = _valid_call_short_id(found)
|
|
111
|
+
if valid:
|
|
112
|
+
return valid
|
|
113
|
+
return None
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def build_outbound_sip_headers(
|
|
117
|
+
*,
|
|
118
|
+
call_short_id: str,
|
|
119
|
+
evaluator_result_id: Optional[str] = None,
|
|
120
|
+
agent_id: Optional[str] = None,
|
|
121
|
+
) -> dict[str, str]:
|
|
122
|
+
"""Headers to attach on Vobiz/Plivo outbound INVITE for customer-side correlation."""
|
|
123
|
+
cid = _valid_call_short_id(call_short_id)
|
|
124
|
+
if not cid:
|
|
125
|
+
raise ValueError("call_short_id must be a 6-digit string")
|
|
126
|
+
|
|
127
|
+
headers: dict[str, str] = {
|
|
128
|
+
SIP_VH_CALL_SHORT_ID: cid,
|
|
129
|
+
SIP_PLIVO_CALL_SHORT_ID: cid,
|
|
130
|
+
}
|
|
131
|
+
if evaluator_result_id:
|
|
132
|
+
headers[SIP_VH_RUN_ID] = str(evaluator_result_id)
|
|
133
|
+
headers[SIP_PLIVO_RUN_ID] = str(evaluator_result_id)
|
|
134
|
+
if agent_id:
|
|
135
|
+
headers["X-VH-EfficientAI-Agent-Id"] = str(agent_id)
|
|
136
|
+
headers["EfficientAI-Agent-Id"] = str(agent_id)
|
|
137
|
+
return headers
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def format_plivo_sip_headers(headers: Mapping[str, str]) -> str:
|
|
141
|
+
"""Plivo/Vobiz REST API expects comma-separated key=value pairs."""
|
|
142
|
+
return ",".join(f"{k}={v}" for k, v in headers.items())
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def otlp_export_headers(
|
|
146
|
+
*,
|
|
147
|
+
api_key: str,
|
|
148
|
+
call_short_id: Optional[str] = None,
|
|
149
|
+
evaluator_result_id: Optional[str] = None,
|
|
150
|
+
workspace_id: Optional[str] = None,
|
|
151
|
+
) -> dict[str, str]:
|
|
152
|
+
headers = {OTLP_HEADER_API_KEY: api_key}
|
|
153
|
+
cid = _valid_call_short_id(call_short_id)
|
|
154
|
+
if cid:
|
|
155
|
+
headers[OTLP_HEADER_CALL_SHORT_ID] = cid
|
|
156
|
+
if evaluator_result_id:
|
|
157
|
+
headers[OTLP_HEADER_RUN_ID] = str(evaluator_result_id)
|
|
158
|
+
ws = str(workspace_id).strip() if workspace_id and str(workspace_id).strip() else None
|
|
159
|
+
if ws:
|
|
160
|
+
headers[OTLP_HEADER_WORKSPACE_ID] = ws
|
|
161
|
+
return headers
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def span_correlation_attributes(
|
|
165
|
+
*,
|
|
166
|
+
call_short_id: Optional[str] = None,
|
|
167
|
+
evaluator_result_id: Optional[str] = None,
|
|
168
|
+
agent_id: Optional[str] = None,
|
|
169
|
+
workspace_id: Optional[str] = None,
|
|
170
|
+
organization_id: Optional[str] = None,
|
|
171
|
+
transport: Optional[str] = None,
|
|
172
|
+
export_endpoint: Optional[str] = None,
|
|
173
|
+
environment: str = "pre_prod",
|
|
174
|
+
) -> dict[str, str]:
|
|
175
|
+
attrs: dict[str, str] = {ATTR_ENVIRONMENT: environment}
|
|
176
|
+
cid = _valid_call_short_id(call_short_id)
|
|
177
|
+
if cid:
|
|
178
|
+
attrs[ATTR_CALL_SHORT_ID] = cid
|
|
179
|
+
if evaluator_result_id:
|
|
180
|
+
attrs[ATTR_RUN_ID] = str(evaluator_result_id)
|
|
181
|
+
if agent_id:
|
|
182
|
+
attrs[ATTR_AGENT_ID] = str(agent_id)
|
|
183
|
+
if workspace_id:
|
|
184
|
+
attrs[ATTR_WORKSPACE_ID] = str(workspace_id)
|
|
185
|
+
if organization_id:
|
|
186
|
+
attrs[ATTR_ORGANIZATION_ID] = str(organization_id)
|
|
187
|
+
if transport:
|
|
188
|
+
attrs[ATTR_TRANSPORT] = str(transport)
|
|
189
|
+
if export_endpoint:
|
|
190
|
+
attrs[ATTR_OTLP_EXPORT_ENDPOINT] = str(export_endpoint).rstrip("/")
|
|
191
|
+
return attrs
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Parse the trace handshake EfficientAI sends to an agent at the start of a test call."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from typing import Any, Optional
|
|
7
|
+
|
|
8
|
+
__all__ = ["HANDSHAKE_TYPE", "LEGACY_CALL_SHORT_ID_KEY", "parse_trace_handshake"]
|
|
9
|
+
|
|
10
|
+
HANDSHAKE_TYPE = "efficientai_trace_handshake"
|
|
11
|
+
LEGACY_CALL_SHORT_ID_KEY = "efficientai_call_short_id"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _normalized(message: Mapping[str, Any], call_short_id: Any) -> dict[str, Any]:
|
|
15
|
+
return {
|
|
16
|
+
"type": HANDSHAKE_TYPE,
|
|
17
|
+
"call_short_id": str(call_short_id),
|
|
18
|
+
"trace_id": message.get("trace_id"),
|
|
19
|
+
"agent_id": message.get("agent_id"),
|
|
20
|
+
"workspace_id": message.get("workspace_id"),
|
|
21
|
+
"transport": message.get("transport"),
|
|
22
|
+
"otel_correlation": dict(message.get("otel_correlation") or {}),
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def parse_trace_handshake(message: Any) -> Optional[dict[str, Any]]:
|
|
27
|
+
"""Return the normalized handshake, or ``None`` if ``message`` is not one.
|
|
28
|
+
|
|
29
|
+
Accepts ``{"type": "efficientai_trace_handshake", "call_short_id": ...}`` and the
|
|
30
|
+
older ``{"efficientai_call_short_id": ...}`` form.
|
|
31
|
+
"""
|
|
32
|
+
if not isinstance(message, Mapping):
|
|
33
|
+
return None
|
|
34
|
+
|
|
35
|
+
if message.get("type") == HANDSHAKE_TYPE:
|
|
36
|
+
call_short_id = message.get("call_short_id")
|
|
37
|
+
return _normalized(message, call_short_id) if call_short_id else None
|
|
38
|
+
|
|
39
|
+
legacy_id = message.get(LEGACY_CALL_SHORT_ID_KEY)
|
|
40
|
+
if legacy_id:
|
|
41
|
+
return _normalized(message, legacy_id)
|
|
42
|
+
return None
|