forked from HAWK-Digital-Environments/HAWKI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from hm-edu/update
Update
- Loading branch information
Showing
27 changed files
with
1,042 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
session_start(); | ||
if (!isset($_SESSION['username'])) { | ||
http_response_code(401); | ||
exit; | ||
} | ||
|
||
header('Content-Type: application/json'); | ||
header('Cache-Control: no-cache'); | ||
header('Connection: keep-alive'); | ||
header('Access-Control-Allow-Origin: *'); | ||
header('Access-Control-Allow-Headers: Content-Type'); | ||
header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); | ||
|
||
// Add this block to handle preflight requests | ||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { | ||
http_response_code(204); | ||
exit; | ||
} | ||
|
||
$_SESSION['last_activity'] = time(); | ||
|
||
// API configuration | ||
$apiUrl = isset($env) ? $env['GWDG_API_URL'] : getenv('OPENAI_API_URL'); | ||
$apiKey = isset($env) ? $env['GWDG_API_KEY'] : getenv('OPENAI_API_KEY'); | ||
|
||
$requestPayload = file_get_contents('php://input'); | ||
// Decode the JSON payload into an associative array | ||
$decodedPayload = json_decode($requestPayload, true); | ||
|
||
// Add temperature and max_tokens keys | ||
$decodedPayload['temperature'] = 0; | ||
$decodedPayload['max_tokens'] = 1000; | ||
|
||
// Encode the modified array back to JSON | ||
$requestPayload = json_encode($decodedPayload); | ||
|
||
|
||
$ch = curl_init(); | ||
curl_setopt($ch, CURLOPT_URL, $apiUrl); | ||
curl_setopt($ch, CURLOPT_POST, 1); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestPayload); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | ||
'Authorization: Bearer ' . $apiKey, | ||
'Content-Type: application/json', | ||
'Accept: application/json' | ||
]); | ||
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) { | ||
echo $data; | ||
// ob_flush(); | ||
flush(); | ||
return strlen($data); | ||
}); | ||
|
||
curl_exec($ch); | ||
|
||
if (curl_errno($ch)) { | ||
echo 'Error:' . curl_error($ch); | ||
} | ||
|
||
curl_close($ch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
$encryptionSalt = isset($env) ? $env['CHATLOG_ENCRYPTION_SALT'] : getenv('CHATLOG_ENCRYPTION_SALT'); | ||
$userSpecificSalt = $encryptionSalt . $_SESSION['username']; | ||
|
||
?> | ||
|
||
<script> | ||
|
||
function saveMessagesToLocalStorage() { | ||
const username = '<?= htmlspecialchars($_SESSION['username']) ?>'; // Use the PHP variable | ||
const messagesElement = document.querySelector(".messages"); | ||
const messageElements = messagesElement.querySelectorAll(".message"); | ||
|
||
let archiveObject = { messages: [] }; | ||
|
||
messageElements.forEach(messageElement => { | ||
let messageObject = {}; | ||
messageObject.role = messageElement.dataset.role; | ||
|
||
if(messageElement.dataset.role === 'assistant'){ | ||
messageObject.content = messageElement.querySelector(".message-text").getAttribute('rawContent'); | ||
}else{ | ||
messageObject.content = messageElement.querySelector(".message-text").textContent; | ||
} | ||
|
||
archiveObject.messages.push(messageObject); | ||
}); | ||
|
||
// Convert messages to string | ||
const messageString = JSON.stringify(archiveObject.messages); | ||
const compressedMessages = LZString.compressToUTF16(messageString); | ||
|
||
const salt = '<?= htmlspecialchars($userSpecificSalt) ?>'; | ||
|
||
// Derive a key from the username | ||
const key = CryptoJS.PBKDF2(username, CryptoJS.enc.Hex.parse(salt), { | ||
keySize: 256 / 32, | ||
iterations: 1000 | ||
}); | ||
|
||
// Encrypt the messages | ||
const encrypted = CryptoJS.AES.encrypt(compressedMessages, key.toString()); | ||
const storageDate = Date.now(); | ||
|
||
const storagePackage = JSON.stringify({ | ||
encryptedData: encrypted.toString(), | ||
storageDate: storageDate, | ||
}) | ||
|
||
// Save encrypted data to local storage | ||
localStorage.setItem('chatLog_' + username, storagePackage); | ||
} | ||
|
||
|
||
|
||
function loadMessagesFromLocalStorage() { | ||
|
||
const username = '<?= htmlspecialchars($_SESSION['username']) ?>'; // Use the PHP variable | ||
const storedData = localStorage.getItem('chatLog_' + username); | ||
if(storedData === null){ | ||
return; | ||
} | ||
const parsedData = JSON.parse(storedData); | ||
const encryptedData = parsedData.encryptedData; | ||
const salt = '<?= htmlspecialchars($userSpecificSalt) ?>'; | ||
|
||
if (encryptedData) { | ||
try { | ||
// Derive the key from the username | ||
const key = CryptoJS.PBKDF2(username, CryptoJS.enc.Hex.parse(salt), { | ||
keySize: 256 / 32, | ||
iterations: 1000 | ||
}); | ||
|
||
// Decrypt the messages | ||
const decrypted = CryptoJS.AES.decrypt(encryptedData, key.toString()); | ||
const decryptedString = decrypted.toString(CryptoJS.enc.Utf8); | ||
// Decompress the messages | ||
const decompressedString = LZString.decompressFromUTF16(decryptedString) | ||
const messages = JSON.parse(decompressedString); | ||
|
||
if(messages != null){ | ||
document.querySelector('.limitations')?.remove(); | ||
} | ||
|
||
messages.forEach(message => { | ||
|
||
const messagesElement = document.querySelector(".messages"); | ||
const messageTemplate = document.querySelector('#message'); | ||
const messageElement = messageTemplate.content.cloneNode(true); | ||
|
||
messageElement.querySelector(".message").dataset.role = message.role; | ||
|
||
if(message.role == "assistant"){ | ||
messageElement.querySelector(".message-icon").textContent = "AI"; | ||
messageElement.querySelector(".message-text").setAttribute('rawContent', message.content); | ||
//FORMAT RAW TEXT AGAIN | ||
const formattedContent = FormatWholeMessage(message.content); | ||
messageElement.querySelector(".message-text").innerHTML = formattedContent; | ||
|
||
} else{ | ||
messageElement.querySelector(".message-text").innerHTML = message.content; | ||
messageElement.querySelector(".message-icon").textContent = '<?= htmlspecialchars($_SESSION['username']) ?>'; | ||
messageElement.querySelector(".message").classList.add("me"); | ||
} | ||
|
||
messagesElement.appendChild(messageElement); | ||
hljs.highlightAll(); | ||
FormatMathFormulas(); | ||
scrollToLast(true); | ||
|
||
}); | ||
} catch (error) { | ||
console.error("Failed to decrypt or parse messages:", error); | ||
} | ||
} | ||
} | ||
|
||
|
||
function cleanupStoredLogs(){ | ||
const items = {}; | ||
for (let i = 0; i < localStorage.length; i++) { | ||
const key = localStorage.key(i); | ||
if (key.startsWith("chatLog_")) { | ||
const storedData = localStorage.getItem(localStorage.key(i)); | ||
const parsedData = JSON.parse(storedData); | ||
|
||
// check if the stored data is older than one week | ||
if(Date.now() > parsedData.storageDate + 7 * 24 * 60 * 60 * 1000){ | ||
localStorage.removeItem(localStorage.key(i)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
function deleteChatLog(){ | ||
const username = '<?= htmlspecialchars($_SESSION['username']) ?>'; // Use the PHP variable | ||
localStorage.removeItem('chatLog_' + username); | ||
const chatBtn = document.querySelector("#chatMenuButton"); | ||
load(chatBtn ,'chat.php'); | ||
} | ||
function openDeletePanel(){ | ||
document.getElementById('delete-chat-confirm').style.display = "flex"; | ||
} | ||
function cancelDelete(){ | ||
document.getElementById('delete-chat-confirm').style.display = "none"; | ||
} | ||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.