Skip to content

Commit 2286ae7

Browse files
committed
fix(knowledge): resolve the v1 reader before updating and drop unscoped update totals
1 parent cde2256 commit 2286ae7

4 files changed

Lines changed: 19 additions & 47 deletions

File tree

‎apps/sim/app/api/v1/knowledge/[id]/route.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export const PUT = withRouteHandler(async (request: NextRequest, context: Knowle
8787
)
8888
if (result instanceof NextResponse) return result
8989

90+
/** Resolved before the write, so a reader that cannot be resolved changes nothing. */
91+
const access = await resolveV1KnowledgeReadAccess(userId, rateLimit, workspaceId)
9092
const outcome = await performUpdateKnowledgeBase({
9193
knowledgeBaseId: id,
9294
workspaceId,
@@ -103,10 +105,7 @@ export const PUT = withRouteHandler(async (request: NextRequest, context: Knowle
103105
)
104106
}
105107

106-
const knowledgeBase = await attachKnowledgeBaseConnectors(
107-
outcome.knowledgeBase,
108-
await resolveV1KnowledgeReadAccess(userId, rateLimit, workspaceId)
109-
)
108+
const knowledgeBase = await attachKnowledgeBaseConnectors(outcome.knowledgeBase, access)
110109
return NextResponse.json({
111110
success: true,
112111
data: {

‎apps/sim/app/workspace/[workspaceId]/lib/prefetch.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ describe('workspace list prefetches', () => {
208208
await prefetchKnowledgeBases(client, WORKSPACE_ID, USER_ID)
209209

210210
expect(mockListInternalKnowledgeBases).not.toHaveBeenCalled()
211-
expect(client.getQueryData(knowledgeKeys.list(WORKSPACE_ID, 'active'))).toBeUndefined()
211+
expect(client.getQueryData(knowledgeKeys.countedList(WORKSPACE_ID, 'active'))).toBeUndefined()
212212
})
213213
})
214214

‎apps/sim/lib/knowledge/orchestration/knowledge-bases.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { PlatformEvents } from '@/lib/core/telemetry'
44
import { generateRequestId } from '@/lib/core/utils/request'
55
import { DEFAULT_CHUNKING_CONFIG } from '@/lib/knowledge/constants'
66
import { getConfiguredKbEmbedding } from '@/lib/knowledge/embeddings'
7+
import type { ActiveKnowledgeBaseReference } from '@/lib/knowledge/knowledge-base-reference'
78
import {
89
auditActorFields,
910
classifyKnowledgeFailure,
@@ -25,6 +26,11 @@ export type PerformKnowledgeBaseResult = KnowledgeOrchestrationResult<{
2526
knowledgeBase: KnowledgeBaseWithCounts
2627
}>
2728

29+
/** The updated base without document totals, which a surface reads as its caller. */
30+
export type PerformUpdateKnowledgeBaseResult = KnowledgeOrchestrationResult<{
31+
knowledgeBase: ActiveKnowledgeBaseReference
32+
}>
33+
2834
export interface PerformCreateKnowledgeBaseParams extends KnowledgeOperationContext {
2935
workspaceId: string
3036
name: string
@@ -143,7 +149,7 @@ export interface PerformUpdateKnowledgeBaseParams extends KnowledgeOperationCont
143149
*/
144150
export async function performUpdateKnowledgeBase(
145151
params: PerformUpdateKnowledgeBaseParams
146-
): Promise<PerformKnowledgeBaseResult> {
152+
): Promise<PerformUpdateKnowledgeBaseResult> {
147153
const { knowledgeBaseId, updates, request, source } = params
148154
const requestId = params.requestId ?? generateRequestId()
149155

@@ -154,7 +160,7 @@ export async function performUpdateKnowledgeBase(
154160
return fail('No updates specified', 'validation')
155161
}
156162

157-
let updated: KnowledgeBaseWithCounts
163+
let updated: ActiveKnowledgeBaseReference
158164
try {
159165
updated = await updateKnowledgeBase(knowledgeBaseId, updates, requestId, {
160166
actorUserId: params.userId,

‎apps/sim/lib/knowledge/service.ts‎

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,8 @@ export async function createAuthorizedKnowledgeBase(
544544
}
545545

546546
/**
547-
* Update a knowledge base
547+
* Updates a knowledge base and returns it without document totals; a surface that shows them
548+
* reads them through {@link attachKnowledgeBaseConnectors} as its caller.
548549
*/
549550
export async function updateKnowledgeBase(
550551
knowledgeBaseId: string,
@@ -557,7 +558,7 @@ export async function updateKnowledgeBase(
557558
},
558559
requestId: string,
559560
options?: { actorUserId?: string; assertedWorkspaceId?: string }
560-
): Promise<KnowledgeBaseWithCounts> {
561+
): Promise<ActiveKnowledgeBaseReference> {
561562
if (updates.workspaceId !== undefined && !updates.workspaceId) {
562563
throw new OrchestrationError('validation', 'Workspace ID is required')
563564
}
@@ -890,35 +891,9 @@ export async function updateKnowledgeBase(
890891
}
891892
}
892893

893-
const updatedKb = await db
894-
.select({
895-
id: knowledgeBase.id,
896-
userId: knowledgeBase.userId,
897-
name: knowledgeBase.name,
898-
isSearchIndex: knowledgeBase.isSearchIndex,
899-
description: knowledgeBase.description,
900-
tokenCount: sql<number>`COALESCE(SUM(${document.tokenCount}), 0)`.mapWith(Number),
901-
embeddingModel: knowledgeBase.embeddingModel,
902-
embeddingDimension: knowledgeBase.embeddingDimension,
903-
chunkingConfig: knowledgeBase.chunkingConfig,
904-
createdAt: knowledgeBase.createdAt,
905-
updatedAt: knowledgeBase.updatedAt,
906-
deletedAt: knowledgeBase.deletedAt,
907-
workspaceId: knowledgeBase.workspaceId,
908-
organizationId: knowledgeBase.organizationId,
909-
folderId: knowledgeBase.folderId,
910-
docCount: count(document.knowledgeBaseId),
911-
})
894+
const [updated] = await db
895+
.select(ACTIVE_KNOWLEDGE_BASE_REFERENCE_FIELDS)
912896
.from(knowledgeBase)
913-
.leftJoin(
914-
document,
915-
and(
916-
eq(document.knowledgeBaseId, knowledgeBase.id),
917-
eq(document.userExcluded, false),
918-
isNull(document.archivedAt),
919-
isNull(document.deletedAt)
920-
)
921-
)
922897
.where(
923898
and(
924899
eq(knowledgeBase.id, knowledgeBaseId),
@@ -928,23 +903,15 @@ export async function updateKnowledgeBase(
928903
: undefined
929904
)
930905
)
931-
.groupBy(knowledgeBase.id)
932906
.limit(1)
933907

934-
if (updatedKb.length === 0) {
908+
if (!updated) {
935909
throw new KnowledgeBaseNotFoundError(knowledgeBaseId)
936910
}
937911

938912
logger.info(`[${requestId}] Updated knowledge base: ${knowledgeBaseId}`)
939913

940-
const [withConnectors] = await attachConnectorTypes([
941-
{
942-
...updatedKb[0],
943-
chunkingConfig: updatedKb[0].chunkingConfig as ChunkingConfig,
944-
docCount: Number(updatedKb[0].docCount),
945-
},
946-
])
947-
return withConnectors
914+
return toActiveKnowledgeBaseReference(updated)
948915
}
949916

950917
/**

0 commit comments

Comments
 (0)