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 <noreply@anthropic.com>
This commit is contained in:
@@ -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']);
|
||||
|
||||
Reference in New Issue
Block a user