You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
4.7 KiB
176 lines
4.7 KiB
<?php
|
|
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/default.php';
|
|
require_once __DIR__ . '/auth.php';
|
|
|
|
requireAuthentication();
|
|
|
|
$username = getAuthenticatedUsername();
|
|
|
|
function createTitle($messages) {
|
|
foreach ($messages as $msg) {
|
|
if (($msg['role'] ?? '') === 'user') {
|
|
$title = trim($msg['content']);
|
|
if (mb_strlen($title) > 60) {
|
|
$title = mb_substr($title, 0, 60) . '...';
|
|
}
|
|
return $title;
|
|
}
|
|
}
|
|
return 'Untitled Chat';
|
|
}
|
|
|
|
$action = $_GET['action'] ?? '';
|
|
switch ($action) {
|
|
case 'load-config':
|
|
$config = getUserConfig($username);
|
|
echo json_encode([
|
|
'defaultModel' => (string) ($config['defaultModel'] ?? '')
|
|
]);
|
|
exit;
|
|
|
|
case 'save-config':
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
echo json_encode(['error' => 'POST required.']);
|
|
exit;
|
|
}
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
if (!is_array($input)) {
|
|
echo json_encode(['error' => 'Invalid JSON.']);
|
|
exit;
|
|
}
|
|
|
|
$config = getUserConfig($username);
|
|
$config['defaultModel'] = isset($input['defaultModel']) ? (string) $input['defaultModel'] : '';
|
|
|
|
if (!saveUserConfigData($username, $config)) {
|
|
echo json_encode(['error' => 'Unable to save config.']);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode(['success' => true, 'defaultModel' => $config['defaultModel']]);
|
|
exit;
|
|
|
|
case 'list':
|
|
$chats = readUserChat($username);
|
|
$list = [];
|
|
foreach ($chats as $chat) {
|
|
$list[] = [
|
|
'id' => $chat['id'] ?? '',
|
|
'title' => $chat['title'] ?? 'Untitled Chat',
|
|
'created' => $chat['created'] ?? '',
|
|
'model' => $chat['model'] ?? ''
|
|
];
|
|
}
|
|
|
|
usort($list, function ($a, $b) {
|
|
return strcmp($b['created'], $a['created']);
|
|
});
|
|
|
|
echo json_encode($list);
|
|
exit;
|
|
|
|
case 'load':
|
|
$id = $_GET['id'] ?? '';
|
|
if ($id === '') {
|
|
echo json_encode(['error' => 'Missing id.']);
|
|
exit;
|
|
}
|
|
|
|
$chats = readUserChat($username);
|
|
foreach ($chats as $chat) {
|
|
if (($chat['id'] ?? '') === $id) {
|
|
echo json_encode($chat);
|
|
exit;
|
|
}
|
|
}
|
|
echo json_encode(['error' => 'Chat not found.']);
|
|
exit;
|
|
|
|
case 'save':
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
echo json_encode(['error' => 'POST required.']);
|
|
exit;
|
|
}
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
if (!is_array($input)) {
|
|
echo json_encode(['error' => 'Invalid JSON.']);
|
|
exit;
|
|
}
|
|
|
|
$messages = $input['messages'] ?? [];
|
|
if (!is_array($messages) || count($messages) === 0) {
|
|
echo json_encode(['error' => 'No messages.']);
|
|
exit;
|
|
}
|
|
|
|
$id = $input['id'] ?? '';
|
|
if ($id === '') {
|
|
$id = uniqid('', true);
|
|
}
|
|
|
|
$title = createTitle($messages);
|
|
$model = isset($input['model']) ? (string) $input['model'] : '';
|
|
$chat = [
|
|
'id' => $id,
|
|
'title' => $title,
|
|
'created' => date('Y-m-d H:i:s'),
|
|
'model' => $model,
|
|
'messages' => $messages
|
|
];
|
|
|
|
$chats = readUserChat($username);
|
|
$found = false;
|
|
foreach ($chats as &$existing) {
|
|
if (($existing['id'] ?? '') === $id) {
|
|
$existing = $chat;
|
|
$found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$found) {
|
|
$chats[] = $chat;
|
|
}
|
|
|
|
$config = getUserConfig($username);
|
|
$config['defaultModel'] = $model;
|
|
if (!saveUserConfigData($username, $config) || !writeUserChat($username, $chats)) {
|
|
echo json_encode(['error' => 'Unable to save.']);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'id' => $id,
|
|
'title' => $title
|
|
]);
|
|
exit;
|
|
|
|
case 'delete':
|
|
$id = $_GET['id'] ?? '';
|
|
if ($id === '') {
|
|
echo json_encode(['error' => 'Missing id.']);
|
|
exit;
|
|
}
|
|
|
|
$chats = readUserChat($username);
|
|
$newChats = [];
|
|
foreach ($chats as $chat) {
|
|
if (($chat['id'] ?? '') !== $id) {
|
|
$newChats[] = $chat;
|
|
}
|
|
}
|
|
|
|
writeUserChat($username, $newChats);
|
|
echo json_encode(['success' => true]);
|
|
exit;
|
|
|
|
default:
|
|
echo json_encode(['error' => 'Unknown action.']);
|
|
exit;
|
|
}
|