Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandodilland authored Nov 16, 2024
0 parents commit c3cf7d6
Show file tree
Hide file tree
Showing 9 changed files with 1,166 additions and 0 deletions.
137 changes: 137 additions & 0 deletions css/workers-ai-chat.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
.workers-ai-chat-container {
border: 1px solid #ccc;
padding: 15px;
max-width: 600px;
margin: 20px auto;
border-radius: 5px;
background-color: #f9f9f9;
font-family: Arial, sans-serif;
}

/* Styles for predefined questions */
.workers-ai-predefined-questions {
margin-bottom: 15px;
text-align: center;
}

.workers-ai-predefined-questions .workers-ai-question-button {
margin: 5px;
padding: 8px 12px;
background-color: #0073aa;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
font-size: 14px;
}

.workers-ai-predefined-questions .workers-ai-question-button:hover {
background-color: #005177;
}

.workers-ai-chat-box {
max-height: 400px;
overflow-y: auto;
margin-bottom: 15px;
padding-right: 10px;
}

.user-message, .ai-message {
margin-bottom: 10px;
}

.user-message strong {
color: #0073aa;
}

.ai-message strong {
color: #d54e21;
}

/* Styles for Markdown content */
.workers-ai-chat-box .ai-message,
.workers-ai-chat-box .user-message {
word-wrap: break-word;
}

.workers-ai-chat-box h1,
.workers-ai-chat-box h2,
.workers-ai-chat-box h3,
.workers-ai-chat-box h4,
.workers-ai-chat-box h5,
.workers-ai-chat-box h6 {
margin: 10px 0 5px;
font-weight: bold;
}

.workers-ai-chat-box p {
margin: 5px 0;
}

.workers-ai-chat-box a {
color: #0073aa;
text-decoration: none;
}

.workers-ai-chat-box a:hover {
text-decoration: underline;
}

.workers-ai-chat-box ul,
.workers-ai-chat-box ol {
margin: 5px 0 5px 20px;
}

.workers-ai-chat-box code {
background-color: #f5f5f5;
padding: 2px 4px;
border-radius: 3px;
font-family: monospace;
}

.workers-ai-chat-box pre {
background-color: #f5f5f5;
padding: 10px;
border-radius: 3px;
overflow-x: auto;
}

.workers-ai-chat-box img {
max-width: 100%;
height: auto;
border-radius: 3px;
}

/* Styles for the input form */
#workers-ai-chat-form {
display: flex;
flex-direction: column;
}

#workers-ai-user-input {
padding: 8px;
border: 1px solid #ccc;
border-radius: 3px;
margin-bottom: 10px;
font-size: 14px;
}

#workers-ai-submit-button {
padding: 8px 15px;
border: none;
background-color: #0073aa;
color: #fff;
border-radius: 3px;
cursor: pointer;
font-size: 14px;
}

#workers-ai-submit-button:hover {
background-color: #005177;
}

/* Styles for the "Thinking..." indicator */
.loading-indicator {
font-style: italic;
color: #555;
}
100 changes: 100 additions & 0 deletions js/workers-ai-chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
jQuery(document).ready(function($) {
// Function to convert Markdown to HTML using Marked.js
function convertMarkdownToHTML(markdownText) {
if (typeof marked !== 'undefined') {
// Configure Marked.js to sanitize generated HTML
marked.setOptions({
sanitize: true, // Sanitize HTML to prevent XSS
breaks: true, // Optional: convert line breaks to <br>
});
return marked.parse(markdownText);
} else {
// If Marked.js is not loaded, return the text unchanged
return $('<div>').text(markdownText).html();
}
}

// Handle form submission
$('#workers-ai-chat-form').on('submit', function(e) {
e.preventDefault();

var userInput = $('#workers-ai-user-input').val().trim();
if (userInput === '') {
alert(workersAIChatData.error_empty_message || 'Please enter a message.');
return;
}

sendMessage(userInput);
});

// Handle clicks on predefined questions
$(document).on('click', '.workers-ai-question-button', function() {
var question = $(this).text().trim();
if (question !== '') {
sendMessage(question);
}
});

// Function to send messages
function sendMessage(message) {
// If ephemeral chats are enabled, clear the chat history
if (workersAIChatData.ephemeral_chats === 'yes') {
$('#workers-ai-chat-box').empty();
}

// Display user's message
$('#workers-ai-chat-box').append('<div class="user-message"><strong>' + workersAIChatData.you_text + ':</strong> ' + $('<div>').text(message).html() + '</div>');

// Scroll to the bottom
$('#workers-ai-chat-box').scrollTop($('#workers-ai-chat-box')[0].scrollHeight);

// Clear the input field
$('#workers-ai-user-input').val('');

// Show "Thinking..." indicator
var loadingHTML = '<div class="ai-message"><strong>' + workersAIChatData.ai_name + ':</strong> <span class="loading-indicator">' + workersAIChatData.thinking_text + '</span></div>';
$('#workers-ai-chat-box').append(loadingHTML);

// Scroll to the bottom
$('#workers-ai-chat-box').scrollTop($('#workers-ai-chat-box')[0].scrollHeight);

// Disable the send button to prevent multiple submissions
$('#workers-ai-submit-button').prop('disabled', true);

// Send AJAX request
$.ajax({
type: 'POST',
url: workersAIChatData.ajax_url,
data: {
action: 'workers_ai_chat',
nonce: workersAIChatData.nonce,
user_input: message
},
success: function(response) {
if (response.success) {
// Convert Markdown to HTML
var aiResponseHTML = convertMarkdownToHTML(response.data);
// Replace loading indicator with AI response
$('#workers-ai-chat-box .ai-message:last').html('<strong>' + workersAIChatData.ai_name + ':</strong> ' + aiResponseHTML);
} else {
$('#workers-ai-chat-box .ai-message:last').html('<strong>' + workersAIChatData.ai_name + ':</strong> <span style="color: red;">' + $('<div>').text(response.data).html() + '</span>');
}

// Scroll to the bottom
$('#workers-ai-chat-box').scrollTop($('#workers-ai-chat-box')[0].scrollHeight);

// Enable the send button
$('#workers-ai-submit-button').prop('disabled', false);
},
error: function() {
$('#workers-ai-chat-box .ai-message:last').html('<strong>' + workersAIChatData.ai_name + ':</strong> <span style="color: red;">' + workersAIChatData.error_processing_request + '</span>');

// Scroll to the bottom
$('#workers-ai-chat-box').scrollTop($('#workers-ai-chat-box')[0].scrollHeight);

// Enable the send button
$('#workers-ai-submit-button').prop('disabled', false);
}
});
}
});
Binary file added languages/workers-ai-chat-es_ES.mo
Binary file not shown.
134 changes: 134 additions & 0 deletions languages/workers-ai-chat-es_ES.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# UserTemp <[email protected]>, 2024.
msgid ""
msgstr ""
"Project-Id-Version: Workers AI Chat\n"
"POT-Creation-Date: 2023-10-11 12:00+0000\n"
"PO-Revision-Date: 2024-11-16 08:14-0600\n"
"Last-Translator: UserTemp <[email protected]>\n"
"Language-Team: \n"
"Language: es_ES\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Virtaal 0.7.1\n"

#: workers-ai-chat.php:9
msgid "Uses Cloudflare Workers AI in a web chat embedded via shortcode."
msgstr "Usa Cloudflare Workers AI en un chat web incrustrándolo vía shortcode."

#: workers-ai-chat.php:23
msgid "Workers AI Chat Settings"
msgstr "Ajustes de Workers AI Chat"

#: workers-ai-chat.php:24
msgid "Workers AI Chat"
msgstr "Workers AI Chat"

#: workers-ai-chat.php:38
msgid "Workers AI Chat Settings"
msgstr "Ajustes de Workrkers AI Chat"

#: workers-ai-chat.php:67
msgid "API and Security Settings"
msgstr "Ajustes de API y Seguridad"

#: workers-ai-chat.php:94
msgid "Account ID"
msgstr "Account ID"

#: workers-ai-chat.php:101
msgid "API Token"
msgstr "API Token"

#: workers-ai-chat.php:108
msgid "API Endpoint"
msgstr "Endpoint de API"

#: workers-ai-chat.php:117
msgid "Prompting Type"
msgstr "Tipo de prompt"

#: workers-ai-chat.php:125
msgid "System Message (for Scoped Prompts)"
msgstr "Mensaje de sistema (para prompts Scoped)"

#: workers-ai-chat.php:135
msgid "AI Name"
msgstr "Nombre de IA"

#: workers-ai-chat.php:143
msgid "Predefined Questions"
msgstr "Preguntas por defecto"

#: workers-ai-chat.php:152
msgid "Ephemeral Chats"
msgstr "Chats efímeros"

#: workers-ai-chat.php:183
msgid ""
"Enter the necessary settings to use the Cloudflare Workers AI API, as well as "
"the AI name and predefined questions:"
msgstr ""
"Escriba los ajustes necesarios para usar la API de Cloudflare Workers AI, de "
"igual forma el nombre de la IA y preguntas por defecto:"

#: workers-ai-chat.php:223
msgid "Unscoped"
msgstr "Unscoped"

#: workers-ai-chat.php:224
msgid "Scoped"
msgstr "Scoped"

#: workers-ai-chat.php:229
msgid "Only used if \"Scoped\" is selected in Prompting Type."
msgstr "Solo usa si \"Scoped\" está seleccionado en tipo de prompt"

#: workers-ai-chat.php:236
msgid "This will be the name displayed in the AI responses."
msgstr "Esto aparecerá como nombre en las respuestas de la IA."

#: workers-ai-chat.php:243
msgid "Enter predefined questions, one per line."
msgstr "Ingrese preguntas por defecto, una por línea."

#: workers-ai-chat.php:250
msgid "Enable ephemeral chats (default)"
msgstr "Habilitar chats efímeros (defecto)"

#: workers-ai-chat.php:261
msgid "Type a message..."
msgstr "Escriba un mensaje..."

#: workers-ai-chat.php:261
msgid "Send"
msgstr "Enviar"

#: workers-ai-chat.php:283
msgid "Incomplete plugin settings."
msgstr "Configuraciones de plugin incompletas"

#: workers-ai-chat.php:307
msgid "No response received."
msgstr "Respuesta no recibida"

#: workers-ai-chat.php:309
msgid "Unknown error."
msgstr "Error desconocido."

#: js/workers-ai-chat.js:10
msgid "Please enter a message."
msgstr "Por favor ingrese un mensaje."

#: js/workers-ai-chat.js:30
msgid "You"
msgstr "Tu"

#: js/workers-ai-chat.js:60
msgid "Thinking..."
msgstr "Pensando..."

#: js/workers-ai-chat.js:86
msgid "Error processing the request."
msgstr "Ha ocurrido un error procesando su solicitud."
Binary file added languages/workers-ai-chat-es_MX.mo
Binary file not shown.
Loading

0 comments on commit c3cf7d6

Please sign in to comment.