causal-logic-engine 2.3__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.
causal_engine/client.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# causal-engine-sdk/causal_engine/client.py
|
|
2
|
+
import os
|
|
3
|
+
import requests
|
|
4
|
+
from typing import List, Dict, Any, Optional
|
|
5
|
+
from .exceptions import CausalAPIError, AuthenticationError, RateLimitError
|
|
6
|
+
|
|
7
|
+
class AuditResponse:
|
|
8
|
+
"""Wraps the strict JSON schema response from the Causal Engine."""
|
|
9
|
+
def __init__(self, data: Dict[str, Any]):
|
|
10
|
+
self.anomaly_detected: bool = data.get("anomaly_detected", False)
|
|
11
|
+
self.issue_summary: str = data.get("issue_summary", "")
|
|
12
|
+
self.severity_level: str = data.get("severity_level", "NONE")
|
|
13
|
+
self.remedy_steps: List[str] = data.get("remedy_steps", [])
|
|
14
|
+
self.sources: List[Dict[str, Any]] = data.get("sources", [])
|
|
15
|
+
self.latency_ms: int = data.get("latency_ms", 0)
|
|
16
|
+
|
|
17
|
+
def __repr__(self):
|
|
18
|
+
return f"<CausalAudit severity={self.severity_level} anomaly={self.anomaly_detected}>"
|
|
19
|
+
|
|
20
|
+
class CausalClient:
|
|
21
|
+
def __init__(self, api_key: str, base_url: str = "https://causal-logic-engine.onrender.com"):
|
|
22
|
+
"""Initializes the Sovereign API Client wrapper."""
|
|
23
|
+
self.api_key = api_key.strip()
|
|
24
|
+
self.base_url = base_url.rstrip("/")
|
|
25
|
+
|
|
26
|
+
self.headers = {
|
|
27
|
+
"Content-Type": "application/json",
|
|
28
|
+
"X-Causal-Engine-Key": self.api_key
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
def _handle_response(self, response: requests.Response) -> Dict[str, Any]:
|
|
32
|
+
"""Evaluates status codes and handles exceptions professionally."""
|
|
33
|
+
if response.status_code in [200, 201]:
|
|
34
|
+
return response.json()
|
|
35
|
+
|
|
36
|
+
try:
|
|
37
|
+
error_data = response.json()
|
|
38
|
+
detail = error_data.get("detail", "An unhandled API error occurred.")
|
|
39
|
+
except Exception:
|
|
40
|
+
detail = response.text or "An unhandled API error occurred."
|
|
41
|
+
|
|
42
|
+
if response.status_code == 401:
|
|
43
|
+
raise AuthenticationError("Authentication Failed: Invalid or expired API Key.", status_code=401)
|
|
44
|
+
elif response.status_code == 429:
|
|
45
|
+
raise RateLimitError("Quota Exceeded: Your tenant sliding window limit has been hit.", status_code=429)
|
|
46
|
+
else:
|
|
47
|
+
raise CausalAPIError(f"API Error: {detail}", status_code=response.status_code)
|
|
48
|
+
|
|
49
|
+
def query(self, user_query: str, persona_override: Optional[str] = None) -> AuditResponse:
|
|
50
|
+
"""
|
|
51
|
+
Executes a headless cognitive query against your isolated database partition.
|
|
52
|
+
Returns a structured AuditResponse object.
|
|
53
|
+
"""
|
|
54
|
+
url = f"{self.base_url}/api/document/v1/query"
|
|
55
|
+
payload = {
|
|
56
|
+
"user_query": user_query,
|
|
57
|
+
"persona_override": persona_override
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
response = requests.post(url, headers=self.headers, json=payload)
|
|
61
|
+
json_data = self._handle_response(response)
|
|
62
|
+
return AuditResponse(json_data)
|
|
63
|
+
|
|
64
|
+
def upload_file(self, file_path: str) -> Dict[str, Any]:
|
|
65
|
+
"""
|
|
66
|
+
Uploads and indexes a local file (.pdf, .xlsx, .docx, .txt)
|
|
67
|
+
directly into your secure database partition.
|
|
68
|
+
"""
|
|
69
|
+
url = f"{self.base_url}/api/document/upload"
|
|
70
|
+
filename = os.path.basename(file_path)
|
|
71
|
+
|
|
72
|
+
with open(file_path, "rb") as f:
|
|
73
|
+
files = {"file": (filename, f, "application/octet-stream")}
|
|
74
|
+
multipart_headers = {"X-Causal-Engine-Key": self.api_key}
|
|
75
|
+
response = requests.post(url, headers=multipart_headers, files=files)
|
|
76
|
+
|
|
77
|
+
return self._handle_response(response)
|
|
78
|
+
|
|
79
|
+
def list_files(self) -> List[Dict[str, Any]]:
|
|
80
|
+
"""
|
|
81
|
+
Lists all active files in your secure workspace registry.
|
|
82
|
+
Resolves both flat list keys and structured directory registries.
|
|
83
|
+
"""
|
|
84
|
+
url = f"{self.base_url}/api/document/list"
|
|
85
|
+
response = requests.get(url, headers=self.headers)
|
|
86
|
+
data = self._handle_response(response)
|
|
87
|
+
# 🎯 BUG FIX: Resolves both "documents" and "directory_registry" keys seamlessly
|
|
88
|
+
return data.get("documents", data.get("directory_registry", []))
|
|
89
|
+
|
|
90
|
+
def execute_mcp_tool(self, tool_name: str, parameters: Dict[str, Any]) -> Dict[str, Any]:
|
|
91
|
+
"""
|
|
92
|
+
Model Context Protocol (MCP) Gateway.
|
|
93
|
+
Allows external AI agents (Cursor, Claude) to programmatically invoke UCOS tools.
|
|
94
|
+
"""
|
|
95
|
+
url = f"{self.base_url}/api/mcp/execute"
|
|
96
|
+
payload = {
|
|
97
|
+
"tool_name": tool_name,
|
|
98
|
+
"parameters": parameters
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
response = requests.post(url, headers=self.headers, json=payload)
|
|
102
|
+
return self._handle_response(response)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class CausalAPIError(Exception):
|
|
2
|
+
"""Base exception for all Causal Engine API faults."""
|
|
3
|
+
def __init__(self, message: str, status_code: int = None, details: str = None):
|
|
4
|
+
super().__init__(message)
|
|
5
|
+
self.status_code = status_code
|
|
6
|
+
self.details = details
|
|
7
|
+
|
|
8
|
+
class AuthenticationError(CausalAPIError):
|
|
9
|
+
"""Raised when the X-Causal-Engine-Key is invalid or expired."""
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
class RateLimitError(CausalAPIError):
|
|
13
|
+
"""Raised when the workspace exceeds its dynamic tier sliding window limit."""
|
|
14
|
+
pass
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: causal-logic-engine
|
|
3
|
+
Version: 2.3
|
|
4
|
+
Summary: The official Python SDK for the multi-tenant Causal Logic Engine API.
|
|
5
|
+
Home-page: https://causal-logic-engine.onrender.com
|
|
6
|
+
Author: Ben Denholm
|
|
7
|
+
Author-email: bendenholm1@outlook.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
Requires-Dist: requests>=2.25.0
|
|
14
|
+
|
|
15
|
+
Sovereign Cognitive API Client.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
causal_engine/__init__.py,sha256=vm9lRg8KiET-9bm7Om3Q4y-wbT9PrVrYvSRPDICKlNk,195
|
|
2
|
+
causal_engine/client.py,sha256=LyEGaVhCdD1UAhITbOI6vGh8rupgJHR8c9L05B5ClO0,4474
|
|
3
|
+
causal_engine/exceptions.py,sha256=MnSlHOaK9Zv1EfpLbIuZrnaKG17zE5Fv_94N4_9Hy9E,532
|
|
4
|
+
causal_logic_engine-2.3.dist-info/METADATA,sha256=GzEhsmT0hG3KI8bHLVMtjaOutTTJTjelRZ_jFv2bqQ0,524
|
|
5
|
+
causal_logic_engine-2.3.dist-info/WHEEL,sha256=51RkbunBAw4BWsgaQWTpPhg4Diwp3c9P5iaLk67Hdtg,92
|
|
6
|
+
causal_logic_engine-2.3.dist-info/top_level.txt,sha256=2caYqiuD31RZ5eKFvP7HgWs_sNsmg_jk6wzXImtRBeU,14
|
|
7
|
+
causal_logic_engine-2.3.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
causal_engine
|