-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
116 lines (100 loc) · 3.43 KB
/
script.js
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
// Event listener for the send button
document.getElementById('send-btn').addEventListener('click', function () {
sendMessage();
});
// Event listener for theme toggle
document.getElementById('theme-toggle').addEventListener('change', function () {
document.body.classList.toggle('dark-theme');
});
// Event listener for pressing Enter key in the input field
document
.getElementById('user-input')
.addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
sendMessage();
}
});
async function sendMessage() {
let userInput = document.getElementById('user-input').value;
if (!userInput) return;
addMessage('user', userInput);
document.getElementById('user-input').value = ''; // Clear the input field
try {
const response = await fetch(
'https://autonomous-ai-nu3o8c.5sc6y6-1.usa-e2.cloudhub.io/aiservice',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({ question: userInput }),
}
);
const data = await response.json();
const formattedReply = formatReply(data.response);
addMessage('bot', formattedReply);
} catch (error) {
addMessage('bot', 'Error: Unable to communicate with the AI.');
console.error('Error:', error);
}
}
function addMessage(sender, message) {
let outputDiv = document.getElementById('output');
let messageDiv = document.createElement('div');
messageDiv.classList.add(sender);
messageDiv.innerHTML = message;
outputDiv.appendChild(messageDiv);
// Auto-scroll to the bottom
messageDiv.scrollIntoView({ behavior: 'smooth', block: 'end' });
}
function formatReply(reply) {
// Assuming `reply` is a plain text string
return reply.replace(/\n/g, '<br>');
}
function formatReply1(reply) {
// Split the reply string at the first colon
const colonIndex = reply.indexOf(':');
const mainMessage = reply.substring(0, colonIndex).trim();
const sections = reply.substring(colonIndex + 1).split('\n\n');
// Join the main message with a line break
let formattedMessage = mainMessage + '<br>';
// For each section, handle bullet points and paragraphs
sections.forEach((section) => {
const paragraphs = section.trim().split('/\n-|\n -/');
paragraphs.forEach((paragraph, index) => {
const trimmedParagraph = paragraph.trim();
if (trimmedParagraph) {
if (index === 0) {
formattedMessage += `${trimmedParagraph}<br>`;
} else {
formattedMessage += `- ${trimmedParagraph}<br>`;
}
} else if (index === paragraphs.length - 1) {
formattedMessage += '<br>';
}
});
});
return formattedMessage;
}
// Function to add the introductory message from the bot
function addIntroMessage() {
const introMessage = `
<p>Hi, I'm Max Mule, an AI Agent built with MuleChain on the MuleSoft Anypoint Platform.</p>
<br>
<p>Here are my key skills:</p>
<ul>
<li>- Check <b>SAP ECC</b> inventory</li>
<li>- Retrieve <b>SAP S4H</b> order details</li>
<li>- Access <b>Salesforce</b> CRM accounts details</li>
<li>- Gather <b>Hubspot</b> sales leads</li>
<li>- Display <b>Workday</b> employee info</li>
<li>- Order laptops from your asset <b>portal</b></li>
</ul>
`;
addMessage('bot', introMessage);
}
// Add the introductory message when the page loads
window.onload = function () {
addIntroMessage();
};