diff --git a/includes/bootstrap.php b/includes/bootstrap.php index 1946daa..dffff74 100644 --- a/includes/bootstrap.php +++ b/includes/bootstrap.php @@ -1137,6 +1137,27 @@ function dbnToolsFetchDocChunks(array $docIds, int $clientId): string $byDoc[$id] ??= ['title' => (string)$row['doc_title'], 'chunks' => []]; $byDoc[$id]['chunks'][] = (string)$row['content']; } + + // Docs saved-from-tool or case-uploaded store content directly in client_documents + // with no rows in client_chunks — fall back for those. + $missingIds = array_values(array_diff($docIds, array_keys($byDoc))); + if (!empty($missingIds)) { + $mp = implode(',', array_fill(0, count($missingIds), '?')); + $fb = $db->prepare( + "SELECT id, title, content + FROM client_documents + WHERE client_id = ? AND id IN ($mp) + AND source_type != 'audio' + AND content IS NOT NULL AND content != '' + LIMIT 50" + ); + $fb->execute(array_merge([$clientId], $missingIds)); + foreach ($fb->fetchAll(PDO::FETCH_ASSOC) as $row) { + $id = (int)$row['id']; + $byDoc[$id] = ['title' => (string)$row['title'], 'chunks' => [(string)$row['content']]]; + } + } + $parts = []; foreach ($byDoc as $doc) { $parts[] = '=== ' . $doc['title'] . " ===\n" . implode("\n\n", $doc['chunks']);