From 2e8fda72d2d12f79764efcafa6159a6a3acf842e Mon Sep 17 00:00:00 2001 From: davegilligan Date: Sun, 24 May 2026 00:24:57 +0200 Subject: [PATCH] fix: fetch doc content from client_documents when no chunks exist Documents saved via save-from-tool or case-upload store content directly in client_documents.content without being chunked into client_chunks. dbnToolsFetchDocChunks now falls back to client_documents.content for any requested doc_ids that returned no rows from client_chunks. Co-Authored-By: Claude Sonnet 4.6 --- includes/bootstrap.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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']);