File size: 9,982 Bytes
9ab69a5
33ef0bb
 
a89ae13
33ef0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a89ae13
33ef0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
9ab69a5
 
 
 
 
 
 
 
 
 
33ef0bb
 
9ab69a5
33ef0bb
 
 
 
9ab69a5
33ef0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
Smriti MCP — HTTP Client
=========================
Async HTTP client that wraps the Smriti REST API.
Handles authentication, retries, timeouts, and error mapping.

All methods return plain Python dicts — the MCP server layer
converts them to tool responses.
"""

from __future__ import annotations

import logging
import sys
from datetime import datetime
from typing import Any, Optional

import httpx

from .config import SmritiMCPConfig

# All logging goes to stderr (MCP requirement — stdout is the JSON-RPC channel)
logger = logging.getLogger("smriti.mcp.client")
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(logging.Formatter("%(asctime)s | %(name)s | %(levelname)s | %(message)s"))
logger.addHandler(handler)
logger.setLevel(logging.INFO)


class SmritiAPIError(Exception):
    """Raised when the Smriti API returns an error response."""

    def __init__(self, status_code: int, detail: str):
        self.status_code = status_code
        self.detail = detail
        super().__init__(f"Smriti API error {status_code}: {detail}")


class SmritiClient:
    """
    Async HTTP client for the Smriti temporal memory API.

    Usage:
        client = SmritiClient(config)
        await client.connect()
        result = await client.ingest("User started learning Rust")
        await client.close()
    """

    def __init__(self, config: SmritiMCPConfig):
        self.config = config
        self._client: Optional[httpx.AsyncClient] = None

    async def connect(self) -> None:
        """Initialize the HTTP client with auth headers."""
        headers: dict[str, str] = {
            "X-API-Key": self.config.api_key,
            "Content-Type": "application/json",
            "User-Agent": "smriti-mcp/0.1.0",
        }
        # Forward Supabase BYODB URL when configured — applies to all tool calls
        if self.config.supabase_url:
            headers["X-Supabase-Url"] = self.config.supabase_url
            logger.info("Supabase BYODB active — memory routed to user's Supabase DB")

        self._client = httpx.AsyncClient(
            base_url=self.config.base_url.rstrip("/"),
            headers=headers,
            timeout=httpx.Timeout(self.config.timeout_seconds),
        )
        logger.info(f"SmritiClient connected to {self.config.base_url}")


    async def close(self) -> None:
        """Close the HTTP client."""
        if self._client:
            await self._client.aclose()
            self._client = None
            logger.info("SmritiClient disconnected")

    # ── Internal helpers ──────────────────────────────────────────────────────

    def _ensure_connected(self) -> httpx.AsyncClient:
        if self._client is None:
            raise RuntimeError("SmritiClient not connected. Call connect() first.")
        return self._client

    async def _request(
        self,
        method: str,
        path: str,
        json: dict | None = None,
        params: dict | None = None,
    ) -> dict[str, Any]:
        """Make an HTTP request with retry logic."""
        client = self._ensure_connected()
        last_error: Exception | None = None

        for attempt in range(1, self.config.max_retries + 1):
            try:
                response = await client.request(
                    method=method,
                    url=path,
                    json=json,
                    params=params,
                )

                if response.status_code == 200:
                    return response.json()

                # Non-retryable errors
                if response.status_code in (400, 401, 403, 404, 409, 422):
                    detail = self._extract_error_detail(response)
                    raise SmritiAPIError(response.status_code, detail)

                # Retryable errors (429, 500, 502, 503, 504)
                detail = self._extract_error_detail(response)
                last_error = SmritiAPIError(response.status_code, detail)
                logger.warning(
                    f"Retryable error (attempt {attempt}/{self.config.max_retries}): "
                    f"{response.status_code} {detail}"
                )

            except httpx.TimeoutException as e:
                last_error = e
                logger.warning(
                    f"Timeout (attempt {attempt}/{self.config.max_retries}): {e}"
                )
            except httpx.ConnectError as e:
                last_error = e
                logger.warning(
                    f"Connection error (attempt {attempt}/{self.config.max_retries}): {e}"
                )
            except SmritiAPIError:
                raise  # Non-retryable, already raised

        # All retries exhausted
        raise SmritiAPIError(
            503,
            f"All {self.config.max_retries} retries exhausted. Last error: {last_error}",
        )

    @staticmethod
    def _extract_error_detail(response: httpx.Response) -> str:
        """Extract a human-readable error message from an API response."""
        try:
            data = response.json()
            return data.get("detail", str(data))
        except Exception:
            return response.text[:500]

    # ── Public API methods ────────────────────────────────────────────────────

    async def ingest(
        self,
        text: str,
        source_id: str | None = None,
        parse_svo: bool = True,
        timestamp: str | None = None,
        scope: str | None = None,
        metadata: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """
        Ingest text into Smriti temporal memory.

        The text is automatically decomposed into S-V-O (Subject-Verb-Object)
        causal event tuples and stored with bi-temporal validity.

        Args:
            text: The memory to store (e.g., "User switched from React to Vue")
            source_id: Namespace label for this memory source
            parse_svo: Whether to extract S-V-O tuples (uses LLM)
            timestamp: ISO timestamp override (default: now)
            scope: Logical namespace (e.g., "work", "personal")
            metadata: Additional metadata to attach to events

        Returns:
            Dict with ingested_count, event_ids, svo_tuples, turn_ids
        """
        event: dict[str, Any] = {"text": text}
        if timestamp:
            event["timestamp"] = timestamp
        if metadata:
            event["metadata"] = metadata
        if scope:
            event["scope"] = scope

        payload = {
            "source_id": source_id or self.config.source_id,
            "events": [event],
            "parse_svo": parse_svo,
        }
        if scope:
            payload["scope"] = scope

        return await self._request("POST", "/ingest", json=payload)

    async def query(
        self,
        query: str,
        max_results: int | None = None,
        source_ids: list[str] | None = None,
        time_range_start: str | None = None,
        time_range_end: str | None = None,
        scope: str | None = None,
        semantic_weight: float = 0.5,
    ) -> dict[str, Any]:
        """
        Search Smriti temporal memory with hybrid retrieval.

        Uses a 3-phase pipeline:
        1. Semantic search via pgvector (fuzzy recall)
        2. Temporal filtering via PostgreSQL (deterministic time ranges)
        3. Entity multi-hop (capitalized entity matching)

        Args:
            query: Natural language search query
            max_results: Maximum results to return
            source_ids: Filter by specific source namespaces
            time_range_start: ISO timestamp for range start
            time_range_end: ISO timestamp for range end
            scope: Restrict to a specific scope
            semantic_weight: Balance between semantic (1.0) and temporal (0.0)

        Returns:
            Dict with results (list of events), total_found, query_time_ms
        """
        payload: dict[str, Any] = {
            "query": query,
            "max_results": max_results or self.config.max_results,
            "semantic_weight": semantic_weight,
        }
        if source_ids:
            payload["source_ids"] = source_ids
        if scope:
            payload["scope"] = scope

        if time_range_start or time_range_end:
            time_range: dict[str, str] = {}
            if time_range_start:
                time_range["start"] = time_range_start
            if time_range_end:
                time_range["end"] = time_range_end
            payload["time_range"] = time_range

        return await self._request("POST", "/query", json=payload)

    async def health(self) -> dict[str, Any]:
        """
        Check Smriti API health status.

        Returns:
            Dict with status, postgres_events count, pgvector_embeddings count
        """
        return await self._request("GET", "/health")

    async def usage(self) -> dict[str, Any]:
        """
        Get current API usage statistics and tier limits.

        Returns:
            Dict with tier, usage (events/orchestration/connectors), limits
        """
        return await self._request("GET", "/billing/usage")

    async def list_connectors(self) -> list[dict[str, Any]]:
        """
        List all registered SaaS tool connectors.

        Returns:
            List of connector dicts with id, name, description, endpoints_count
        """
        result = await self._request("GET", "/connectors")
        # The API returns a list directly, not wrapped in an object
        return result if isinstance(result, list) else []

    async def root_health(self) -> dict[str, Any]:
        """
        Basic health check (GET /).

        Returns:
            Dict with service name, version, status
        """
        return await self._request("GET", "/")