File size: 8,287 Bytes
732e77c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!-- /app/assets/html/agenticcore_frontend.html -->
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>AgenticCore Chatbot Frontend</title>
  <style>
    :root {
      --bg: #0b0d12;
      --panel: #0f172a;
      --panel-2: #111827;
      --text: #e5e7eb;
      --muted: #9ca3af;
      --accent: #60a5fa;
      --border: #1f2940;
      --danger: #ef4444;
      --success: #22c55e;
    }
    * { box-sizing: border-box; }
    body { margin: 0; font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif; background: var(--bg); color: var(--text); }
    .wrap { max-width: 920px; margin: 32px auto; padding: 0 16px; }
    header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; gap: 16px; }
    header h1 { font-size: 18px; margin: 0; letter-spacing: .3px; }
    header .badge { font-size: 12px; opacity: .85; padding: 4px 8px; border:1px solid var(--border); border-radius: 999px; background: rgba(255,255,255,0.03); }
    .card { background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 16px; }
    .row { display: flex; gap: 10px; align-items: center; }
    .stack { display: grid; gap: 12px; }
    label { font-size: 12px; color: var(--muted); }
    input[type=text] { flex: 1; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--panel-2); color: var(--text); outline: none; }
    input[type=text]::placeholder { color: #6b7280; }
    button { padding: 10px 14px; border-radius: 12px; border: 1px solid var(--border); background: #1f2937; color: var(--text); cursor: pointer; transition: transform .02s ease, background .2s; }
    button:hover { background: #273449; }
    button:active { transform: translateY(1px); }
    .btn-primary { background: #1f2937; border-color: #31405a; }
    .btn-ghost { background: transparent; border-color: var(--border); }
    .grid { display: grid; gap: 12px; }
    .grid-2 { grid-template-columns: 1fr 1fr; }
    .log { margin-top: 16px; display: grid; gap: 10px; }
    .bubble { max-width: 80%; padding: 12px 14px; border-radius: 14px; line-height: 1.35; }
    .user { background: #1e293b; border:1px solid #2b3b55; margin-left: auto; border-bottom-right-radius: 4px; }
    .bot  { background: #0d1b2a; border:1px solid #223049; margin-right: auto; border-bottom-left-radius: 4px; }
    .meta { font-size: 12px; color: var(--muted); margin-top: 4px; }
    pre { margin: 0; white-space: pre-wrap; word-break: break-word; }
    .status { display:flex; align-items:center; gap:8px; font-size: 12px; color: var(--muted); }
    .dot { width:8px; height:8px; border-radius:999px; background: #64748b; display:inline-block; }
    .dot.ok { background: var(--success); }
    .dot.bad { background: var(--danger); }
    footer { margin: 24px 0; text-align:center; color: var(--muted); font-size: 12px; }
    .small { font-size: 12px; }
    @media (max-width: 700px) { .grid-2 { grid-template-columns: 1fr; } }
  </style>
</head>
<body>
  <div class="wrap">
    <header>
      <h1>AgenticCore Chatbot Frontend</h1>
      <div class="badge">Frontend → FastAPI → providers_unified</div>
    </header>

    <section class="card stack">
      <div class="grid grid-2">
        <div class="stack">
          <label for="backend">Backend URL</label>
          <div class="row">
            <input id="backend" type="text" placeholder="http://127.0.0.1:8000" />
            <button id="save" class="btn-ghost">Save</button>
          </div>
          <div class="status" id="status"><span class="dot"></span><span>Not checked</span></div>
        </div>
        <div class="stack">
          <label for="message">Message</label>
          <div class="row">
            <input id="message" type="text" placeholder="Type a message…" />
            <button id="send" class="btn-primary">Send</button>
          </div>
          <div class="row">
            <button id="cap" class="btn-ghost small">Capabilities</button>
            <button id="health" class="btn-ghost small">Health</button>
            <button id="clear" class="btn-ghost small">Clear</button>
          </div>
        </div>
      </div>
      <div class="log" id="log"></div>
    </section>

    <footer>
      Use with your FastAPI backend at <code>/chatbot/message</code>. Configure CORS if you serve this file from a different origin.
    </footer>
  </div>

  <script>
    const $ = (sel) => document.querySelector(sel);
    const backendInput = $('#backend');
    const sendBtn = $('#send');
    const saveBtn = $('#save');
    const msgInput = $('#message');
    const capBtn = $('#cap');
    const healthBtn = $('#health');
    const clearBtn = $('#clear');
    const log = $('#log');
    const status = $('#status');
    const dot = status.querySelector('.dot');
    const statusText = status.querySelector('span:last-child');

    function getBackendUrl() {
      return localStorage.getItem('BACKEND_URL') || 'http://127.0.0.1:8000';
    }
    function setBackendUrl(v) {
      localStorage.setItem('BACKEND_URL', v);
    }
    function cardUser(text) {
      const div = document.createElement('div');
      div.className = 'bubble user';
      div.textContent = text;
      log.appendChild(div);
      log.scrollTop = log.scrollHeight;
    }
    function cardBot(obj) {
      const wrap = document.createElement('div');
      wrap.className = 'bubble bot';
      const pre = document.createElement('pre');
      pre.textContent = typeof obj === 'string' ? obj : JSON.stringify(obj, null, 2);
      wrap.appendChild(pre);
      log.appendChild(wrap);
      log.scrollTop = log.scrollHeight;
    }
    function setStatus(ok, text) {
      dot.classList.toggle('ok', !!ok);
      dot.classList.toggle('bad', ok === false);
      statusText.textContent = text || (ok ? 'OK' : 'Error');
    }
    async function api(path, init) {
      const base = backendInput.value.trim().replace(/\/$/, '');
      const url = base + path;
      const resp = await fetch(url, init);
      if (!resp.ok) {
        let t = await resp.text().catch(() => '');
        throw new Error(`HTTP ${resp.status} ${resp.statusText}${t}`);
      }
      const contentType = resp.headers.get('content-type') || '';
      if (contentType.includes('application/json')) return resp.json();
      return resp.text();
    }

    async function checkHealth() {
      try {
        const h = await api('/health', { method: 'GET' });
        setStatus(true, 'Healthy');
        cardBot({ health: h });
      } catch (e) {
        setStatus(false, String(e.message || e));
        cardBot({ error: String(e.message || e) });
      }
    }

    async function sendMessage() {
      const text = msgInput.value.trim();
      if (!text) return;
      cardUser(text);
      msgInput.value = '';
      try {
        const data = await api('/chatbot/message', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ message: text })
        });
        cardBot(data);
      } catch (e) {
        cardBot({ error: String(e.message || e) });
      }
    }

    async function showCapabilities() {
      try {
        // Prefer API if available; if 404, fall back to library-like prompt.
        const data = await api('/chatbot/message', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ message: 'help' })
        });
        cardBot(data);
      } catch (e) {
        cardBot({ capabilities: ['text-input','sentiment-analysis','help'], note: 'API help failed, showing defaults', error: String(e.message || e) });
      }
    }

    // Wire up
    backendInput.value = getBackendUrl();
    saveBtn.onclick = () => { setBackendUrl(backendInput.value.trim()); setStatus(null, 'Saved'); };
    sendBtn.onclick = sendMessage;
    msgInput.addEventListener('keydown', (ev) => { if (ev.key === 'Enter') sendMessage(); });
    capBtn.onclick = showCapabilities;
    healthBtn.onclick = checkHealth;
    clearBtn.onclick = () => { log.innerHTML = ''; setStatus(null, 'Idle'); };

    // Initial health ping
    checkHealth();
  </script>
</body>
</html>