-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget_libraries.php
More file actions
53 lines (46 loc) · 1.69 KB
/
get_libraries.php
File metadata and controls
53 lines (46 loc) · 1.69 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
<?php
declare(strict_types=1);
require_once __DIR__ . '/bootstrap.php';
header('Content-Type: application/json');
if (empty($_SESSION['user_data']['userid']) || empty($_SESSION['authorized'])) {
http_response_code(401);
echo json_encode(['error' => 'Authentication required']);
exit;
}
$chatId = filter_input(INPUT_GET, 'chat_id', FILTER_SANITIZE_STRING);
$selectedMap = [];
if (!empty($chatId)) {
$chatLibraries = get_chat_libraries($_SESSION['user_data']['userid'], $chatId);
foreach ($chatLibraries as $lib) {
$libId = (int)($lib['library_id'] ?? 0);
if ($libId > 0) {
$selectedMap[$libId] = [
'selected' => true,
'enabled' => !(isset($lib['enabled']) && !$lib['enabled']),
];
}
}
}
$libraries = get_available_libraries();
$output = [];
foreach ($libraries as $library) {
$libId = (int)($library['id'] ?? 0);
if ($libId <= 0) {
continue;
}
$isSelected = isset($selectedMap[$libId]);
$output[] = [
'id' => $libId,
'name' => (string)($library['name'] ?? ('Library ' . $libId)),
'description' => (string)($library['description'] ?? ''),
'type' => 'library',
'prompt' => (string)($library['prompt'] ?? ''),
'source' => (string)($library['source'] ?? 'library'),
'ready' => (int)($library['ready'] ?? 0),
'token_length' => (int)($library['library_token_length'] ?? 0),
'full_text_available' => (int)($library['full_text_available'] ?? 0),
'selected' => $isSelected,
'enabled_in_chat' => $isSelected ? (bool)$selectedMap[$libId]['enabled'] : false,
];
}
echo json_encode(['libraries' => $output]);