tenxyte-langchain 0.1.0__tar.gz
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.
- tenxyte_langchain-0.1.0/.gitignore +114 -0
- tenxyte_langchain-0.1.0/PKG-INFO +129 -0
- tenxyte_langchain-0.1.0/README.md +97 -0
- tenxyte_langchain-0.1.0/pyproject.toml +54 -0
- tenxyte_langchain-0.1.0/pytest.ini +2 -0
- tenxyte_langchain-0.1.0/src/tenxyte_langchain/__init__.py +43 -0
- tenxyte_langchain-0.1.0/src/tenxyte_langchain/auth.py +77 -0
- tenxyte_langchain-0.1.0/src/tenxyte_langchain/callbacks.py +76 -0
- tenxyte_langchain-0.1.0/src/tenxyte_langchain/client.py +209 -0
- tenxyte_langchain-0.1.0/src/tenxyte_langchain/exceptions.py +62 -0
- tenxyte_langchain-0.1.0/src/tenxyte_langchain/hitl.py +71 -0
- tenxyte_langchain-0.1.0/src/tenxyte_langchain/trace.py +18 -0
- tenxyte_langchain-0.1.0/tests/__init__.py +0 -0
- tenxyte_langchain-0.1.0/tests/conftest.py +33 -0
- tenxyte_langchain-0.1.0/tests/test_client.py +131 -0
- tenxyte_langchain-0.1.0/tests/test_import_graph.py +56 -0
- tenxyte_langchain-0.1.0/tests/test_property_auth.py +78 -0
- tenxyte_langchain-0.1.0/tests/test_property_budget.py +81 -0
- tenxyte_langchain-0.1.0/tests/test_property_failsafe.py +126 -0
- tenxyte_langchain-0.1.0/tests/test_property_hitl.py +68 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
lib/
|
|
14
|
+
lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
pip-wheel-metadata/
|
|
20
|
+
share/python-wheels/
|
|
21
|
+
*.egg-info/
|
|
22
|
+
.installed.cfg
|
|
23
|
+
*.egg
|
|
24
|
+
MANIFEST
|
|
25
|
+
|
|
26
|
+
# Virtual environments
|
|
27
|
+
venv/
|
|
28
|
+
ENV/
|
|
29
|
+
env/
|
|
30
|
+
.venv
|
|
31
|
+
|
|
32
|
+
# IDE
|
|
33
|
+
.vscode/
|
|
34
|
+
.idea/
|
|
35
|
+
*.swp
|
|
36
|
+
*.swo
|
|
37
|
+
*~
|
|
38
|
+
|
|
39
|
+
# Testing
|
|
40
|
+
.coverage
|
|
41
|
+
.coverage.*
|
|
42
|
+
htmlcov/
|
|
43
|
+
.pytest_cache/
|
|
44
|
+
.tox/
|
|
45
|
+
.nox/
|
|
46
|
+
coverage.xml
|
|
47
|
+
*.cover
|
|
48
|
+
|
|
49
|
+
checklist.md
|
|
50
|
+
log.md
|
|
51
|
+
site
|
|
52
|
+
site/*
|
|
53
|
+
|
|
54
|
+
next-steps.md
|
|
55
|
+
next-steps-phases.md
|
|
56
|
+
|
|
57
|
+
# Django
|
|
58
|
+
*.log
|
|
59
|
+
local_settings.py
|
|
60
|
+
db.sqlite3
|
|
61
|
+
db.sqlite3-journal
|
|
62
|
+
media/
|
|
63
|
+
staticfiles/
|
|
64
|
+
|
|
65
|
+
# Jupyter Notebook
|
|
66
|
+
.ipynb_checkpoints
|
|
67
|
+
|
|
68
|
+
# pyenv
|
|
69
|
+
.python-version
|
|
70
|
+
|
|
71
|
+
# mypy
|
|
72
|
+
.mypy_cache/
|
|
73
|
+
.dmypy.json
|
|
74
|
+
dmypy.json
|
|
75
|
+
|
|
76
|
+
# Pyre type checker
|
|
77
|
+
.pyre/
|
|
78
|
+
|
|
79
|
+
# OS
|
|
80
|
+
.DS_Store
|
|
81
|
+
Thumbs.db
|
|
82
|
+
|
|
83
|
+
# Distribution / packaging
|
|
84
|
+
*.tar.gz
|
|
85
|
+
*.whl
|
|
86
|
+
|
|
87
|
+
old_dists/
|
|
88
|
+
old_dists/*
|
|
89
|
+
old_dists/*/*
|
|
90
|
+
|
|
91
|
+
# Hypothesis
|
|
92
|
+
.hypothesis/
|
|
93
|
+
|
|
94
|
+
/plan
|
|
95
|
+
logs
|
|
96
|
+
|
|
97
|
+
# AIRS Conformance Suite ephemeral CI instance (spec z_aud_2)
|
|
98
|
+
spec/airs/conformance/tenxyte_ci/_ephemeral.sqlite3
|
|
99
|
+
spec/airs/conformance/tenxyte_ci/_ci_info.json
|
|
100
|
+
|
|
101
|
+
# AIRS Conformance Suite ephemeral FastAPI CI instance (spec z_aud_3, Wave F, task 9.2)
|
|
102
|
+
spec/airs/conformance/tenxyte_ci_fastapi/_ephemeral.sqlite3
|
|
103
|
+
spec/airs/conformance/tenxyte_ci_fastapi/_ci_info.json
|
|
104
|
+
|
|
105
|
+
# AIRS benchmark raw output (RESULTS.md is the canonical human-readable record)
|
|
106
|
+
benchmarks/airs/results_*.json
|
|
107
|
+
|
|
108
|
+
# js-contract-backend ephemeral demo instance (spec z_aud_3, task 14.2) — written to the current
|
|
109
|
+
# working directory wherever seed_and_serve.py is launched from (repo root when run directly,
|
|
110
|
+
# /app inside the container), recreated on every run, same convention as the AIRS Conformance
|
|
111
|
+
# Suite's own ephemeral instances above.
|
|
112
|
+
js-contract-backend.db
|
|
113
|
+
|
|
114
|
+
manual_tests
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: tenxyte-langchain
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: LangChain/LangGraph connector for AIRS/1.0 — AI agent identity, delegation, and Human-in-the-Loop governance. Pure HTTP client, no dependency on the tenxyte package.
|
|
5
|
+
Project-URL: Homepage, https://github.com/tenxyte/tenxyte
|
|
6
|
+
Project-URL: Specification, https://github.com/tenxyte/tenxyte/blob/main/spec/airs/AIRS-1.0.md
|
|
7
|
+
Project-URL: Documentation, https://github.com/tenxyte/tenxyte/tree/main/integrations/langchain
|
|
8
|
+
Author: Tenxyte
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
Keywords: agent-identity,ai-agents,airs,hitl,langchain,langgraph
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Security
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Requires-Dist: httpx>=0.27
|
|
23
|
+
Requires-Dist: langchain-core>=0.3
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: black==26.5.1; extra == 'dev'
|
|
26
|
+
Requires-Dist: hypothesis>=6.0; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
|
|
28
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
29
|
+
Requires-Dist: respx>=0.21; extra == 'dev'
|
|
30
|
+
Requires-Dist: ruff==0.16.7; extra == 'dev'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# tenxyte-langchain
|
|
34
|
+
|
|
35
|
+
LangChain / LangGraph connector for [AIRS/1.0](../../spec/airs/AIRS-1.0.md) — the open
|
|
36
|
+
specification for AI agent identity, delegation, and Human-in-the-Loop governance that
|
|
37
|
+
[Tenxyte](https://github.com/tenxyte/tenxyte) implements.
|
|
38
|
+
|
|
39
|
+
A pure HTTP client: it never imports the `tenxyte` package, and works against any
|
|
40
|
+
AIRS-conformant server (discovered at runtime, never hardcoded).
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install tenxyte-langchain
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Minimal example
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import httpx
|
|
52
|
+
from tenxyte_langchain import TenxyteAIRSClient, TenxyteAgentAuth, TenxyteHITLPending
|
|
53
|
+
|
|
54
|
+
# 1. A human issues the Agent Token once, out-of-band (e.g. via Tenxyte's own /ai/tokens/ API
|
|
55
|
+
# with their own Bearer JWT) — this connector never creates tokens itself.
|
|
56
|
+
AGENT_TOKEN = "the-raw-token-value-returned-at-issuance"
|
|
57
|
+
TOKEN_ID = 42
|
|
58
|
+
|
|
59
|
+
client = TenxyteAIRSClient("https://your-tenxyte-server.example.com", agent_token=AGENT_TOKEN)
|
|
60
|
+
|
|
61
|
+
auth = TenxyteAgentAuth(
|
|
62
|
+
AGENT_TOKEN,
|
|
63
|
+
client=client,
|
|
64
|
+
token_id=TOKEN_ID,
|
|
65
|
+
heartbeat_required_every=300, # matches what the token was issued with, if a DMS is set
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
with httpx.Client(base_url=client.base_url, auth=auth) as http:
|
|
69
|
+
# Every request through `http` now carries AgentBearer + X-Prompt-Trace-ID automatically.
|
|
70
|
+
response = http.post(client.endpoint("report_usage", id=TOKEN_ID), json={"cost_usd": 0.01})
|
|
71
|
+
|
|
72
|
+
try:
|
|
73
|
+
client.request("POST", "/business/invoices/", json={"amount": 500})
|
|
74
|
+
except TenxyteHITLPending as pending:
|
|
75
|
+
# A human approves pending.confirmation_token through Tenxyte's own API — outside this
|
|
76
|
+
# package, by design (see spec/airs/AIRS-1.0.md §11, privilege amplification).
|
|
77
|
+
...
|
|
78
|
+
response = pending.resume() # after approval
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Budget tracking with a LangChain chain
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
from tenxyte_langchain import TenxyteBudgetCallbackHandler, TenxyteBudgetExceededError
|
|
85
|
+
|
|
86
|
+
budget_callback = TenxyteBudgetCallbackHandler(
|
|
87
|
+
client, TOKEN_ID, cost_per_prompt_token=3e-6, cost_per_completion_token=15e-6
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
try:
|
|
91
|
+
llm.invoke("...", config={"callbacks": [budget_callback]})
|
|
92
|
+
except TenxyteBudgetExceededError:
|
|
93
|
+
# The token has been suspended server-side — stop the agent, don't retry.
|
|
94
|
+
raise
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## HITL with LangGraph `interrupt()`
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from langgraph.types import interrupt
|
|
101
|
+
from tenxyte_langchain import TenxyteHITLPending
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def sensitive_tool(payload):
|
|
105
|
+
try:
|
|
106
|
+
return client.request("POST", "/business/refunds/", json=payload)
|
|
107
|
+
except TenxyteHITLPending as pending:
|
|
108
|
+
# Suspend the graph; a human approves confirmation_token via Tenxyte's own API.
|
|
109
|
+
interrupt({"confirmation_token": pending.confirmation_token, "expires_at": pending.expires_at})
|
|
110
|
+
return pending.resume()
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Design notes
|
|
114
|
+
|
|
115
|
+
- **`TenxyteAIRSClient`**: low-level, resolves every endpoint through the target's Discovery
|
|
116
|
+
Endpoint — nothing is hardcoded.
|
|
117
|
+
- **`TenxyteAgentAuth`**: an `httpx.Auth` — attach it to any `httpx.Client(auth=...)` for
|
|
118
|
+
automatic `AgentBearer` + `X-Prompt-Trace-ID` on every request, plus an optional background
|
|
119
|
+
heartbeat thread.
|
|
120
|
+
- **Fail-safe by design**: every terminal Agent Token condition (suspended, revoked, expired,
|
|
121
|
+
budget-exceeded) raises a distinct, typed exception. None of them are meant to be retried with
|
|
122
|
+
the same token — see [AIRS-CORE-17] and [AIRS-CORE-22] in the specification.
|
|
123
|
+
- **No privilege amplification**: this package never creates Agent Tokens and never confirms or
|
|
124
|
+
denies a HITL action itself — both stay strictly on the human side of Tenxyte's own API.
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
MIT (this package). The AIRS/1.0 specification it implements is separately licensed CC BY 4.0 —
|
|
129
|
+
see [`spec/airs/LICENSE`](../../spec/airs/LICENSE).
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# tenxyte-langchain
|
|
2
|
+
|
|
3
|
+
LangChain / LangGraph connector for [AIRS/1.0](../../spec/airs/AIRS-1.0.md) — the open
|
|
4
|
+
specification for AI agent identity, delegation, and Human-in-the-Loop governance that
|
|
5
|
+
[Tenxyte](https://github.com/tenxyte/tenxyte) implements.
|
|
6
|
+
|
|
7
|
+
A pure HTTP client: it never imports the `tenxyte` package, and works against any
|
|
8
|
+
AIRS-conformant server (discovered at runtime, never hardcoded).
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install tenxyte-langchain
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Minimal example
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
import httpx
|
|
20
|
+
from tenxyte_langchain import TenxyteAIRSClient, TenxyteAgentAuth, TenxyteHITLPending
|
|
21
|
+
|
|
22
|
+
# 1. A human issues the Agent Token once, out-of-band (e.g. via Tenxyte's own /ai/tokens/ API
|
|
23
|
+
# with their own Bearer JWT) — this connector never creates tokens itself.
|
|
24
|
+
AGENT_TOKEN = "the-raw-token-value-returned-at-issuance"
|
|
25
|
+
TOKEN_ID = 42
|
|
26
|
+
|
|
27
|
+
client = TenxyteAIRSClient("https://your-tenxyte-server.example.com", agent_token=AGENT_TOKEN)
|
|
28
|
+
|
|
29
|
+
auth = TenxyteAgentAuth(
|
|
30
|
+
AGENT_TOKEN,
|
|
31
|
+
client=client,
|
|
32
|
+
token_id=TOKEN_ID,
|
|
33
|
+
heartbeat_required_every=300, # matches what the token was issued with, if a DMS is set
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
with httpx.Client(base_url=client.base_url, auth=auth) as http:
|
|
37
|
+
# Every request through `http` now carries AgentBearer + X-Prompt-Trace-ID automatically.
|
|
38
|
+
response = http.post(client.endpoint("report_usage", id=TOKEN_ID), json={"cost_usd": 0.01})
|
|
39
|
+
|
|
40
|
+
try:
|
|
41
|
+
client.request("POST", "/business/invoices/", json={"amount": 500})
|
|
42
|
+
except TenxyteHITLPending as pending:
|
|
43
|
+
# A human approves pending.confirmation_token through Tenxyte's own API — outside this
|
|
44
|
+
# package, by design (see spec/airs/AIRS-1.0.md §11, privilege amplification).
|
|
45
|
+
...
|
|
46
|
+
response = pending.resume() # after approval
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Budget tracking with a LangChain chain
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from tenxyte_langchain import TenxyteBudgetCallbackHandler, TenxyteBudgetExceededError
|
|
53
|
+
|
|
54
|
+
budget_callback = TenxyteBudgetCallbackHandler(
|
|
55
|
+
client, TOKEN_ID, cost_per_prompt_token=3e-6, cost_per_completion_token=15e-6
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
try:
|
|
59
|
+
llm.invoke("...", config={"callbacks": [budget_callback]})
|
|
60
|
+
except TenxyteBudgetExceededError:
|
|
61
|
+
# The token has been suspended server-side — stop the agent, don't retry.
|
|
62
|
+
raise
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## HITL with LangGraph `interrupt()`
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from langgraph.types import interrupt
|
|
69
|
+
from tenxyte_langchain import TenxyteHITLPending
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def sensitive_tool(payload):
|
|
73
|
+
try:
|
|
74
|
+
return client.request("POST", "/business/refunds/", json=payload)
|
|
75
|
+
except TenxyteHITLPending as pending:
|
|
76
|
+
# Suspend the graph; a human approves confirmation_token via Tenxyte's own API.
|
|
77
|
+
interrupt({"confirmation_token": pending.confirmation_token, "expires_at": pending.expires_at})
|
|
78
|
+
return pending.resume()
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Design notes
|
|
82
|
+
|
|
83
|
+
- **`TenxyteAIRSClient`**: low-level, resolves every endpoint through the target's Discovery
|
|
84
|
+
Endpoint — nothing is hardcoded.
|
|
85
|
+
- **`TenxyteAgentAuth`**: an `httpx.Auth` — attach it to any `httpx.Client(auth=...)` for
|
|
86
|
+
automatic `AgentBearer` + `X-Prompt-Trace-ID` on every request, plus an optional background
|
|
87
|
+
heartbeat thread.
|
|
88
|
+
- **Fail-safe by design**: every terminal Agent Token condition (suspended, revoked, expired,
|
|
89
|
+
budget-exceeded) raises a distinct, typed exception. None of them are meant to be retried with
|
|
90
|
+
the same token — see [AIRS-CORE-17] and [AIRS-CORE-22] in the specification.
|
|
91
|
+
- **No privilege amplification**: this package never creates Agent Tokens and never confirms or
|
|
92
|
+
denies a HITL action itself — both stay strictly on the human side of Tenxyte's own API.
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
MIT (this package). The AIRS/1.0 specification it implements is separately licensed CC BY 4.0 —
|
|
97
|
+
see [`spec/airs/LICENSE`](../../spec/airs/LICENSE).
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tenxyte-langchain"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "LangChain/LangGraph connector for AIRS/1.0 — AI agent identity, delegation, and Human-in-the-Loop governance. Pure HTTP client, no dependency on the tenxyte package."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [{ name = "Tenxyte" }]
|
|
13
|
+
keywords = ["langchain", "langgraph", "ai-agents", "airs", "agent-identity", "hitl"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Topic :: Security",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"httpx>=0.27",
|
|
28
|
+
"langchain-core>=0.3",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
dev = [
|
|
33
|
+
"pytest>=8.0",
|
|
34
|
+
"pytest-asyncio>=0.24",
|
|
35
|
+
"respx>=0.21",
|
|
36
|
+
"hypothesis>=6.0",
|
|
37
|
+
"ruff==0.16.7",
|
|
38
|
+
"black==26.5.1",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.urls]
|
|
42
|
+
Homepage = "https://github.com/tenxyte/tenxyte"
|
|
43
|
+
Specification = "https://github.com/tenxyte/tenxyte/blob/main/spec/airs/AIRS-1.0.md"
|
|
44
|
+
Documentation = "https://github.com/tenxyte/tenxyte/tree/main/integrations/langchain"
|
|
45
|
+
|
|
46
|
+
[tool.hatch.build.targets.wheel]
|
|
47
|
+
packages = ["src/tenxyte_langchain"]
|
|
48
|
+
|
|
49
|
+
[tool.ruff]
|
|
50
|
+
line-length = 120
|
|
51
|
+
extend-exclude = ["tests"]
|
|
52
|
+
|
|
53
|
+
[tool.black]
|
|
54
|
+
line-length = 120
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""
|
|
2
|
+
tenxyte-langchain — LangChain/LangGraph connector for AIRS/1.0 (spec/airs/AIRS-1.0.md).
|
|
3
|
+
|
|
4
|
+
A pure HTTP client: never imports the `tenxyte` package (HTTP_Only_Rule, design.md D2). Everything
|
|
5
|
+
it does is speak the wire protocol Tenxyte's reference implementation formalizes, discovered at
|
|
6
|
+
runtime from the target server — it works against any AIRS-conformant server.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from .auth import TenxyteAgentAuth
|
|
10
|
+
from .callbacks import TenxyteBudgetCallbackHandler
|
|
11
|
+
from .client import TenxyteAIRSClient
|
|
12
|
+
from .exceptions import (
|
|
13
|
+
TenxyteAgentTokenError,
|
|
14
|
+
TenxyteAIRSError,
|
|
15
|
+
TenxyteAIRSUnavailable,
|
|
16
|
+
TenxyteBudgetExceededError,
|
|
17
|
+
TenxyteCapabilityError,
|
|
18
|
+
TenxytePermissionDeniedError,
|
|
19
|
+
TenxyteTokenExpiredError,
|
|
20
|
+
TenxyteTokenNotFoundError,
|
|
21
|
+
TenxyteTokenRevokedError,
|
|
22
|
+
TenxyteTokenSuspendedError,
|
|
23
|
+
)
|
|
24
|
+
from .hitl import TenxyteHITLPending
|
|
25
|
+
|
|
26
|
+
__version__ = "0.1.0"
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"TenxyteAIRSClient",
|
|
30
|
+
"TenxyteAIRSError",
|
|
31
|
+
"TenxyteAIRSUnavailable",
|
|
32
|
+
"TenxyteAgentAuth",
|
|
33
|
+
"TenxyteAgentTokenError",
|
|
34
|
+
"TenxyteBudgetCallbackHandler",
|
|
35
|
+
"TenxyteBudgetExceededError",
|
|
36
|
+
"TenxyteCapabilityError",
|
|
37
|
+
"TenxyteHITLPending",
|
|
38
|
+
"TenxytePermissionDeniedError",
|
|
39
|
+
"TenxyteTokenExpiredError",
|
|
40
|
+
"TenxyteTokenNotFoundError",
|
|
41
|
+
"TenxyteTokenRevokedError",
|
|
42
|
+
"TenxyteTokenSuspendedError",
|
|
43
|
+
]
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""
|
|
2
|
+
`TenxyteAgentAuth` — per-request authentication + background heartbeat (spec z_aud_2,
|
|
3
|
+
Requirement 4.3/4.4/4.7).
|
|
4
|
+
|
|
5
|
+
An `httpx.Auth` implementation: attach it to any `httpx.Client(auth=...)` and every outgoing
|
|
6
|
+
request gets `Authorization: AgentBearer <token>` and `X-Prompt-Trace-ID` automatically
|
|
7
|
+
([AIRS-CORE-11], [AIRS-TRACE-1]). When the Agent Token declares a heartbeat requirement, it also
|
|
8
|
+
runs a background daemon thread sending heartbeats at a safety margin below the required
|
|
9
|
+
interval (design.md: `max(1, heartbeat_required_every // 2)`), never propagating a failure —
|
|
10
|
+
the server's own suspension is the authority, not this client (design.md "Décision de conception
|
|
11
|
+
: heartbeat en tâche de fond").
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import threading
|
|
17
|
+
from collections.abc import Callable
|
|
18
|
+
from typing import TYPE_CHECKING
|
|
19
|
+
|
|
20
|
+
import httpx
|
|
21
|
+
|
|
22
|
+
from .trace import resolve_trace_id
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from .client import TenxyteAIRSClient
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def heartbeat_interval_seconds(heartbeat_required_every: int) -> int:
|
|
29
|
+
"""[AIRS-GUARDS-4]-safe interval: strictly below the server's requirement, floor 1s."""
|
|
30
|
+
return max(1, heartbeat_required_every // 2)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class TenxyteAgentAuth(httpx.Auth):
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
agent_token: str,
|
|
37
|
+
*,
|
|
38
|
+
client: TenxyteAIRSClient | None = None,
|
|
39
|
+
token_id: object | None = None,
|
|
40
|
+
heartbeat_required_every: int | None = None,
|
|
41
|
+
run_id_provider: Callable[[], str | None] | None = None,
|
|
42
|
+
):
|
|
43
|
+
self._agent_token = agent_token
|
|
44
|
+
self._run_id_provider = run_id_provider or (lambda: None)
|
|
45
|
+
|
|
46
|
+
self.heartbeat_failed = False
|
|
47
|
+
self._stop_event = threading.Event()
|
|
48
|
+
self._thread: threading.Thread | None = None
|
|
49
|
+
|
|
50
|
+
if heartbeat_required_every and client is not None and token_id is not None:
|
|
51
|
+
interval = heartbeat_interval_seconds(heartbeat_required_every)
|
|
52
|
+
self._thread = threading.Thread(target=self._heartbeat_loop, args=(client, token_id, interval), daemon=True)
|
|
53
|
+
self._thread.start()
|
|
54
|
+
|
|
55
|
+
def auth_flow(self, request: httpx.Request):
|
|
56
|
+
request.headers["Authorization"] = f"AgentBearer {self._agent_token}"
|
|
57
|
+
request.headers.setdefault("X-Prompt-Trace-ID", resolve_trace_id(self._run_id_provider()))
|
|
58
|
+
yield request
|
|
59
|
+
|
|
60
|
+
def _heartbeat_loop(self, client: TenxyteAIRSClient, token_id: object, interval: float) -> None:
|
|
61
|
+
while not self._stop_event.wait(interval):
|
|
62
|
+
try:
|
|
63
|
+
client.heartbeat(token_id)
|
|
64
|
+
self.heartbeat_failed = False
|
|
65
|
+
except Exception: # noqa: BLE001 — never propagate from a background thread
|
|
66
|
+
self.heartbeat_failed = True
|
|
67
|
+
|
|
68
|
+
def close(self) -> None:
|
|
69
|
+
self._stop_event.set()
|
|
70
|
+
if self._thread is not None:
|
|
71
|
+
self._thread.join(timeout=5)
|
|
72
|
+
|
|
73
|
+
def __enter__(self) -> TenxyteAgentAuth: # noqa: PYI034 — no typing.Self on the Python 3.10 floor
|
|
74
|
+
return self
|
|
75
|
+
|
|
76
|
+
def __exit__(self, *exc_info: object) -> None:
|
|
77
|
+
self.close()
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""
|
|
2
|
+
`TenxyteBudgetCallbackHandler` — LangChain callback reporting LLM usage to AIRS's Budget
|
|
3
|
+
Guardrail (spec z_aud_2, Requirement 4.5; spec/airs/AIRS-1.0.md §6.3).
|
|
4
|
+
|
|
5
|
+
Tenxyte does not compute cost from token counts itself ([AIRS-BUDGET-2]) — that stays the
|
|
6
|
+
caller's responsibility, so this handler accepts either a linear per-token price or a custom
|
|
7
|
+
`cost_fn(prompt_tokens, completion_tokens) -> float`.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
from typing import TYPE_CHECKING, Any
|
|
14
|
+
|
|
15
|
+
from langchain_core.callbacks import BaseCallbackHandler
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from .client import TenxyteAIRSClient
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _extract_usage(response: Any) -> tuple[int, int]:
|
|
22
|
+
"""
|
|
23
|
+
Read either LangChain usage shape: legacy `llm_output.token_usage`/`.usage` (OpenAI-style
|
|
24
|
+
dict with `prompt_tokens`/`completion_tokens` or `input_tokens`/`output_tokens`), or the
|
|
25
|
+
newer per-generation `message.usage_metadata` (`input_tokens`/`output_tokens`).
|
|
26
|
+
"""
|
|
27
|
+
prompt_tokens = 0
|
|
28
|
+
completion_tokens = 0
|
|
29
|
+
|
|
30
|
+
llm_output = getattr(response, "llm_output", None) or {}
|
|
31
|
+
usage = llm_output.get("token_usage") or llm_output.get("usage")
|
|
32
|
+
if usage:
|
|
33
|
+
prompt_tokens += usage.get("prompt_tokens", usage.get("input_tokens", 0)) or 0
|
|
34
|
+
completion_tokens += usage.get("completion_tokens", usage.get("output_tokens", 0)) or 0
|
|
35
|
+
return prompt_tokens, completion_tokens
|
|
36
|
+
|
|
37
|
+
for generation_list in getattr(response, "generations", None) or []:
|
|
38
|
+
for generation in generation_list:
|
|
39
|
+
message = getattr(generation, "message", None)
|
|
40
|
+
usage_metadata = getattr(message, "usage_metadata", None) if message else None
|
|
41
|
+
if usage_metadata:
|
|
42
|
+
prompt_tokens += usage_metadata.get("input_tokens", 0) or 0
|
|
43
|
+
completion_tokens += usage_metadata.get("output_tokens", 0) or 0
|
|
44
|
+
|
|
45
|
+
return prompt_tokens, completion_tokens
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class TenxyteBudgetCallbackHandler(BaseCallbackHandler):
|
|
49
|
+
def __init__(
|
|
50
|
+
self,
|
|
51
|
+
client: TenxyteAIRSClient,
|
|
52
|
+
token_id: object,
|
|
53
|
+
*,
|
|
54
|
+
cost_per_prompt_token: float = 0.0,
|
|
55
|
+
cost_per_completion_token: float = 0.0,
|
|
56
|
+
cost_fn: Callable[[int, int], float] | None = None,
|
|
57
|
+
):
|
|
58
|
+
self._client = client
|
|
59
|
+
self._token_id = token_id
|
|
60
|
+
self._cost_per_prompt_token = cost_per_prompt_token
|
|
61
|
+
self._cost_per_completion_token = cost_per_completion_token
|
|
62
|
+
self._cost_fn = cost_fn
|
|
63
|
+
|
|
64
|
+
def _cost_usd(self, prompt_tokens: int, completion_tokens: int) -> float:
|
|
65
|
+
if self._cost_fn is not None:
|
|
66
|
+
return self._cost_fn(prompt_tokens, completion_tokens)
|
|
67
|
+
return prompt_tokens * self._cost_per_prompt_token + completion_tokens * self._cost_per_completion_token
|
|
68
|
+
|
|
69
|
+
def on_llm_end(self, response: Any, **kwargs: Any) -> None:
|
|
70
|
+
prompt_tokens, completion_tokens = _extract_usage(response)
|
|
71
|
+
cost_usd = self._cost_usd(prompt_tokens, completion_tokens)
|
|
72
|
+
# Raises TenxyteBudgetExceededError if this report suspends the token ([AIRS-BUDGET-3]) —
|
|
73
|
+
# intentionally not caught here: the agent stopping is the correct fail-safe behavior.
|
|
74
|
+
self._client.report_usage(
|
|
75
|
+
self._token_id, cost_usd=cost_usd, prompt_tokens=prompt_tokens, completion_tokens=completion_tokens
|
|
76
|
+
)
|