fix(knowledge): rollback on delete failure, deduplicate sub-block IDs

- Add compensating rollback: if deleteDocument throws after create
  succeeds, clean up the new record to prevent orphaned pending docs
- Merge duplicate name/content sub-blocks into single entries with
  array conditions, matching the documentTags pattern

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Waleed Latif
2026-03-17 17:53:02 -07:00
parent e346f7761f
commit f6359ff633
2 changed files with 19 additions and 26 deletions

View File

@@ -144,16 +144,28 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
requestId
)
if (existingDocumentId) {
await deleteDocument(existingDocumentId, requestId)
}
const firstDocument = createdDocuments[0]
if (!firstDocument) {
logger.error(`[${requestId}] createDocumentRecords returned empty array unexpectedly`)
return NextResponse.json({ error: 'Failed to create document record' }, { status: 500 })
}
if (existingDocumentId) {
try {
await deleteDocument(existingDocumentId, requestId)
} catch (deleteError) {
logger.error(
`[${requestId}] Failed to delete old document ${existingDocumentId}, rolling back new record`,
deleteError
)
await deleteDocument(firstDocument.documentId, requestId).catch(() => {})
return NextResponse.json(
{ error: 'Failed to replace existing document' },
{ status: 500 }
)
}
}
processDocumentsWithQueue(
createdDocuments,
knowledgeBaseId,

View File

@@ -176,14 +176,14 @@ export const KnowledgeBlock: BlockConfig = {
condition: { field: 'operation', value: 'upload_chunk' },
},
// --- Create Document ---
// --- Create Document / Upsert Document ---
{
id: 'name',
title: 'Document Name',
type: 'short-input',
placeholder: 'Enter document name',
required: true,
condition: { field: 'operation', value: 'create_document' },
condition: { field: 'operation', value: ['create_document', 'upsert_document'] },
},
{
id: 'content',
@@ -192,7 +192,7 @@ export const KnowledgeBlock: BlockConfig = {
placeholder: 'Enter the document content',
rows: 6,
required: true,
condition: { field: 'operation', value: 'create_document' },
condition: { field: 'operation', value: ['create_document', 'upsert_document'] },
},
{
id: 'documentTags',
@@ -201,25 +201,6 @@ export const KnowledgeBlock: BlockConfig = {
dependsOn: ['knowledgeBaseSelector'],
condition: { field: 'operation', value: ['create_document', 'upsert_document'] },
},
// --- Upsert Document ---
{
id: 'name',
title: 'Document Name',
type: 'short-input',
placeholder: 'Enter document name',
required: true,
condition: { field: 'operation', value: 'upsert_document' },
},
{
id: 'content',
title: 'Document Content',
type: 'long-input',
placeholder: 'Enter the document content',
rows: 6,
required: true,
condition: { field: 'operation', value: 'upsert_document' },
},
{
id: 'upsertDocumentId',
title: 'Document ID (Optional)',