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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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
"""OpenAI tool-calling loop with mandatory policy gating before MCP I/O."""

from __future__ import annotations

import json
import os
import re
import time
import uuid
from dataclasses import dataclass
from typing import Any

from openai import APIError, AsyncOpenAI

from src.logger import log_event, summarize_tool_args
from src.mcp_client import MCPAuthError, MCPNotFoundError, MCPTimeoutError, MCPTransportError, MeridianMCPClient
from src.policy import Allow, Deny, RequireConfirmation, authorize, inject_sensitive_tool_args, sync_pending_order_snapshot
from src.prompts import build_system_prompt
from src.schemas import LLM_TOOL_NAMES, TOOL_INPUT_MODELS
from src.session import Message, Session

_FALLBACK_MODEL = "gpt-4o-mini"
_MAX_TOOL_ITERATIONS = 5

MCP_TOOL_WHITELIST: frozenset[str] = frozenset(TOOL_INPUT_MODELS.keys())

_CONFIRM_RE = re.compile(
    r"^(yes|yep|yeah|confirm|confirmed|place\s+(?:the\s+)?order|go\s+ahead|"
    r"do\s+it|ok(?:ay)?|sounds\s+good|please\s+do)\b",
    re.I | re.S,
)
_REJECT_RE = re.compile(r"^(no|nope|nah|cancel|stop|wait|not\s+now)\b", re.I | re.S)

_SKU_RE = re.compile(r"\b([A-Z]{2,12}-\d{2,8})\b")


@dataclass
class ToolCallRecord:
    tool_name: str
    args_summary: str
    decision: str | None = None
    latency_ms: int | None = None
    status: str | None = None


@dataclass
class TurnResult:
    assistant_text: str
    tool_calls_made: list[ToolCallRecord]
    confirmation_requested: bool
    error: str | None = None


def apply_user_confirmation_intent(session: Session, user_message: str) -> None:
    """Deterministic confirmation handling — never delegate this to the LLM."""

    text = user_message.strip()
    if not text:
        return
    pending = session.pending_confirmation
    if not pending:
        return
    if _REJECT_RE.match(text):
        session.pending_confirmation = None
        return
    if _CONFIRM_RE.match(text):
        pending.confirmed = True


def _extract_price(text: str) -> str | None:
    m = re.search(r"\$?\s*(\d+(?:\.\d{1,2})?)", text)
    return m.group(1) if m else None


def update_facts_from_mcp_text(tool_name: str, text: str, session: Session) -> None:
    """Best-effort fact extraction — MCP returns prose, not JSON."""

    if tool_name in {"search_products", "list_products"}:
        results: list[dict[str, str | None]] = []
        for line in text.splitlines():
            sku_m = _SKU_RE.search(line)
            if not sku_m:
                continue
            results.append(
                {
                    "sku": sku_m.group(1),
                    "name": line.strip()[:160],
                    "price": _extract_price(line),
                }
            )
            if len(results) >= 5:
                break
        if results:
            session.facts["last_search_results"] = results
        return

    if tool_name == "get_product":
        sku_m = _SKU_RE.search(text)
        top = text.splitlines()[0][:220] if text else ""
        session.facts["last_viewed_product"] = {
            "sku": sku_m.group(1) if sku_m else None,
            "name": top,
            "price": _extract_price(text),
        }
        return

    if tool_name == "list_orders":
        orders: list[dict[str, str | None]] = []
        uuid_re = re.compile(
            r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
            re.I,
        )
        for line in text.splitlines():
            oid_m = uuid_re.search(line)
            if not oid_m:
                continue
            status: str | None = None
            for st in ("draft", "submitted", "approved", "fulfilled", "cancelled"):
                if st in line.lower():
                    status = st
                    break
            orders.append(
                {
                    "order_id": oid_m.group(0),
                    "status": status,
                    "total": _extract_price(line),
                }
            )
            if len(orders) >= 5:
                break
        if orders:
            session.facts["last_orders"] = orders
        return

    if tool_name == "create_order":
        session.pending_confirmation = None
        uuid_re = re.compile(
            r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
            re.I,
        )
        oid_m = uuid_re.search(text)
        entry = {"order_id": oid_m.group(0) if oid_m else None, "snippet": text[:400]}
        recent = session.facts.get("recent_orders")
        buf: list[Any] = recent if isinstance(recent, list) else []
        session.facts["recent_orders"] = [entry] + buf[:4]


def _mcp_tool_to_openai(tool: Any) -> dict[str, Any]:
    return {
        "type": "function",
        "function": {
            "name": tool.name,
            "description": (tool.description or "")[:4096],
            "parameters": tool.inputSchema,
        },
    }


def _history_to_messages(session: Session) -> list[dict[str, Any]]:
    """Prior turns only — the active user utterance is appended by handle_turn."""

    msgs: list[dict[str, Any]] = []
    hist = list(session.history)
    if len(hist) < 2:
        return msgs
    for m in hist[:-1]:
        if m.role not in {"user", "assistant"}:
            continue
        msgs.append({"role": m.role, "content": m.content})
    return msgs


class ChatEngine:
    def __init__(
        self,
        mcp_client: MeridianMCPClient,
        session: Session,
        openai_client: AsyncOpenAI,
        *,
        session_id: str | None = None,
        model: str | None = None,
    ) -> None:
        self._mcp = mcp_client
        self._session = session
        self._openai = openai_client
        self._session_id = session_id or str(uuid.uuid4())
        self._model = model or os.environ.get("OPENAI_MODEL", "").strip() or _FALLBACK_MODEL
        self._tools_cache: list[dict[str, Any]] | None = None

    @property
    def session_id(self) -> str:
        return self._session_id

    async def _openai_tools(self) -> list[dict[str, Any]]:
        if self._tools_cache is None:
            remote = await self._mcp.list_tools()
            self._tools_cache = [_mcp_tool_to_openai(t) for t in remote if t.name in LLM_TOOL_NAMES]
        return self._tools_cache

    async def handle_turn(self, user_message: str) -> TurnResult:
        apply_user_confirmation_intent(self._session, user_message)

        self._session.add_message(Message(role="user", content=user_message))

        hist = list(self._session.history)
        if not hist or hist[-1].role != "user":
            return TurnResult(
                "I'm missing internal chat state. Please restart the session.",
                [],
                False,
                error="history_invariant_broken",
            )
        prior_user_text = hist[-1].content
        api_messages: list[dict[str, Any]] = _history_to_messages(self._session) + [
            {"role": "user", "content": prior_user_text}
        ]

        tool_records: list[ToolCallRecord] = []
        confirmation_requested = False
        tools = await self._openai_tools()

        for iteration in range(_MAX_TOOL_ITERATIONS):
            system_prompt = build_system_prompt(self._session)
            t_llm = time.perf_counter()
            try:
                completion = await self._openai.chat.completions.create(
                    model=self._model,
                    max_tokens=1024,
                    messages=[{"role": "system", "content": system_prompt}, *api_messages],
                    tools=tools,
                    tool_choice="auto",
                )
            except APIError as e:
                log_event(
                    "error",
                    session_id=self._session_id,
                    tool=None,
                    decision=None,
                    deny_reason=str(e),
                    latency_ms=int((time.perf_counter() - t_llm) * 1000),
                    status="error",
                    args_summary=None,
                )
                msg = "I'm experiencing a technical issue. Please try again."
                self._session.add_message(Message(role="assistant", content=msg))
                return TurnResult(msg, tool_records, confirmation_requested, error=str(e))

            choice = completion.choices[0]
            finish = choice.finish_reason
            msg = choice.message

            log_event(
                "llm_call",
                session_id=self._session_id,
                tool=None,
                decision=None,
                deny_reason=None,
                latency_ms=int((time.perf_counter() - t_llm) * 1000),
                status="success",
                args_summary=f"finish_reason={finish};iteration={iteration + 1}",
            )

            tool_calls = msg.tool_calls or []
            if finish != "tool_calls" or not tool_calls:
                text = (msg.content or "").strip() or (
                    "I'm not sure how to help with that — could you rephrase?"
                )
                self._session.add_message(Message(role="assistant", content=text))
                return TurnResult(text, tool_records, confirmation_requested, None)

            assistant_msg: dict[str, Any] = {
                "role": "assistant",
                "content": msg.content or "",
                "tool_calls": [
                    {
                        "id": tc.id,
                        "type": "function",
                        "function": {
                            "name": tc.function.name,
                            "arguments": tc.function.arguments or "{}",
                        },
                    }
                    for tc in tool_calls
                ],
            }
            api_messages.append(assistant_msg)

            for tc in tool_calls:
                tool_name = tc.function.name
                tool_id = tc.id
                raw_args = tc.function.arguments or "{}"
                try:
                    parsed = json.loads(raw_args) if raw_args.strip() else {}
                except json.JSONDecodeError:
                    parsed = {}
                if not isinstance(parsed, dict):
                    summary = summarize_tool_args(tool_name, {})
                    tool_records.append(
                        ToolCallRecord(
                            tool_name,
                            summary,
                            decision=None,
                            latency_ms=None,
                            status="bad_args",
                        )
                    )
                    log_event(
                        "error",
                        session_id=self._session_id,
                        tool=tool_name,
                        decision=None,
                        deny_reason="invalid_tool_arguments_json",
                        latency_ms=None,
                        status="error",
                        args_summary=summary,
                    )
                    api_messages.append(
                        {
                            "role": "tool",
                            "tool_call_id": tool_id,
                            "content": "Tool arguments must be a JSON object; please fix and retry.",
                        }
                    )
                    continue

                args = dict(parsed)
                summary = summarize_tool_args(tool_name, args)
                t0 = time.perf_counter()

                if tool_name not in MCP_TOOL_WHITELIST:
                    lat = int((time.perf_counter() - t0) * 1000)
                    tool_records.append(
                        ToolCallRecord(tool_name, summary, decision="deny", latency_ms=lat, status="blocked")
                    )
                    log_event(
                        "policy_decision",
                        session_id=self._session_id,
                        tool=tool_name,
                        decision="deny",
                        deny_reason="Unknown tool",
                        latency_ms=lat,
                        status=None,
                        args_summary=summary,
                    )
                    api_messages.append(
                        {
                            "role": "tool",
                            "tool_call_id": tool_id,
                            "content": "Unknown tool — pick from the registered Meridian MCP tools only.",
                        }
                    )
                    continue

                decision = authorize(tool_name, args, self._session)
                if isinstance(decision, Deny):
                    lat = int((time.perf_counter() - t0) * 1000)
                    tool_records.append(
                        ToolCallRecord(
                            tool_name,
                            summary,
                            decision="deny",
                            latency_ms=lat,
                            status="denied",
                        )
                    )
                    log_event(
                        "policy_decision",
                        session_id=self._session_id,
                        tool=tool_name,
                        decision="deny",
                        deny_reason=decision.reason,
                        latency_ms=lat,
                        status=None,
                        args_summary=summary,
                    )
                    api_messages.append(
                        {"role": "tool", "tool_call_id": tool_id, "content": decision.reason}
                    )
                    continue

                if isinstance(decision, RequireConfirmation):
                    lat = int((time.perf_counter() - t0) * 1000)
                    confirmation_requested = True
                    tool_records.append(
                        ToolCallRecord(
                            tool_name,
                            summary,
                            decision="require_confirmation",
                            latency_ms=lat,
                            status="pending",
                        )
                    )
                    log_event(
                        "policy_decision",
                        session_id=self._session_id,
                        tool=tool_name,
                        decision="require_confirmation",
                        deny_reason=None,
                        latency_ms=lat,
                        status=None,
                        args_summary=summary,
                    )
                    if tool_name == "create_order" and self._session.pending_confirmation is None:
                        canonical = inject_sensitive_tool_args(
                            tool_name,
                            args,
                            self._session,
                        )
                        sync_pending_order_snapshot(self._session, canonical)
                    api_messages.append(
                        {"role": "tool", "tool_call_id": tool_id, "content": decision.prompt}
                    )
                    continue

                merged = inject_sensitive_tool_args(tool_name, args, self._session)
                log_event(
                    "policy_decision",
                    session_id=self._session_id,
                    tool=tool_name,
                    decision="allow",
                    deny_reason=None,
                    latency_ms=int((time.perf_counter() - t0) * 1000),
                    status=None,
                    args_summary=summary,
                )

                mcp_t0 = time.perf_counter()
                try:
                    payload = await self._mcp.call_tool(tool_name, merged)
                    mcp_lat = int((time.perf_counter() - mcp_t0) * 1000)
                    update_facts_from_mcp_text(tool_name, payload, self._session)
                    tool_records.append(
                        ToolCallRecord(
                            tool_name,
                            summarize_tool_args(tool_name, merged),
                            decision="allow",
                            latency_ms=mcp_lat,
                            status="success",
                        )
                    )
                    log_event(
                        "tool_call",
                        session_id=self._session_id,
                        tool=tool_name,
                        decision="allow",
                        deny_reason=None,
                        latency_ms=mcp_lat,
                        status="success",
                        args_summary=summarize_tool_args(tool_name, merged),
                    )
                    api_messages.append(
                        {"role": "tool", "tool_call_id": tool_id, "content": payload}
                    )
                except (MCPTransportError, MCPAuthError, MCPNotFoundError, MCPTimeoutError) as e:
                    mcp_lat = int((time.perf_counter() - mcp_t0) * 1000)
                    tool_records.append(
                        ToolCallRecord(
                            tool_name,
                            summarize_tool_args(tool_name, merged),
                            decision="allow",
                            latency_ms=mcp_lat,
                            status="error",
                        )
                    )
                    log_event(
                        "error",
                        session_id=self._session_id,
                        tool=tool_name,
                        decision="allow",
                        deny_reason=str(e),
                        latency_ms=mcp_lat,
                        status="error",
                        args_summary=summarize_tool_args(tool_name, merged),
                    )
                    api_messages.append(
                        {
                            "role": "tool",
                            "tool_call_id": tool_id,
                            "content": "I couldn't reach that information right now. Please try again shortly.",
                        }
                    )

        fallback = "I'm having trouble completing that request. Could you rephrase?"
        self._session.add_message(Message(role="assistant", content=fallback))
        log_event(
            "error",
            session_id=self._session_id,
            tool=None,
            decision=None,
            deny_reason="tool_iteration_cap",
            latency_ms=None,
            status="error",
            args_summary=None,
        )
        return TurnResult(fallback, tool_records, confirmation_requested, error="tool_iteration_cap")