File size: 42,311 Bytes
e36160c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
"""
Multi-Agent Autonomous Procurement System — FastAPI + Auth
Hugging Face Spaces Docker entry-point  ·  uvicorn app:app --host 0.0.0.0 --port 7860

Auth:   POST /auth/login  →  Bearer JWT (HS256, 2-hour expiry)
UI:     GET  /            →  full SPA (login + dashboard, no framework)
API:    all /workflow/* routes require Authorization: Bearer <token>
"""

import json
import time
import hmac
import hashlib
import base64
import traceback
from contextlib import asynccontextmanager
from typing import Any

from fastapi import FastAPI, HTTPException, Depends, Request, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from pydantic import BaseModel, Field

from procurement_system import build_procurement_graph, create_llm, ProcurementWorkflowExecutor

# ── Auth config ────────────────────────────────────────────────────────────────
_JWT_SECRET  = "procurement-demo-secret-2024"
_JWT_EXPIRY  = 7200  # 2 hours

# Dummy user store  { username: { password, role, display_name } }
USERS = {
    "admin":    {"password": "admin123",    "role": "admin",    "name": "Admin User"},
    "buyer":    {"password": "buyer123",    "role": "buyer",    "name": "Sarah (Buyer)"},
    "manager":  {"password": "manager123",  "role": "manager",  "name": "John (Manager)"},
    "analyst":  {"password": "analyst123",  "role": "analyst",  "name": "Priya (Analyst)"},
}

# ── Preset test scenarios exposed to the UI ────────────────────────────────────
TEST_CASES = [
    {
        "id": "tc1",
        "label": "✅ Cloud – within budget",
        "request": "We need enterprise cloud infrastructure with auto-scaling and 99.99% uptime SLA for our data pipeline.",
        "budget": 5500,
        "expected": "Analysis APPROVED → awaiting human approval",
    },
    {
        "id": "tc2",
        "label": "❌ Cloud – budget too low",
        "request": "We need enterprise cloud infrastructure with auto-scaling and high availability SLA.",
        "budget": 2000,
        "expected": "Analysis REJECTED – all vendors exceed budget",
    },
    {
        "id": "tc3",
        "label": "✅ Software – within budget",
        "request": "We need enterprise software licensing for 1500 users with cloud deployment.",
        "budget": 3000,
        "expected": "Analysis APPROVED → awaiting human approval",
    },
    {
        "id": "tc4",
        "label": "❌ Software – budget too low",
        "request": "We need software licensing with custom integrations and quarterly updates.",
        "budget": 1500,
        "expected": "Analysis REJECTED – all vendors exceed budget",
    },
    {
        "id": "tc5",
        "label": "✅ Cloud – tight budget (cheapest vendor)",
        "request": "We need basic cloud infrastructure with auto-scaling for our startup workloads.",
        "budget": 3500,
        "expected": "Analysis APPROVED (cheapest vendor selected)",
    },
]

# ── Graph singleton ────────────────────────────────────────────────────────────
_graph  = None
_memory = None

@asynccontextmanager
async def lifespan(app: FastAPI):
    global _graph, _memory
    llm = create_llm()
    _graph, _memory, _ = build_procurement_graph(llm)
    print("Procurement graph ready.")
    yield

app = FastAPI(title="Procurement System", version="1.0.0", lifespan=lifespan)
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])

@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
    tb = traceback.format_exc()
    print(f"Unhandled exception on {request.url}:\n{tb}")
    return JSONResponse(
        status_code=500,
        content={"detail": str(exc), "type": type(exc).__name__},
    )

_sessions: dict[str, ProcurementWorkflowExecutor] = {}
_bearer   = HTTPBearer()

# ── Minimal JWT (no external lib) ─────────────────────────────────────────────

def _b64(data: bytes) -> str:
    return base64.urlsafe_b64encode(data).rstrip(b"=").decode()

def _sign(msg: str) -> str:
    return _b64(hmac.new(_JWT_SECRET.encode(), msg.encode(), hashlib.sha256).digest())

def create_token(username: str, role: str) -> str:
    header  = _b64(json.dumps({"alg": "HS256", "typ": "JWT"}).encode())
    payload = _b64(json.dumps({"sub": username, "role": role, "exp": int(time.time()) + _JWT_EXPIRY}).encode())
    return f"{header}.{payload}.{_sign(header + '.' + payload)}"

def verify_token(token: str) -> dict:
    try:
        h, p, sig = token.split(".")
        if _sign(h + "." + p) != sig:
            raise ValueError("bad sig")
        claims = json.loads(base64.urlsafe_b64decode(p + "=="))
        if claims["exp"] < int(time.time()):
            raise ValueError("expired")
        return claims
    except Exception:
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or expired token")

def current_user(creds: HTTPAuthorizationCredentials = Depends(_bearer)) -> dict:
    return verify_token(creds.credentials)

# ── Helpers ────────────────────────────────────────────────────────────────────

def _ser(state: Any) -> dict:
    if state is None:
        return {}
    return dict(state)

def _fmt_events(events: list) -> list[dict]:
    out = []
    for e in (events or []):
        try:
            if not isinstance(e, dict) or not e:
                continue
            node    = next(iter(e))
            updates = e[node]
            if not isinstance(updates, dict):
                out.append({"node": node, "updates": {}})
                continue
            safe = {}
            for k, v in updates.items():
                if isinstance(v, str):
                    safe[k] = v[:400]
                elif isinstance(v, (int, float, bool, type(None))):
                    safe[k] = v
                elif isinstance(v, list):
                    safe[k] = [str(x)[:200] if not isinstance(x, (int, float, bool, dict)) else x for x in v[:5]]
                else:
                    safe[k] = str(v)[:200]
            out.append({"node": node, "updates": safe})
        except Exception:
            pass
    return out

# ── Auth routes ────────────────────────────────────────────────────────────────

class LoginRequest(BaseModel):
    username: str
    password: str

@app.post("/auth/login", tags=["auth"])
def login(body: LoginRequest):
    user = USERS.get(body.username)
    if not user or user["password"] != body.password:
        raise HTTPException(status_code=401, detail="Invalid credentials")
    token = create_token(body.username, user["role"])
    return {"access_token": token, "token_type": "bearer",
            "username": body.username, "name": user["name"], "role": user["role"]}

@app.get("/auth/me", tags=["auth"])
def me(user: dict = Depends(current_user)):
    u = USERS[user["sub"]]
    return {"username": user["sub"], "name": u["name"], "role": user["role"]}

# ── Workflow routes ────────────────────────────────────────────────────────────

class ParseRequest(BaseModel):
    raw_input: str = Field(..., example="We need servers for our startup, budget around 4k a month")

class StartRequest(BaseModel):
    procurement_request: str   = Field(default="", example="We need enterprise cloud infrastructure")
    budget_limit: float        = Field(default=5000.0, gt=0, example=5500.0)
    raw_input: str             = Field(default="", example="")

class DecideRequest(BaseModel):
    approve: bool = Field(..., example=True)

@app.get("/health")
def health():
    return {"status": "ok", "graph_ready": _graph is not None}

@app.get("/testcases", tags=["workflow"])
def get_test_cases():
    return TEST_CASES

@app.post("/workflow/parse", tags=["workflow"])
def parse_prose(body: ParseRequest, user: dict = Depends(current_user)):
    """Use the LLM to extract structured fields from free-form prose. Preview before running agents."""
    try:
        from langchain_core.prompts import PromptTemplate
        llm = create_llm()
        prompt = PromptTemplate(
            input_variables=["raw"],
            template=(
                "You are a procurement intake assistant. Extract structured procurement details "
                "from the user's free-form description below.\n\n"
                "User input:\n{raw}\n\n"
                "Reply with ONLY valid JSON — no markdown, no explanation:\n"
                '{{\n'
                '  "procurement_request": "<concise 1-2 sentence professional description of what is needed>",\n'
                '  "budget_limit": <monthly budget as a number, default 5000 if not mentioned>,\n'
                '  "confidence": "<high|medium|low>",\n'
                '  "assumptions": "<brief note on any assumptions made>"\n'
                '}}'
            ),
        )
        response = llm.invoke(prompt.format(raw=body.raw_input))
        content  = response.content.strip()
        if content.startswith("```"):
            content = content.split("```")[1]
            if content.startswith("json"):
                content = content[4:]
            content = content.strip()
        parsed = json.loads(content)
        return {
            "raw_input":           body.raw_input,
            "procurement_request": parsed.get("procurement_request", body.raw_input),
            "budget_limit":        float(parsed.get("budget_limit", 5000)),
            "confidence":          parsed.get("confidence", "medium"),
            "assumptions":         parsed.get("assumptions", ""),
        }
    except json.JSONDecodeError:
        return {
            "raw_input":           body.raw_input,
            "procurement_request": body.raw_input,
            "budget_limit":        5000.0,
            "confidence":          "low",
            "assumptions":         "Could not parse LLM response; using raw input verbatim.",
        }
    except Exception as exc:
        tb = traceback.format_exc()
        print(f"parse_prose error:\n{tb}")
        raise HTTPException(status_code=500, detail=f"{type(exc).__name__}: {exc}")

@app.post("/workflow/start", tags=["workflow"])
def start_workflow(body: StartRequest, user: dict = Depends(current_user)):
    try:
        executor = ProcurementWorkflowExecutor(_graph, _memory)
        _sessions[executor.thread_id] = executor
        state, events = executor.start_workflow(
            body.procurement_request,
            body.budget_limit,
            raw_input=body.raw_input,
        )
        s = _ser(state)
        return {
            "thread_id":         executor.thread_id,
            "status":            "awaiting_approval" if s.get("analysis_approved") else "rejected",
            "analysis_approved": s.get("analysis_approved"),
            "selected_vendor":   s.get("selected_vendor"),
            "vendor_options":         s.get("vendor_options"),
            "budget_limit":           s.get("budget_limit"),
            "procurement_request":    s.get("procurement_request"),
            "logs":                   s.get("logs"),
            "events":                 _fmt_events(events),
            "triggered_by":           user["sub"],
        }
    except Exception as exc:
        tb = traceback.format_exc()
        print(f"start_workflow error:\n{tb}")
        raise HTTPException(status_code=500, detail=f"{type(exc).__name__}: {exc}")

@app.get("/workflow/{thread_id}", tags=["workflow"])
def get_workflow(thread_id: str, user: dict = Depends(current_user)):
    ex = _sessions.get(thread_id)
    if not ex:
        raise HTTPException(404, f"Thread '{thread_id}' not found")
    return {"thread_id": thread_id, "state": _ser(ex.get_state())}

@app.post("/workflow/{thread_id}/decide", tags=["workflow"])
def decide(thread_id: str, body: DecideRequest, user: dict = Depends(current_user)):
    ex = _sessions.get(thread_id)
    if not ex:
        raise HTTPException(404, f"Thread '{thread_id}' not found")
    try:
        state, events = ex.approve_vendor(body.approve)
        s = _ser(state)
        outcome = ("contract_generated" if s.get("contract_draft") else "approved_pending_contract") if body.approve else "rejected_by_human"
        return {
            "thread_id":       thread_id,
            "outcome":         outcome,
            "human_approved":  s.get("human_approved"),
            "contract_draft":  s.get("contract_draft"),
            "selected_vendor": s.get("selected_vendor"),
            "logs":            s.get("logs"),
            "events":          _fmt_events(events),
            "decided_by":      user["sub"],
        }
    except Exception as exc:
        tb = traceback.format_exc()
        print(f"decide error:\n{tb}")
        raise HTTPException(status_code=500, detail=f"{type(exc).__name__}: {exc}")

# ── SPA ────────────────────────────────────────────────────────────────────────

@app.get("/", response_class=HTMLResponse, include_in_schema=False)
def root():
    return HTMLResponse(content=r"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/><meta name="viewport" content="width=device-width,initial-scale=1"/>
<title>Procurement AI System</title>
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{font-family:'Segoe UI',system-ui,sans-serif;background:#0f172a;color:#e2e8f0;min-height:100vh}
/* ─── LOGIN ─── */
#login-page{display:flex;align-items:center;justify-content:center;min-height:100vh;padding:1rem}
.login-box{background:#1e293b;border:1px solid #334155;border-radius:1rem;padding:2.5rem;width:100%;max-width:420px}
.login-box h1{font-size:1.4rem;font-weight:700;color:#93c5fd;margin-bottom:.4rem}
.login-box p{color:#64748b;font-size:.85rem;margin-bottom:1.5rem}
.users-hint{background:#0f172a;border-radius:.5rem;padding:.75rem;margin-bottom:1.5rem;font-size:.78rem;color:#94a3b8;line-height:1.8}
.users-hint strong{color:#e2e8f0}
input[type=text],input[type=password],input[type=number]{width:100%;background:#0f172a;border:1px solid #334155;color:#e2e8f0;border-radius:.5rem;padding:.65rem .9rem;font-size:.9rem;margin-bottom:.75rem;outline:none}
input:focus{border-color:#3b82f6}
.btn{padding:.7rem 1.2rem;border:none;border-radius:.5rem;font-size:.9rem;font-weight:600;cursor:pointer;transition:.15s;display:inline-block}
.btn-full{width:100%}
.btn-primary{background:#2563eb;color:#fff}.btn-primary:hover{background:#1d4ed8}
.btn-secondary{background:#1e293b;color:#94a3b8;border:1px solid #334155}.btn-secondary:hover{background:#273548}
.btn-success{background:#16a34a;color:#fff}.btn-success:hover{background:#15803d}
.btn-danger{background:#dc2626;color:#fff}.btn-danger:hover{background:#b91c1c}
.btn-outline{background:transparent;color:#93c5fd;border:1px solid #3b82f6}.btn-outline:hover{background:#1e3a5f}
.btn-sm{padding:.35rem .8rem;font-size:.8rem}
.btn:disabled{opacity:.45;cursor:not-allowed}
.err{color:#f87171;font-size:.82rem;margin-top:.5rem}
/* ─── APP ─── */
#app-page{display:none;flex-direction:column;min-height:100vh}
header{background:linear-gradient(135deg,#1e40af,#7c3aed);padding:1rem 1.5rem;display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:.5rem}
header h1{font-size:1.1rem;font-weight:700}
header p{font-size:.8rem;opacity:.8}
.user-pill{background:rgba(255,255,255,.15);border-radius:999px;padding:.3rem .8rem;font-size:.8rem;display:flex;align-items:center;gap:.5rem}
main{flex:1;max-width:1200px;width:100%;margin:0 auto;padding:1.5rem 1rem;display:grid;grid-template-columns:1fr 1fr;gap:1.5rem}
@media(max-width:750px){main{grid-template-columns:1fr}}
.card{background:#1e293b;border:1px solid #334155;border-radius:.75rem;padding:1.25rem}
.card h2{font-size:.95rem;font-weight:600;color:#93c5fd;margin-bottom:1rem}
/* ─── TABS ─── */
.tabs{display:flex;gap:0;margin-bottom:1rem;border-bottom:1px solid #334155}
.tab{background:none;border:none;border-bottom:2px solid transparent;padding:.5rem 1rem;font-size:.85rem;color:#64748b;cursor:pointer;margin-bottom:-1px;transition:.15s}
.tab.active{color:#93c5fd;border-bottom-color:#3b82f6;font-weight:600}
.tab-panel{display:none}.tab-panel.active{display:block}
/* ─── PROSE PANEL ─── */
.prose-area{width:100%;background:#0f172a;border:1px solid #334155;color:#e2e8f0;border-radius:.5rem;padding:.75rem .9rem;font-size:.88rem;resize:vertical;outline:none;min-height:110px;line-height:1.6}
.prose-area:focus{border-color:#3b82f6}
.parse-preview{background:#0f172a;border:1px solid #334155;border-radius:.5rem;padding:.9rem;margin:1rem 0;font-size:.82rem}
.parse-preview .pf-label{color:#64748b;font-size:.75rem;text-transform:uppercase;letter-spacing:.05em;margin-bottom:.2rem}
.parse-preview .pf-val{color:#e2e8f0;margin-bottom:.6rem}
.confidence-badge{display:inline-block;padding:.15rem .5rem;border-radius:999px;font-size:.72rem;font-weight:600}
.conf-high{background:#14532d;color:#86efac}
.conf-medium{background:#713f12;color:#fde68a}
.conf-low{background:#450a0a;color:#fca5a5}
.parse-actions{display:flex;gap:.6rem;align-items:center;flex-wrap:wrap}
/* ─── STRUCTURED PANEL ─── */
label{font-size:.82rem;color:#94a3b8;display:block;margin-bottom:.3rem}
textarea{width:100%;background:#0f172a;border:1px solid #334155;color:#e2e8f0;border-radius:.5rem;padding:.65rem .9rem;font-size:.85rem;resize:vertical;outline:none;min-height:80px}
textarea:focus{border-color:#3b82f6}
.row{display:flex;gap:.75rem;align-items:flex-end;margin-top:.75rem}
.row>div{flex:1}
/* ─── TEST CASES ─── */
.tc-list{display:flex;flex-direction:column;gap:.5rem}
.tc-btn{background:#0f172a;border:1px solid #334155;border-radius:.5rem;padding:.55rem .9rem;cursor:pointer;text-align:left;color:#e2e8f0;font-size:.82rem;transition:.15s;width:100%}
.tc-btn:hover{border-color:#3b82f6;background:#172035}
.tc-btn small{display:block;color:#64748b;margin-top:.15rem}
/* ─── RESULTS ─── */
.status-badge{display:inline-block;padding:.2rem .6rem;border-radius:999px;font-size:.75rem;font-weight:600}
.s-waiting{background:#1e3a5f;color:#93c5fd}
.s-ok{background:#14532d;color:#86efac}
.s-reject{background:#450a0a;color:#fca5a5}
.s-contract{background:#3b0764;color:#d8b4fe}
.s-parse{background:#1c1917;color:#d6d3d1;border:1px solid #44403c}
.vendor-card{background:#0f172a;border:1px solid #334155;border-radius:.5rem;padding:.75rem;margin-top:.75rem;font-size:.82rem}
.vendor-card b{color:#93c5fd}
.log-box{background:#0f172a;border:1px solid #1e293b;border-radius:.5rem;padding:.75rem;font-size:.75rem;font-family:monospace;color:#94a3b8;max-height:180px;overflow-y:auto;margin-top:.75rem;white-space:pre-wrap}
.contract-box{background:#0f172a;border:1px solid #334155;border-radius:.5rem;padding:1rem;font-size:.8rem;max-height:340px;overflow-y:auto;margin-top:.75rem;white-space:pre-wrap;line-height:1.6}
.sep{border:none;border-top:1px solid #334155;margin:.75rem 0}
.spinner{display:inline-block;width:14px;height:14px;border:2px solid #334155;border-top-color:#3b82f6;border-radius:50%;animation:spin .7s linear infinite;vertical-align:middle;margin-right:.4rem}
@keyframes spin{to{transform:rotate(360deg)}}
.approve-row{display:flex;gap:.75rem;margin-top:.75rem}
.flow-steps{display:flex;gap:.4rem;flex-wrap:wrap;margin-bottom:1rem}
.fs{background:#0f172a;border:1px solid #475569;border-radius:.4rem;padding:.3rem .6rem;font-size:.75rem;color:#64748b}
.fs.done{border-color:#16a34a;color:#86efac}
.fs.active{border-color:#2563eb;color:#93c5fd;animation:pulse 1.5s ease-in-out infinite}
.fs.error{border-color:#dc2626;color:#fca5a5}
@keyframes pulse{0%,100%{opacity:1}50%{opacity:.5}}
.arrow{color:#334155;align-self:center;font-size:.8rem}
.parsed-req-box{background:#0f172a;border:1px dashed #3b82f6;border-radius:.5rem;padding:.6rem .9rem;font-size:.82rem;color:#93c5fd;margin-bottom:.75rem;display:none}
.parsed-req-box span{color:#64748b;font-size:.75rem}
</style>
</head>
<body>

<!-- ══════════════════ LOGIN ══════════════════ -->
<div id="login-page">
  <div class="login-box">
    <h1>🏢 Procurement AI System</h1>
    <p>Multi-Agent LangGraph · Groq Llama-3.3-70b · LangSmith</p>
    <div class="users-hint">
      <strong>Demo accounts</strong><br>
      admin / admin123 &nbsp;·&nbsp; buyer / buyer123<br>
      manager / manager123 &nbsp;·&nbsp; analyst / analyst123
    </div>
    <input type="text" id="uname" placeholder="Username" onkeydown="if(event.key==='Enter')doLogin()"/>
    <input type="password" id="upass" placeholder="Password" onkeydown="if(event.key==='Enter')doLogin()"/>
    <button class="btn btn-primary btn-full" onclick="doLogin()">Sign In</button>
    <div id="login-err" class="err"></div>
  </div>
</div>

<!-- ══════════════════ APP ══════════════════ -->
<div id="app-page">
  <header>
    <div>
      <h1>Multi-Agent Procurement System</h1>
      <p>LangGraph · LangChain · Groq llama-3.3-70b · LangSmith</p>
    </div>
    <div style="display:flex;align-items:center;gap:.75rem;flex-wrap:wrap">
      <span class="user-pill">👤 <span id="user-display"></span></span>
      <button class="btn btn-secondary btn-sm" onclick="doLogout()">Sign Out</button>
    </div>
  </header>

  <main>
    <!-- LEFT col -->
    <div style="display:flex;flex-direction:column;gap:1.5rem">

      <!-- INPUT CARD with tabs -->
      <div class="card">
        <h2>📋 Procurement Input</h2>

        <div class="tabs">
          <button class="tab active" onclick="switchTab('prose')" id="tab-prose">✍️ Prose / Free Text</button>
          <button class="tab" onclick="switchTab('structured')" id="tab-structured">🗂 Structured Form</button>
        </div>

        <!-- ── PROSE TAB ── -->
        <div class="tab-panel active" id="panel-prose">
          <label>Describe your need in plain English</label>
          <textarea class="prose-area" id="prose-input" placeholder="e.g. We need servers for our startup, something that auto-scales. Budget is around 4 thousand a month.&#10;&#10;Or: Looking for software licensing for about 500 users, no more than 2500 per month."></textarea>

          <div style="display:flex;gap:.6rem;margin-top:.6rem">
            <button class="btn btn-outline btn-sm" onclick="parseProse()" id="parse-btn">🔍 Parse &amp; Preview</button>
            <span id="parse-status" style="font-size:.8rem;color:#64748b;align-self:center"></span>
          </div>

          <!-- Parse preview -->
          <div class="parse-preview" id="parse-preview" style="display:none">
            <div class="pf-label">Extracted Procurement Request</div>
            <div class="pf-val" id="pp-request"></div>
            <div class="pf-label">Monthly Budget</div>
            <div class="pf-val" id="pp-budget"></div>
            <div class="pf-label">Confidence &nbsp;<span id="pp-conf-badge"></span></div>
            <div class="pf-val" id="pp-assumptions" style="color:#64748b;font-style:italic"></div>
            <hr class="sep"/>
            <div class="parse-actions">
              <button class="btn btn-primary btn-sm" onclick="runFromProse()" id="prose-run-btn">🚀 Confirm &amp; Run Agents</button>
              <button class="btn btn-secondary btn-sm" onclick="editParsed()">✏️ Edit before running</button>
            </div>
          </div>
          <div id="prose-err" class="err"></div>
        </div>

        <!-- ── STRUCTURED TAB ── -->
        <div class="tab-panel" id="panel-structured">
          <!-- Shows parsed values when coming from prose, or manual entry -->
          <div class="parsed-req-box" id="parsed-req-banner">
            <span>Parsed by AI → </span><span id="parsed-req-text"></span>
          </div>
          <label>Request Description</label>
          <textarea id="req-text" rows="3" placeholder="Describe your procurement need in a sentence or two…"></textarea>
          <div class="row">
            <div>
              <label>Budget Limit ($/month)</label>
              <input type="number" id="req-budget" value="5500" min="100" step="100" style="margin-bottom:0"/>
            </div>
            <button class="btn btn-primary btn-sm" onclick="startWorkflow()" id="start-btn">🚀 Run Agents</button>
          </div>
          <div id="start-err" class="err"></div>
        </div>
      </div>

      <!-- TEST CASES -->
      <div class="card">
        <h2>⚡ Preset Test Cases</h2>
        <div class="tc-list" id="tc-list"></div>
      </div>

    </div>

    <!-- RIGHT col: Results -->
    <div style="display:flex;flex-direction:column;gap:1.5rem">

      <div class="card">
        <h2>📊 Agent Pipeline</h2>

        <div class="flow-steps">
          <div class="fs" id="fs-parse">🤖 Parse</div>
          <div class="arrow">→</div>
          <div class="fs" id="fs-research">🔍 Research</div>
          <div class="arrow">→</div>
          <div class="fs" id="fs-analysis">💰 Analysis</div>
          <div class="arrow">→</div>
          <div class="fs" id="fs-approval">👤 Gate</div>
          <div class="arrow">→</div>
          <div class="fs" id="fs-legal">📜 Legal</div>
        </div>

        <div id="status-area" style="font-size:.85rem;color:#64748b">
          Run a workflow to see live results.
        </div>

        <div id="vendor-area" style="display:none">
          <hr class="sep"/>
          <div style="font-size:.82rem;color:#94a3b8;margin-bottom:.4rem">Selected Vendor</div>
          <div class="vendor-card" id="vendor-card"></div>
        </div>

        <div id="vendor-options-area" style="display:none;margin-top:.75rem">
          <div style="font-size:.82rem;color:#94a3b8;margin-bottom:.4rem">All Vendor Options</div>
          <div id="vendor-options-list"></div>
        </div>
      </div>

      <div class="card" id="approval-card" style="display:none">
        <h2>👤 Human Approval Gate</h2>
        <p style="font-size:.83rem;color:#94a3b8">
          LangGraph has <strong style="color:#93c5fd">paused</strong> at
          <code>interrupt_before=["legal_node"]</code>. Review the vendor and decide.
        </p>
        <div class="approve-row">
          <button class="btn btn-success" onclick="decide(true)"  id="approve-btn">✅ Approve</button>
          <button class="btn btn-danger"  onclick="decide(false)" id="reject-btn">❌ Reject</button>
        </div>
        <div id="decide-err" class="err"></div>
      </div>

      <div class="card" id="contract-card" style="display:none">
        <h2>📄 Generated Contract</h2>
        <div class="contract-box" id="contract-box"></div>
      </div>

      <div class="card">
        <h2>🪵 Agent Audit Log</h2>
        <div class="log-box" id="log-box">Logs will appear here…</div>
      </div>

    </div>
  </main>
</div>

<script>
let TOKEN = null, THREAD = null;
let _parsedReq = "", _parsedBudget = 5000, _rawInput = "";

function apiHeaders() {
  return { "Content-Type": "application/json", "Authorization": "Bearer " + TOKEN };
}

// ── Auth ───────────────────────────────────────────────────────────────────────
async function doLogin() {
  const u = document.getElementById("uname").value.trim();
  const p = document.getElementById("upass").value;
  document.getElementById("login-err").textContent = "";
  try {
    const r = await fetch("/auth/login", {
      method: "POST", headers: {"Content-Type":"application/json"},
      body: JSON.stringify({username: u, password: p})
    });
    if (!r.ok) { document.getElementById("login-err").textContent = "Invalid username or password."; return; }
    const d = await r.json();
    TOKEN = d.access_token;
    document.getElementById("user-display").textContent = d.name + " (" + d.role + ")";
    document.getElementById("login-page").style.display = "none";
    document.getElementById("app-page").style.display   = "flex";
    loadTestCases();
  } catch(e) {
    document.getElementById("login-err").textContent = "Connection error.";
  }
}

function doLogout() {
  TOKEN = null; THREAD = null;
  document.getElementById("app-page").style.display  = "none";
  document.getElementById("login-page").style.display = "flex";
  document.getElementById("upass").value = "";
  resetUI();
}

// ── Tabs ───────────────────────────────────────────────────────────────────────
function switchTab(name) {
  ["prose","structured"].forEach(t => {
    document.getElementById("tab-"+t).classList.toggle("active", t===name);
    document.getElementById("panel-"+t).classList.toggle("active", t===name);
  });
}

// ── Test cases ─────────────────────────────────────────────────────────────────
async function loadTestCases() {
  try {
    const r = await fetch("/testcases");
    const cases = await r.json();
    document.getElementById("tc-list").innerHTML = cases.map(tc => `
      <button class="tc-btn" onclick='fillTestCase(${JSON.stringify(tc)})'>
        ${tc.label}
        <small>$${tc.budget.toLocaleString()}/mo · ${tc.expected}</small>
      </button>`).join("");
  } catch(e) {}
}

function fillTestCase(tc) {
  // Always fill structured form
  document.getElementById("req-text").value   = tc.request;
  document.getElementById("req-budget").value = tc.budget;
  document.getElementById("parsed-req-banner").style.display = "none";
  switchTab("structured");
  resetUI();
  appendLog("📋 Test case loaded: " + tc.label);
}

// ── Prose parse ────────────────────────────────────────────────────────────────
async function parseProse() {
  const raw = document.getElementById("prose-input").value.trim();
  if (!raw) { document.getElementById("prose-err").textContent = "Enter some text first."; return; }
  document.getElementById("prose-err").textContent = "";
  document.getElementById("parse-preview").style.display = "none";
  document.getElementById("parse-btn").innerHTML  = '<span class="spinner"></span>Parsing…';
  document.getElementById("parse-btn").disabled   = true;
  document.getElementById("parse-status").textContent = "";

  try {
    const r = await fetch("/workflow/parse", {
      method: "POST", headers: apiHeaders(),
      body: JSON.stringify({raw_input: raw})
    });
    if (r.status === 401) { doLogout(); return; }
    const d = await r.json();
    if (!r.ok) throw new Error(d.detail || "HTTP " + r.status);

    _rawInput    = raw;
    _parsedReq   = d.procurement_request;
    _parsedBudget = d.budget_limit;

    document.getElementById("pp-request").textContent    = d.procurement_request;
    document.getElementById("pp-budget").textContent     = "$" + d.budget_limit.toLocaleString() + " / month";
    document.getElementById("pp-assumptions").textContent = d.assumptions || "—";

    const confClass = {high:"conf-high", medium:"conf-medium", low:"conf-low"}[d.confidence] || "conf-medium";
    document.getElementById("pp-conf-badge").outerHTML =
      `<span id="pp-conf-badge" class="confidence-badge ${confClass}">${d.confidence}</span>`;

    document.getElementById("parse-preview").style.display = "block";
    document.getElementById("parse-status").textContent = "✓ Parsed";

    appendLog("🤖 Prose parsed → " + d.procurement_request.substring(0,80) + (d.procurement_request.length>80?"…":""));
    appendLog("   Budget extracted: $" + d.budget_limit.toLocaleString() + " | Confidence: " + d.confidence);
  } catch(e) {
    document.getElementById("prose-err").textContent = "Parse error: " + e.message;
    appendLog("ERROR parsing: " + e.message);
  } finally {
    document.getElementById("parse-btn").innerHTML = "🔍 Parse &amp; Preview";
    document.getElementById("parse-btn").disabled  = false;
  }
}

function runFromProse() {
  // Kick off agents directly with the parsed values
  _startWorkflow(_parsedReq, _parsedBudget, _rawInput);
}

function editParsed() {
  // Prefill structured form and switch tab
  document.getElementById("req-text").value   = _parsedReq;
  document.getElementById("req-budget").value = _parsedBudget;
  document.getElementById("parsed-req-text").textContent = "AI-parsed from prose";
  document.getElementById("parsed-req-banner").style.display = "block";
  switchTab("structured");
}

// ── Structured run ─────────────────────────────────────────────────────────────
function startWorkflow() {
  const req    = document.getElementById("req-text").value.trim();
  const budget = parseFloat(document.getElementById("req-budget").value);
  if (!req)              { document.getElementById("start-err").textContent = "Enter a procurement request."; return; }
  if (!budget || budget <= 0) { document.getElementById("start-err").textContent = "Enter a valid budget."; return; }
  document.getElementById("start-err").textContent = "";
  _startWorkflow(req, budget, "");
}

// ── Core: calls /workflow/start ────────────────────────────────────────────────
async function _startWorkflow(req, budget, rawInput) {
  resetUI();

  const startBtn = document.getElementById("start-btn");
  const proseBtn = document.getElementById("prose-run-btn");
  startBtn.innerHTML = '<span class="spinner"></span>Running…'; startBtn.disabled = true;
  proseBtn.innerHTML = '<span class="spinner"></span>Running…'; proseBtn.disabled = true;

  setStep("fs-parse", "active");
  appendLog("🚀 Starting workflow…");
  if (rawInput) appendLog('   Raw input: "' + rawInput.substring(0,80) + (rawInput.length>80?"...":"") + '"');

  try {
    const r = await fetch("/workflow/start", {
      method: "POST", headers: apiHeaders(),
      body: JSON.stringify({
        procurement_request: req,
        budget_limit: budget,
        raw_input: rawInput,
      })
    });
    if (r.status === 401) { doLogout(); return; }
    const d = await r.json();
    if (!r.ok) throw new Error(d.detail || "HTTP " + r.status);

    THREAD = d.thread_id;

    // Show events in log
    (d.events || []).forEach(ev => appendLog("  [" + ev.node + "] processed"));
    (d.logs   || []).forEach(l  => appendLog(l));

    // If raw input was used, show what the LLM actually sent to research
    if (rawInput && d.procurement_request && d.procurement_request !== req) {
      appendLog("🤖 ParseNode rewrote request → " + d.procurement_request.substring(0,80));
    }

    setStep("fs-parse",    "done");
    setStep("fs-research", "done");
    setStep("fs-analysis",  d.analysis_approved ? "done" : "error");

    renderVendorOptions(d.vendor_options, d.budget_limit);

    if (d.analysis_approved) {
      renderVendorCard(d.selected_vendor, d.budget_limit);
      setStep("fs-approval", "active");
      document.getElementById("status-area").innerHTML =
        '<span class="status-badge s-waiting">⏸ Awaiting Human Approval</span> ' +
        '<span style="color:#64748b;font-size:.78rem">Graph paused · interrupt_before=["legal_node"]</span>';
      document.getElementById("approval-card").style.display = "block";
    } else {
      renderVendorCard(d.selected_vendor, d.budget_limit);
      document.getElementById("status-area").innerHTML =
        '<span class="status-badge s-reject">❌ Analysis Rejected — all vendors exceed budget</span>';
      appendLog("❌ Budget exceeded. Workflow terminated.");
    }
  } catch(e) {
    document.getElementById("status-area").innerHTML = '<span style="color:#f87171">Error: ' + e.message + '</span>';
    appendLog("ERROR: " + e.message);
  } finally {
    startBtn.innerHTML = "🚀 Run Agents"; startBtn.disabled = false;
    proseBtn.innerHTML = "🚀 Confirm &amp; Run Agents"; proseBtn.disabled = false;
  }
}

// ── Decide ─────────────────────────────────────────────────────────────────────
async function decide(approve) {
  if (!THREAD) return;
  setDecideButtons(false);
  document.getElementById("decide-err").textContent = "";
  appendLog((approve ? "✅ Human APPROVED" : "❌ Human REJECTED") + " — resuming graph…");

  try {
    const r = await fetch("/workflow/" + THREAD + "/decide", {
      method: "POST", headers: apiHeaders(),
      body: JSON.stringify({approve})
    });
    if (r.status === 401) { doLogout(); return; }
    const d = await r.json();
    if (!r.ok) throw new Error(d.detail || "HTTP " + r.status);

    (d.events || []).forEach(ev => appendLog("  [" + ev.node + "] processed"));
    (d.logs   || []).forEach(l  => appendLog(l));

    document.getElementById("approval-card").style.display = "none";

    if (approve && d.contract_draft) {
      setStep("fs-approval", "done");
      setStep("fs-legal",    "done");
      document.getElementById("status-area").innerHTML =
        '<span class="status-badge s-contract">✅ Contract Generated</span>';
      document.getElementById("contract-box").textContent = d.contract_draft;
      document.getElementById("contract-card").style.display = "block";
      appendLog("📄 Contract ready.");
    } else if (approve) {
      setStep("fs-approval", "done");
      document.getElementById("status-area").innerHTML =
        '<span class="status-badge s-ok">Approved — contract pending</span>';
    } else {
      setStep("fs-approval", "error");
      document.getElementById("status-area").innerHTML =
        '<span class="status-badge s-reject">🚫 Rejected by Human Reviewer</span>';
      appendLog("🚫 Workflow rejected.");
    }
  } catch(e) {
    document.getElementById("decide-err").textContent = "Error: " + e.message;
    setDecideButtons(true);
  }
}

// ── UI helpers ─────────────────────────────────────────────────────────────────
function resetUI() {
  THREAD = null;
  ["fs-parse","fs-research","fs-analysis","fs-approval","fs-legal"].forEach(id => {
    document.getElementById(id).className = "fs";
  });
  document.getElementById("status-area").innerHTML = '<span style="color:#64748b">Ready to run.</span>';
  document.getElementById("vendor-area").style.display         = "none";
  document.getElementById("vendor-options-area").style.display = "none";
  document.getElementById("approval-card").style.display       = "none";
  document.getElementById("contract-card").style.display       = "none";
  document.getElementById("log-box").textContent = "Logs will appear here…";
  document.getElementById("start-err").textContent  = "";
  document.getElementById("decide-err").textContent = "";
  setDecideButtons(true);
}

function setStep(id, state) {
  document.getElementById(id).className = "fs " + state;
}

function setDecideButtons(enabled) {
  document.getElementById("approve-btn").disabled = !enabled;
  document.getElementById("reject-btn").disabled  = !enabled;
}

function appendLog(msg) {
  const el = document.getElementById("log-box");
  if (el.textContent === "Logs will appear here…") el.textContent = "";
  el.textContent += msg + "\n";
  el.scrollTop = el.scrollHeight;
}

function renderVendorCard(v, budget) {
  if (!v || !v.name) return;
  const within = v.price_per_month <= budget;
  document.getElementById("vendor-card").innerHTML =
    `<b>${v.name}</b> <span class="status-badge ${within?'s-ok':'s-reject'}" style="margin-left:.4rem">${within?"✅ Within":"❌ Over"} budget</span><br/>
    <span style="color:#94a3b8">Service:</span> ${v.service_type} &nbsp;|&nbsp;
    <span style="color:#94a3b8">Price:</span> $${v.price_per_month.toLocaleString()}/mo &nbsp;|&nbsp;
    <span style="color:#94a3b8">Score:</span> ${v.reputation_score}/10<br/>
    <span style="color:#94a3b8">Capabilities:</span> ${v.capabilities.join(" · ")}<br/>
    <span style="color:#94a3b8">Terms:</span> ${v.contract_terms}`;
  document.getElementById("vendor-area").style.display = "block";
}

function renderVendorOptions(vendors, budget) {
  if (!vendors || !vendors.length) return;
  document.getElementById("vendor-options-list").innerHTML = vendors.map(v => {
    const w = v.price_per_month <= budget;
    return `<div class="vendor-card" style="margin-bottom:.4rem">
      <b>${v.name}</b>
      <span class="status-badge ${w?'s-ok':'s-reject'}" style="margin-left:.4rem">${w?"✅":"❌"} $${v.price_per_month.toLocaleString()}/mo</span>
      &nbsp; Score: ${v.reputation_score}/10 &nbsp;|&nbsp; ${v.capabilities.join(", ")}
    </div>`;
  }).join("");
  document.getElementById("vendor-options-area").style.display = "block";
}
</script>
</body>
</html>""")