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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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""Gateway-backed storage for staking intents with local fallback."""
from __future__ import annotations

import copy
import time
import logging
from datetime import datetime, timezone
from threading import Lock
from typing import Any, Dict, List, Optional

from src.integrations.panorama_gateway import (
    PanoramaGatewayClient,
    PanoramaGatewayError,
    PanoramaGatewaySettings,
    get_panorama_settings,
)

STAKING_SESSION_ENTITY = "staking-sessions"
STAKING_HISTORY_ENTITY = "staking-histories"


def _utc_now_iso() -> str:
    return datetime.utcnow().replace(tzinfo=timezone.utc).isoformat()


def _identifier(user_id: str, conversation_id: str) -> str:
    return f"{user_id}:{conversation_id}"


def _as_float(value: Any) -> Optional[float]:
    if value is None:
        return None
    try:
        return float(value)
    except (TypeError, ValueError):
        return None


class StakingStateRepository:
    """Stores staking agent state via Panorama's gateway or an in-memory fallback."""

    _instance: "StakingStateRepository" | None = None
    _instance_lock: Lock = Lock()

    def __init__(
        self,
        *,
        client: PanoramaGatewayClient | None = None,
        settings: PanoramaGatewaySettings | None = None,
        history_limit: int = 10,
    ) -> None:
        self._logger = logging.getLogger(__name__)
        self._history_limit = history_limit
        try:
            self._settings = settings or get_panorama_settings()
            self._client = client or PanoramaGatewayClient(self._settings)
            self._use_gateway = True
        except ValueError:
            # PANORAMA_GATEWAY_URL or JWT secrets not configured - fall back to local store.
            self._settings = None
            self._client = None
            self._use_gateway = False
        self._init_local_store()

    def _init_local_store(self) -> None:
        if not hasattr(self, "_state"):
            self._state = {"intents": {}, "metadata": {}, "history": {}}

    def _tenant_id(self) -> str:
        return self._settings.tenant_id if self._settings else "tenant-agent"

    def _fallback_to_local_store(self) -> None:
        if self._use_gateway:
            self._logger.warning("Panorama gateway unavailable for staking state; switching to in-memory fallback.")
        self._use_gateway = False
        self._init_local_store()

    def _handle_gateway_failure(self, exc: PanoramaGatewayError) -> None:
        self._logger.warning(
            "Panorama gateway error (%s) for staking repository: %s",
            getattr(exc, "status_code", "unknown"),
            getattr(exc, "payload", exc),
        )
        self._fallback_to_local_store()

    # ---- Singleton helpers -----------------------------------------------
    @classmethod
    def instance(cls) -> "StakingStateRepository":
        if cls._instance is None:
            with cls._instance_lock:
                if cls._instance is None:
                    cls._instance = cls()
        return cls._instance

    @classmethod
    def reset(cls) -> None:
        with cls._instance_lock:
            cls._instance = None

    # ---- Core API ---------------------------------------------------------
    def load_intent(self, user_id: str, conversation_id: str) -> Optional[Dict[str, Any]]:
        if not self._use_gateway:
            self._init_local_store()
            record = self._state["intents"].get(_identifier(user_id, conversation_id))
            if not record:
                return None
            return copy.deepcopy(record.get("intent"))

        session = self._get_session(user_id, conversation_id)
        if not self._use_gateway:
            return self.load_intent(user_id, conversation_id)
        if not session:
            return None
        return session.get("intent") or None

    def persist_intent(
        self,
        user_id: str,
        conversation_id: str,
        intent: Dict[str, Any],
        metadata: Dict[str, Any],
        done: bool,
        summary: Optional[Dict[str, Any]] = None,
    ) -> List[Dict[str, Any]]:
        if not self._use_gateway:
            self._init_local_store()
            key = _identifier(user_id, conversation_id)
            now = time.time()
            if done:
                self._state["intents"].pop(key, None)
            else:
                self._state["intents"][key] = {"intent": copy.deepcopy(intent), "updated_at": now}
            if metadata:
                meta_copy = copy.deepcopy(metadata)
                meta_copy["updated_at"] = now
                self._state["metadata"][key] = meta_copy
            if done and summary:
                history = self._state["history"].setdefault(key, [])
                summary_copy = copy.deepcopy(summary)
                summary_copy.setdefault("timestamp", now)
                history.append(summary_copy)
                self._state["history"][key] = history[-self._history_limit:]
            return self.get_history(user_id, conversation_id)

        try:
            if done:
                if summary:
                    self._create_history_entry(user_id, conversation_id, summary)
                self._delete_session(user_id, conversation_id)
            else:
                payload = self._session_payload(intent, metadata)
                self._upsert_session(user_id, conversation_id, payload)
            return self.get_history(user_id, conversation_id)
        except PanoramaGatewayError as exc:
            self._handle_gateway_failure(exc)
            return self.persist_intent(user_id, conversation_id, intent, metadata, done, summary)

    def set_metadata(
        self,
        user_id: str,
        conversation_id: str,
        metadata: Dict[str, Any],
    ) -> None:
        if not self._use_gateway:
            self._init_local_store()
            key = _identifier(user_id, conversation_id)
            if metadata:
                meta_copy = copy.deepcopy(metadata)
                meta_copy["updated_at"] = time.time()
                self._state["metadata"][key] = meta_copy
            else:
                self._state["metadata"].pop(key, None)
            return

        try:
            if not metadata:
                self._delete_session(user_id, conversation_id)
                return

            session = self._get_session(user_id, conversation_id)
            if not self._use_gateway:
                return self.set_metadata(user_id, conversation_id, metadata)
            intent = session.get("intent") if session else {}
            payload = self._session_payload(intent or {}, metadata)
            self._upsert_session(user_id, conversation_id, payload)
        except PanoramaGatewayError as exc:
            self._handle_gateway_failure(exc)
            self.set_metadata(user_id, conversation_id, metadata)

    def clear_metadata(self, user_id: str, conversation_id: str) -> None:
        self.set_metadata(user_id, conversation_id, {})

    def clear_intent(self, user_id: str, conversation_id: str) -> None:
        if not self._use_gateway:
            self._init_local_store()
            self._state["intents"].pop(_identifier(user_id, conversation_id), None)
            self._state["metadata"].pop(_identifier(user_id, conversation_id), None)
            return
        try:
            self._delete_session(user_id, conversation_id)
        except PanoramaGatewayError as exc:
            self._handle_gateway_failure(exc)
            self.clear_intent(user_id, conversation_id)

    def get_metadata(self, user_id: str, conversation_id: str) -> Dict[str, Any]:
        if not self._use_gateway:
            self._init_local_store()
            record = self._state["metadata"].get(_identifier(user_id, conversation_id))
            if not record:
                return {}
            entry = copy.deepcopy(record)
            ts = entry.pop("updated_at", None)
            if ts is not None:
                entry["updated_at"] = datetime.fromtimestamp(float(ts), tz=timezone.utc).isoformat()
            return entry

        session = self._get_session(user_id, conversation_id)
        if not self._use_gateway:
            return self.get_metadata(user_id, conversation_id)
        if not session:
            return {}

        intent = session.get("intent") or {}
        metadata: Dict[str, Any] = {
            "event": session.get("event"),
            "status": session.get("status"),
            "missing_fields": session.get("missingFields") or [],
            "next_field": session.get("nextField"),
            "pending_question": session.get("pendingQuestion"),
            "choices": session.get("choices") or [],
            "error": session.get("errorMessage"),
            "user_id": user_id,
            "conversation_id": conversation_id,
        }
        metadata["action"] = intent.get("action")
        metadata["amount"] = intent.get("amount")
        metadata["network"] = intent.get("network")
        metadata["protocol"] = intent.get("protocol")
        metadata["input_token"] = intent.get("input_token")
        metadata["output_token"] = intent.get("output_token")

        history = self.get_history(user_id, conversation_id)
        if history:
            metadata["history"] = history

        updated_at = session.get("updatedAt")
        if updated_at:
            metadata["updated_at"] = updated_at

        return metadata

    def get_history(
        self,
        user_id: str,
        conversation_id: str,
        limit: Optional[int] = None,
    ) -> List[Dict[str, Any]]:
        if not self._use_gateway:
            key = _identifier(user_id, conversation_id)
            history = self._state["history"].get(key, [])
            effective = limit or self._history_limit
            result: List[Dict[str, Any]] = []
            for item in sorted(history, key=lambda entry: entry.get("timestamp", 0), reverse=True)[:effective]:
                entry = copy.deepcopy(item)
                ts = entry.get("timestamp")
                if ts is not None:
                    entry["timestamp"] = datetime.fromtimestamp(float(ts), tz=timezone.utc).isoformat()
                result.append(entry)
            return result

        effective_limit = limit or self._history_limit
        try:
            result = self._client.list(
                STAKING_HISTORY_ENTITY,
                {
                    "where": {"userId": user_id, "conversationId": conversation_id},
                    "orderBy": {"recordedAt": "desc"},
                    "take": effective_limit,
                },
            )
        except PanoramaGatewayError as exc:
            if exc.status_code == 404:
                return []
            self._handle_gateway_failure(exc)
            return self.get_history(user_id, conversation_id, limit)
        except ValueError:
            self._logger.warning("Invalid staking history response from gateway; falling back to local store.")
            self._fallback_to_local_store()
            return self.get_history(user_id, conversation_id, limit)
        data = result.get("data", []) if isinstance(result, dict) else []
        history: List[Dict[str, Any]] = []
        for entry in data:
            history.append(
                {
                    "status": entry.get("status"),
                    "action": entry.get("action"),
                    "amount": entry.get("amount"),
                    "network": entry.get("network"),
                    "protocol": entry.get("protocol"),
                    "input_token": entry.get("inputToken"),
                    "output_token": entry.get("outputToken"),
                    "error": entry.get("errorMessage"),
                    "timestamp": entry.get("recordedAt"),
                }
            )
        return history

    # ---- Gateway helpers --------------------------------------------------
    def _get_session(self, user_id: str, conversation_id: str) -> Optional[Dict[str, Any]]:
        identifier = _identifier(user_id, conversation_id)
        try:
            return self._client.get(STAKING_SESSION_ENTITY, identifier)
        except PanoramaGatewayError as exc:
            if exc.status_code == 404:
                return None
            self._handle_gateway_failure(exc)
            return None

    def _delete_session(self, user_id: str, conversation_id: str) -> None:
        identifier = _identifier(user_id, conversation_id)
        try:
            self._client.delete(STAKING_SESSION_ENTITY, identifier)
        except PanoramaGatewayError as exc:
            if exc.status_code != 404:
                self._handle_gateway_failure(exc)
                raise

    def _upsert_session(
        self,
        user_id: str,
        conversation_id: str,
        data: Dict[str, Any],
    ) -> None:
        identifier = _identifier(user_id, conversation_id)
        payload = {**data, "updatedAt": _utc_now_iso()}
        try:
            self._client.update(STAKING_SESSION_ENTITY, identifier, payload)
        except PanoramaGatewayError as exc:
            if exc.status_code != 404:
                self._handle_gateway_failure(exc)
                raise
            create_payload = {
                "userId": user_id,
                "conversationId": conversation_id,
                "tenantId": self._tenant_id(),
                **payload,
            }
            try:
                self._client.create(STAKING_SESSION_ENTITY, create_payload)
            except PanoramaGatewayError as create_exc:
                if create_exc.status_code == 409:
                    return
                if create_exc.status_code == 404:
                    self._handle_gateway_failure(create_exc)
                    raise
                self._handle_gateway_failure(create_exc)
                raise

    def _create_history_entry(
        self,
        user_id: str,
        conversation_id: str,
        summary: Dict[str, Any],
    ) -> None:
        history_payload = {
            "userId": user_id,
            "conversationId": conversation_id,
            "status": summary.get("status"),
            "action": summary.get("action"),
            "amount": _as_float(summary.get("amount")),
            "network": summary.get("network"),
            "protocol": summary.get("protocol"),
            "inputToken": summary.get("input_token"),
            "outputToken": summary.get("output_token"),
            "errorMessage": summary.get("error"),
            "recordedAt": _utc_now_iso(),
            "tenantId": self._tenant_id(),
        }
        if self._logger.isEnabledFor(logging.DEBUG):
            self._logger.debug(
                "Persisting staking history for user=%s conversation=%s payload=%s",
                user_id,
                conversation_id,
                history_payload,
            )
        try:
            self._client.create(STAKING_HISTORY_ENTITY, history_payload)
        except PanoramaGatewayError as exc:
            if exc.status_code == 404:
                self._handle_gateway_failure(exc)
                raise
            elif exc.status_code != 409:
                self._handle_gateway_failure(exc)
                raise

    @staticmethod
    def _session_payload(intent: Dict[str, Any], metadata: Dict[str, Any]) -> Dict[str, Any]:
        missing = metadata.get("missing_fields") or []
        if not isinstance(missing, list):
            missing = list(missing)
        return {
            "status": metadata.get("status"),
            "event": metadata.get("event"),
            "intent": intent,
            "missingFields": missing,
            "nextField": metadata.get("next_field"),
            "pendingQuestion": metadata.get("pending_question"),
            "choices": metadata.get("choices"),
            "errorMessage": metadata.get("error"),
            "historyCursor": metadata.get("history_cursor") or 0,
        }