KyleAIers commited on
Commit
68fcd52
·
verified ·
1 Parent(s): afc3521

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +227 -14
index.html CHANGED
@@ -3,25 +3,238 @@
3
  <head>
4
  <meta charset="UTF-8" />
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
- <title>HealthyLifePhil - Assistant</title>
7
- <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
8
- <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
9
- <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
 
 
 
 
10
  <script src="https://cdn.tailwindcss.com"></script>
11
  <script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2"></script>
12
-
13
- <script type="module">
14
- import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.14.0';
15
- window.pipeline = pipeline;
16
- env.allowRemoteModels = true;
17
- </script>
18
  </head>
19
- <body class="bg-slate-950 font-sans">
20
  <div id="root"></div>
21
 
22
- <script type="text/babel" data-type="module">
23
- import { App } from './src/app.js';
24
- ReactDOM.createRoot(document.getElementById('root')).render(<App />);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  </script>
26
  </body>
27
  </html>
 
3
  <head>
4
  <meta charset="UTF-8" />
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>HealthyLifePhil Assistant</title>
7
+
8
+ <!-- UMD React Dependencies -->
9
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"></script>
10
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
11
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/7.23.5/babel.min.js"></script>
12
+
13
+ <!-- Styling & Supabase -->
14
  <script src="https://cdn.tailwindcss.com"></script>
15
  <script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2"></script>
 
 
 
 
 
 
16
  </head>
17
+ <body class="bg-slate-950 text-white font-sans">
18
  <div id="root"></div>
19
 
20
+ <script type="text/babel">
21
+ const { useState } = React;
22
+
23
+ // Supabase Initialization
24
+ const SUPABASE_URL = "https://ixntxgqaggkaaekewazu.supabase.co";
25
+ const SUPABASE_ANON_KEY = "sb_publishable_7SZ_z5FKbA77vZ04rnJKGQ_zXssCIT0";
26
+ const supabase = window.supabase.createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
27
+
28
+ // Guardrail Check
29
+ const validateGuardrails = (text) => {
30
+ const exploitPattern = /(drop\s+table|delete\s+from|select\s+\*|insert\s+into|alter\s+table|union\s+select|<script>|javascript:|eval\(|or\s+1=1|--|\/\*)/i;
31
+ if (exploitPattern.test(text)) {
32
+ return { blocked: true, reason: "🛡️ [Security Guardrail]: Query blocked due to forbidden database operations." };
33
+ }
34
+
35
+ const forbiddenTerms = ['hack', 'h4ck', 'exploit', 'scam', 'steal', 'bypass', 'jailbreak', 'attack', 'payload'];
36
+ const normalized = text.toLowerCase().replace(/[^a-z0-9\s]/g, '');
37
+ const words = normalized.split(/\s+/);
38
+ if (forbiddenTerms.some(term => words.some(w => w.includes(term)))) {
39
+ return { blocked: true, reason: "🛡️ [Security Guardrail]: System limited strictly to store management requests." };
40
+ }
41
+
42
+ return { blocked: false };
43
+ };
44
+
45
+ // Intent Router
46
+ const detectIntent = (text) => {
47
+ const lower = text.toLowerCase().trim();
48
+ const invTerms = ['inventory', 'stock', 'product', 'items', 'supplies', 'units'];
49
+ const salesTerms = ['sales', 'revenue', 'orders', 'earnings', 'performance', 'stat'];
50
+
51
+ if (invTerms.some(i => lower.includes(i))) return 'INVENTORY';
52
+ if (salesTerms.some(s => lower.includes(s))) return 'SALES';
53
+ return 'CHAT';
54
+ };
55
+
56
+ // Card Renderer Component
57
+ const DataResponseCard = ({ data }) => {
58
+ if (data.type === 'inventory') {
59
+ return (
60
+ <div className="space-y-3">
61
+ <p className="text-slate-200">Current store inventory details:</p>
62
+ <div className="grid grid-cols-2 gap-2 text-xs">
63
+ <div className="bg-slate-900 p-2.5 rounded-lg border border-slate-700">
64
+ <span className="text-slate-400 block">Product Lines</span>
65
+ <span className="text-lg font-bold text-emerald-400">{data.productCount}</span>
66
+ </div>
67
+ <div className="bg-slate-900 p-2.5 rounded-lg border border-slate-700">
68
+ <span className="text-slate-400 block">Total Units</span>
69
+ <span className="text-lg font-bold text-emerald-400">{data.totalUnits}</span>
70
+ </div>
71
+ </div>
72
+
73
+ <div className="bg-slate-900 rounded-lg p-3 border border-slate-700 space-y-1 text-xs">
74
+ <div className="font-semibold text-slate-300 mb-1">Items List</div>
75
+ {data.products.length === 0 ? (
76
+ <p className="text-slate-500">No products found in database.</p>
77
+ ) : (
78
+ data.products.map(p => (
79
+ <div key={p.id || p.product_name} className="flex justify-between py-1 border-b border-slate-800 last:border-0">
80
+ <span>{p.product_name}</span>
81
+ <span className="text-slate-400">{p.stock_quantity} in stock (₱{p.price})</span>
82
+ </div>
83
+ ))
84
+ )}
85
+ </div>
86
+ </div>
87
+ );
88
+ }
89
+
90
+ if (data.type === 'sales') {
91
+ return (
92
+ <div className="space-y-3">
93
+ <p className="text-slate-200">Sales metrics summary:</p>
94
+ <div className="grid grid-cols-3 gap-2 text-xs">
95
+ <div className="bg-slate-900 p-2.5 rounded-lg border border-slate-700">
96
+ <span className="text-slate-400 block">Revenue</span>
97
+ <span className="text-sm font-bold text-emerald-400">₱{data.totalRev.toFixed(2)}</span>
98
+ </div>
99
+ <div className="bg-slate-900 p-2.5 rounded-lg border border-slate-700">
100
+ <span className="text-slate-400 block">Orders</span>
101
+ <span className="text-sm font-bold text-emerald-400">{data.totalOrders}</span>
102
+ </div>
103
+ <div className="bg-slate-900 p-2.5 rounded-lg border border-slate-700">
104
+ <span className="text-slate-400 block">Average</span>
105
+ <span className="text-sm font-bold text-emerald-400">₱{data.avgOrder.toFixed(2)}</span>
106
+ </div>
107
+ </div>
108
+ </div>
109
+ );
110
+ }
111
+
112
+ return null;
113
+ };
114
+
115
+ // Main UI Component
116
+ function App() {
117
+ const [messages, setMessages] = useState([
118
+ { sender: "bot", type: "text", text: "HealthyLifePhil Assistant Active. Ask about inventory, sales metrics, or chat." }
119
+ ]);
120
+ const [input, setInput] = useState("");
121
+ const [loading, setLoading] = useState(false);
122
+
123
+ const handleChat = async () => {
124
+ if (!input.trim() || loading) return;
125
+
126
+ const userText = input.trim();
127
+ setMessages(prev => [...prev, { sender: "user", type: "text", text: userText }]);
128
+ setInput("");
129
+ setLoading(true);
130
+
131
+ try {
132
+ // 1. Guardrail Validation
133
+ const guardrail = validateGuardrails(userText);
134
+ if (guardrail.blocked) {
135
+ setMessages(prev => [...prev, { sender: "bot", type: "text", text: guardrail.reason }]);
136
+ setLoading(false);
137
+ return;
138
+ }
139
+
140
+ // 2. Intent Classification
141
+ const intent = detectIntent(userText);
142
+
143
+ if (intent === 'INVENTORY') {
144
+ const { data: products, error } = await supabase.from('products').select('*');
145
+ if (error) throw error;
146
+
147
+ const totalUnits = products ? products.reduce((acc, p) => acc + Number(p.stock_quantity || 0), 0) : 0;
148
+
149
+ setMessages(prev => [...prev, {
150
+ sender: "bot",
151
+ type: "data",
152
+ data: { type: 'inventory', productCount: products ? products.length : 0, totalUnits, products: products || [] }
153
+ }]);
154
+ setLoading(false);
155
+ return;
156
+ }
157
+
158
+ if (intent === 'SALES') {
159
+ const { data: sales, error } = await supabase.from('orders').select('*');
160
+ if (error) throw error;
161
+
162
+ const totalRev = sales ? sales.reduce((acc, i) => acc + Number(i.total_amount || 0), 0) : 0;
163
+ const totalOrders = sales ? sales.length : 0;
164
+ const avgOrder = totalOrders > 0 ? (totalRev / totalOrders) : 0;
165
+
166
+ setMessages(prev => [...prev, {
167
+ sender: "bot",
168
+ type: "data",
169
+ data: { type: 'sales', totalRev, totalOrders, avgOrder }
170
+ }]);
171
+ setLoading(false);
172
+ return;
173
+ }
174
+
175
+ // 3. Conversational Fallback
176
+ setMessages(prev => [...prev, {
177
+ sender: "bot",
178
+ type: "text",
179
+ text: "Hello! I am your store assistant. You can ask me to list inventory or check sales data."
180
+ }]);
181
+
182
+ } catch (err) {
183
+ console.error(err);
184
+ setMessages(prev => [...prev, { sender: "bot", type: "text", text: "Database connection failed. Please try again." }]);
185
+ } finally {
186
+ setLoading(false);
187
+ }
188
+ };
189
+
190
+ return (
191
+ <div className="flex h-screen bg-slate-950 text-white">
192
+ <div className="w-64 bg-slate-900 p-4 border-r border-slate-800 flex flex-col justify-between hidden md:flex">
193
+ <div>
194
+ <h1 className="text-md font-bold text-white mb-6">HealthyLifePhil</h1>
195
+ <p className="text-xs text-slate-400">System Online</p>
196
+ </div>
197
+ <div className="text-xs text-emerald-400">● Engine Active</div>
198
+ </div>
199
+
200
+ <div className="flex-1 flex flex-col">
201
+ <header className="p-4 bg-slate-900 border-b border-slate-800 font-semibold text-sm">
202
+ HealthyLifePhil Store Assistant
203
+ </header>
204
+
205
+ <div className="flex-1 p-6 overflow-y-auto space-y-4">
206
+ {messages.map((m, index) => (
207
+ <div key={index} className={`flex ${m.sender === 'user' ? 'justify-end' : 'justify-start'}`}>
208
+ <div className={`p-4 rounded-xl max-w-lg text-sm ${m.sender === 'user' ? 'bg-emerald-600 text-white' : 'bg-slate-800 text-slate-200 border border-slate-700'}`}>
209
+ {m.type === 'data' ? <DataResponseCard data={m.data} /> : m.text}
210
+ </div>
211
+ </div>
212
+ ))}
213
+ {loading && <div className="text-xs text-emerald-400 italic animate-pulse">Processing query...</div>}
214
+ </div>
215
+
216
+ <div className="p-4 bg-slate-900 border-t border-slate-800 flex gap-2">
217
+ <input
218
+ type="text"
219
+ className="flex-1 p-3 rounded-lg bg-slate-800 border border-slate-700 text-white text-sm focus:outline-none focus:border-emerald-500"
220
+ placeholder="Ask about inventory, sales, or chat..."
221
+ value={input}
222
+ onChange={(e) => setInput(e.target.value)}
223
+ onKeyDown={(e) => e.key === 'Enter' && handleChat()}
224
+ />
225
+ <button onClick={handleChat} disabled={loading} className="bg-emerald-600 hover:bg-emerald-500 px-5 py-2 rounded-lg font-bold text-sm text-black font-semibold">
226
+ Send
227
+ </button>
228
+ </div>
229
+ </div>
230
+ </div>
231
+ );
232
+ }
233
+
234
+ // Mount React App
235
+ const rootElement = document.getElementById('root');
236
+ const root = ReactDOM.createRoot(rootElement);
237
+ root.render(<App />);
238
  </script>
239
  </body>
240
  </html>