-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlibrary_excerpt.php
More file actions
134 lines (119 loc) · 3.71 KB
/
Copy pathlibrary_excerpt.php
File metadata and controls
134 lines (119 loc) · 3.71 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?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([
'ok' => false,
'error' => 'unauthorized',
'message' => 'Session expired. Please refresh the page.',
]);
exit;
}
$libraryId = filter_input(INPUT_GET, 'library_id', FILTER_VALIDATE_INT);
$chatId = filter_input(INPUT_GET, 'chat_id', FILTER_SANITIZE_STRING);
if (!$libraryId || $libraryId < 1) {
http_response_code(400);
echo json_encode([
'ok' => false,
'error' => 'invalid_library_id',
'message' => 'A valid library id is required.',
]);
exit;
}
$userId = $_SESSION['user_data']['userid'];
try {
global $pdo;
$sql = "
SELECT
l.id,
l.name,
l.prompt,
l.content,
l.source,
l.library_token_length,
l.full_text_available,
l.ready,
l.deleted,
lc.chat_id
FROM library l
LEFT JOIN library_chat lc
ON lc.library_id = l.id
AND lc.chat_id = :chat_id
LEFT JOIN chat c
ON c.id = lc.chat_id
AND c.user = :user
WHERE l.id = :library_id
LIMIT 1
";
$stmt = $pdo->prepare($sql);
$stmt->execute([
'library_id' => $libraryId,
'chat_id' => (string)$chatId,
'user' => $userId,
]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row || (int)$row['deleted'] === 1) {
http_response_code(404);
echo json_encode([
'ok' => false,
'error' => 'not_found',
'message' => 'Library not found.',
]);
exit;
}
if (!empty($chatId) && (string)($row['chat_id'] ?? '') !== (string)$chatId) {
http_response_code(403);
echo json_encode([
'ok' => false,
'error' => 'forbidden',
'message' => 'You do not have access to this library in this chat.',
]);
exit;
}
$rawContent = (string)($row['content'] ?? '');
$maxPreviewChars = 8000;
$excerpt = $rawContent;
$truncated = false;
if (function_exists('mb_strlen')) {
if (mb_strlen($excerpt, 'UTF-8') > $maxPreviewChars) {
$excerpt = mb_substr($excerpt, 0, $maxPreviewChars, 'UTF-8');
$truncated = true;
}
} else {
if (strlen($excerpt) > $maxPreviewChars) {
$excerpt = substr($excerpt, 0, $maxPreviewChars);
$truncated = true;
}
}
if ($excerpt === '') {
$excerpt = 'No preview text is available for this library yet.';
}
echo json_encode([
'ok' => true,
'document' => [
'id' => (int)$row['id'],
'chat_id' => (string)$chatId,
'name' => (string)$row['name'],
'type' => 'library',
'source' => 'library',
'prompt' => (string)($row['prompt'] ?? ''),
'token_length' => (int)($row['library_token_length'] ?? 0),
'full_text_available' => (bool)($row['full_text_available'] ?? 0),
'ready' => ((int)($row['ready'] ?? 0) === 1),
'has_preview' => $rawContent !== '',
'excerpt_truncated' => $truncated,
'generated_at' => date('c'),
'excerpt' => $excerpt,
],
]);
} catch (Throwable $e) {
error_log('library_excerpt error: ' . $e->getMessage());
http_response_code(500);
echo json_encode([
'ok' => false,
'error' => 'server_error',
'message' => 'Unable to load the requested library excerpt.',
]);
}