Skip to content

Commit f6359ff

Browse files
waleedlatif1claude
andcommitted
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>
1 parent e346f77 commit f6359ff

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

apps/sim/app/api/knowledge/[id]/documents/upsert/route.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,28 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
144144
requestId
145145
)
146146

147-
if (existingDocumentId) {
148-
await deleteDocument(existingDocumentId, requestId)
149-
}
150-
151147
const firstDocument = createdDocuments[0]
152148
if (!firstDocument) {
153149
logger.error(`[${requestId}] createDocumentRecords returned empty array unexpectedly`)
154150
return NextResponse.json({ error: 'Failed to create document record' }, { status: 500 })
155151
}
156152

153+
if (existingDocumentId) {
154+
try {
155+
await deleteDocument(existingDocumentId, requestId)
156+
} catch (deleteError) {
157+
logger.error(
158+
`[${requestId}] Failed to delete old document ${existingDocumentId}, rolling back new record`,
159+
deleteError
160+
)
161+
await deleteDocument(firstDocument.documentId, requestId).catch(() => {})
162+
return NextResponse.json(
163+
{ error: 'Failed to replace existing document' },
164+
{ status: 500 }
165+
)
166+
}
167+
}
168+
157169
processDocumentsWithQueue(
158170
createdDocuments,
159171
knowledgeBaseId,

apps/sim/blocks/blocks/knowledge.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,14 @@ export const KnowledgeBlock: BlockConfig = {
176176
condition: { field: 'operation', value: 'upload_chunk' },
177177
},
178178

179-
// --- Create Document ---
179+
// --- Create Document / Upsert Document ---
180180
{
181181
id: 'name',
182182
title: 'Document Name',
183183
type: 'short-input',
184184
placeholder: 'Enter document name',
185185
required: true,
186-
condition: { field: 'operation', value: 'create_document' },
186+
condition: { field: 'operation', value: ['create_document', 'upsert_document'] },
187187
},
188188
{
189189
id: 'content',
@@ -192,7 +192,7 @@ export const KnowledgeBlock: BlockConfig = {
192192
placeholder: 'Enter the document content',
193193
rows: 6,
194194
required: true,
195-
condition: { field: 'operation', value: 'create_document' },
195+
condition: { field: 'operation', value: ['create_document', 'upsert_document'] },
196196
},
197197
{
198198
id: 'documentTags',
@@ -201,25 +201,6 @@ export const KnowledgeBlock: BlockConfig = {
201201
dependsOn: ['knowledgeBaseSelector'],
202202
condition: { field: 'operation', value: ['create_document', 'upsert_document'] },
203203
},
204-
205-
// --- Upsert Document ---
206-
{
207-
id: 'name',
208-
title: 'Document Name',
209-
type: 'short-input',
210-
placeholder: 'Enter document name',
211-
required: true,
212-
condition: { field: 'operation', value: 'upsert_document' },
213-
},
214-
{
215-
id: 'content',
216-
title: 'Document Content',
217-
type: 'long-input',
218-
placeholder: 'Enter the document content',
219-
rows: 6,
220-
required: true,
221-
condition: { field: 'operation', value: 'upsert_document' },
222-
},
223204
{
224205
id: 'upsertDocumentId',
225206
title: 'Document ID (Optional)',

0 commit comments

Comments
 (0)