-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmp-index-script.js
More file actions
177 lines (139 loc) · 6.47 KB
/
tmp-index-script.js
File metadata and controls
177 lines (139 loc) · 6.47 KB
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
const dslInput = document.getElementById('dsl-input');
const compileBtn = document.getElementById('compile-btn');
const resultArea = document.getElementById('result-area');
const botInfo = document.getElementById('bot-info');
const botIdSpan = document.getElementById('bot-id');
const embedCodeDiv = document.getElementById('embed-code');
const exampleBtns = document.querySelectorAll('.example-btn');
// Example DSL templates
const examples = {
ecommerce: `bot FashionStore
domain ecommerce
tone friendly
welcome "👋 Welcome to FashionHub! I'm here to help you find the perfect outfit. What are you looking for today?"
intent product_search
keywords: shirts pants dresses shoes jeans jacket hoodie
response "Great choice! We have several options. Let me help you narrow it down:
👉 What size are you looking for? (S, M, L, XL)
👉 What's your budget range?
👉 Any specific color preference?"
intent check_order
keywords: order status tracking delivery shipped
response "I can help you track your order! Please provide your order number (starts with 'FH-') and I'll check the status for you."
intent return_policy
keywords: return refund exchange policy
response "Our return policy allows returns within 30 days of purchase. Items must be unused with original tags. We'll process your refund within 5-7 business days after receiving the item."
fallback "I'm sorry, I didn't understand your request. You can ask me about products, orders, or our return policy. How else can I help you?"`,
education: `bot AdmissionsBot
domain education
tone formal
welcome "Welcome to University Admissions! I'm here to help you with information about our programs and application process."
intent program_info
keywords: programs courses degrees majors
response "We offer Bachelor's and Master's degrees in Computer Science, Business, Engineering, and Liberal Arts. Which program interests you most?"
intent application_deadline
keywords: deadline apply application when
response "Our application deadlines are:
- Fall semester: March 15th
- Spring semester: November 1st
- Summer semester: April 1st
Don't miss out!"
intent tuition_fees
keywords: cost tuition fees price
response "Our tuition rates are:
- Undergraduate: $25,000 per year
- Graduate: $30,000 per year
Financial aid and scholarships are available for eligible students."
fallback "I'm sorry, I don't have information about that. Please contact our admissions office at admissions@university.edu or call (555) 123-4567."`,
saas: `bot SupportBot
domain saas
tone professional
welcome "Hello! Welcome to TechCorp Support. How can I assist you with our platform today?"
intent account_setup
keywords: setup account create login register
response "To set up your account:
1. Visit app.techcorp.com/signup
2. Enter your email and create a password
3. Verify your email address
4. Complete your profile
Need help with any of these steps?"
intent billing_issue
keywords: bill payment charge invoice subscription
response "For billing questions:
- View invoices in your account dashboard
- Update payment methods in Settings > Billing
- Contact billing@techcorp.com for disputes
What specific billing issue are you experiencing?"
intent feature_request
keywords: feature request suggest improvement
response "We love hearing feature suggestions! Please submit them through:
- Our feedback form in the app
- Email: features@techcorp.com
- Our community forum
Your input helps us improve!"
fallback "I'm sorry, I don't have information about that. Please check our help center at help.techcorp.com or contact support@techcorp.com for assistance."`
};
// Load example DSL
exampleBtns.forEach(btn => {
btn.addEventListener('click', () => {
const example = btn.dataset.example;
dslInput.value = examples[example];
});
});
// Compile DSL
compileBtn.addEventListener('click', async () => {
const dsl = dslInput.value.trim();
if (!dsl) {
showResult('Please enter DSL code to compile.', 'error');
return;
}
// Show loading state
compileBtn.disabled = true;
compileBtn.innerHTML = '<span class="loading"></span>Compiling...';
try {
const response = await fetch('/api/compile', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ dsl })
});
const text = await response.text();
let data;
try {
data = text ? JSON.parse(text) : {};
} catch (parseError) {
throw new Error(`Invalid JSON response from server: ${parseError.message}\n${text}`);
}
if (!response.ok) {
const errorMessage = data.error || `Server error ${response.status}`;
throw new Error(errorMessage);
}
if (data.success) {
showResult(`✅ Compilation successful!\n\n📊 Stats:\n- Tokens: ${data.stats.tokens}\n- Intents: ${data.stats.intents}\n- Keywords: ${data.stats.keywords}`, 'success');
// Show bot info
botIdSpan.textContent = data.bot_id;
embedCodeDiv.textContent = `<script src="/widget.js" data-bot-id="${data.bot_id}"><\/script>`;
botInfo.classList.add('show');
} else {
showResult(`❌ Compilation failed:\n${data.error || 'Unknown error'}`, 'error');
botInfo.classList.remove('show');
}
} catch (error) {
showResult(`❌ Request error: ${error.message}`, 'error');
botInfo.classList.remove('show');
} finally {
// Reset button
compileBtn.disabled = false;
compileBtn.textContent = '⚡ Compile Bot';
}
});
function showResult(message, type) {
resultArea.textContent = message;
resultArea.className = `result-area ${type}`;
}
// Auto-resize textarea
dslInput.addEventListener('input', function() {
this.style.height = 'auto';
this.style.height = this.scrollHeight + 'px';
});