-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdocument_status.php
More file actions
executable file
·154 lines (137 loc) · 4.46 KB
/
Copy pathdocument_status.php
File metadata and controls
executable file
·154 lines (137 loc) · 4.46 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
require_once __DIR__ . '/bootstrap.php';
require_once __DIR__ . '/inc/rag_processing_status.php';
header('Content-Type: application/json');
if (!isset($_SESSION['user_data']['userid'])) {
error_log('document_status unauthorized access attempt');
http_response_code(401);
echo json_encode(['error' => 'Authentication required']);
exit;
}
$rawInput = file_get_contents('php://input');
$payload = json_decode($rawInput ?: '[]', true);
$documentIds = [];
if (is_array($payload) && isset($payload['document_ids']) && is_array($payload['document_ids'])) {
foreach ($payload['document_ids'] as $docId) {
$docId = (int)$docId;
if ($docId > 0) {
$documentIds[] = $docId;
}
}
}
$documentIds = array_values(array_unique($documentIds));
if (empty($documentIds)) {
error_log('document_status called without document_ids');
echo json_encode([
'documents' => [],
'all_ready' => true,
]);
exit;
}
$ragPaths = rag_workspace_paths($config ?? null);
$docStatusLogPath = ($ragPaths['logs'] ?? __DIR__ . '/logs') . '/document_status_debug.log';
$logDir = dirname($docStatusLogPath);
if (!is_dir($logDir)) {
@mkdir($logDir, 0775, true);
}
if (!function_exists('document_status_debug_log')) {
function document_status_debug_log(?string $path, array $entry): void
{
if (!$path) {
return;
}
$entry['ts'] = date('c');
$entry['pid'] = getmypid();
$payload = json_encode($entry, JSON_UNESCAPED_SLASHES);
if ($payload !== false) {
@file_put_contents($path, $payload . PHP_EOL, FILE_APPEND | LOCK_EX);
}
}
}
$userId = $_SESSION['user_data']['userid'];
$placeholders = implode(',', array_fill(0, count($documentIds), '?'));
try {
global $pdo;
$sql = "
SELECT
d.id AS document_id,
d.source AS document_source,
MAX(COALESCE(ri.ready, 0)) AS rag_ready
FROM document d
INNER JOIN chat c
ON d.chat_id = c.id
LEFT JOIN rag_index ri
ON ri.document_id = d.id
WHERE d.id IN ($placeholders)
AND c.user = ?
AND d.deleted = 0
GROUP BY d.id, d.source
";
$stmt = $pdo->prepare($sql);
$params = $documentIds;
$params[] = $userId;
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
document_status_debug_log($docStatusLogPath, [
'event' => 'query',
'user' => $userId,
'doc_count' => count($documentIds),
'doc_ids' => array_slice($documentIds, 0, 25),
]);
$statusMap = [];
foreach ($documentIds as $docId) {
$statusMap[$docId] = ['ready' => false];
}
foreach ($rows as $row) {
$docId = (int)($row['document_id'] ?? 0);
if (!$docId || !array_key_exists($docId, $statusMap)) {
continue;
}
$ready = ((int)($row['rag_ready'] ?? 0)) === 1;
$source = (string)($row['document_source'] ?? '');
if ($source === 'inline') {
$ready = true;
}
$statusMap[$docId]['ready'] = $ready;
}
$documents = [];
$allReady = true;
foreach ($statusMap as $docId => $meta) {
$ready = (bool)($meta['ready'] ?? false);
$processing = rag_processing_status_read($docId, $ragPaths);
if ($ready && $processing) {
rag_processing_status_clear($docId, $ragPaths);
$processing = null;
}
$documents[] = [
'document_id' => $docId,
'ready' => $ready,
'processing' => $processing,
];
if ($ready === false) {
$allReady = false;
}
}
echo json_encode([
'documents' => $documents,
'all_ready' => $allReady,
]);
document_status_debug_log($docStatusLogPath, [
'event' => 'response',
'user' => $userId,
'doc_count' => count($documents),
'all_ready' => $allReady,
'documents' => array_slice($documents, 0, 20),
]);
} catch (Throwable $e) {
error_log('document_status error: ' . $e->getMessage());
document_status_debug_log($docStatusLogPath, [
'event' => 'error',
'user' => $userId,
'doc_count' => count($documentIds),
'message' => $e->getMessage(),
]);
http_response_code(500);
echo json_encode(['error' => 'Unable to determine document status']);
}