Skip to content

Commit a657493

Browse files
authored
fix(knowledge): serve list totals when a request omits the count flag (#8367)
* fix(knowledge): serve list totals when a request omits the count flag * test(knowledge): cover the internal list request a pre-flag page sends
1 parent 8f3eb3a commit a657493

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

‎apps/sim/app/api/v1/knowledge/route.integration.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
/**
22
* Knowledge-base document totals against real PostgreSQL: the public v1 list and detail count
33
* only the documents their caller can read, and the internal list reads no document at all
4-
* unless the caller asks for totals.
4+
* unless the caller asks for totals. A request without the flag is counted, since a page loaded
5+
* before the flag existed requires both totals on every row.
56
*/
67
import type { Principal } from '@sim/auth/principal'
78
import { db } from '@sim/db'
89
import { document, organization, user, workspace } from '@sim/db/schema'
9-
import { createMockRequest } from '@sim/testing'
10+
import { authMock, authMockFns, createMockRequest } from '@sim/testing'
1011
import { generateId } from '@sim/utils/id'
1112
import { eq, inArray } from 'drizzle-orm'
1213
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
1314

1415
const caller = vi.hoisted(() => ({ userId: '' }))
1516

17+
vi.mock('@/lib/auth', () => authMock)
18+
1619
vi.mock('@/app/api/v1/middleware', async (importOriginal) => ({
1720
...(await importOriginal<typeof import('@/app/api/v1/middleware')>()),
1821
authenticateRequest: async () => ({
@@ -34,6 +37,7 @@ import {
3437
seedKnowledgeAclFixture,
3538
} from '@/lib/knowledge/__integration__/seed-source-access-fixture'
3639
import { listInternalKnowledgeBases } from '@/lib/knowledge/application/knowledge-bases'
40+
import { GET as listInternalKnowledgeBasesRoute } from '@/app/api/knowledge/route'
3741
import { GET as getKnowledgeBase } from '@/app/api/v1/knowledge/[id]/route'
3842
import { GET as listKnowledgeBases } from '@/app/api/v1/knowledge/route'
3943

@@ -46,6 +50,10 @@ describe('knowledge-base document totals in PostgreSQL', () => {
4650
throw new Error('Unexpected provider request in knowledge-base count tests')
4751
})
4852
caller.userId = ids.bobId
53+
authMockFns.mockGetSession.mockResolvedValue({
54+
user: { id: ids.bobId },
55+
session: { id: 'fixture-reader' },
56+
})
4957
await seedKnowledgeAclFixture(ids, { connectorType: 'google_drive' })
5058
await db.insert(document).values([
5159
{
@@ -134,4 +142,21 @@ describe('knowledge-base document totals in PostgreSQL', () => {
134142
).knowledgeBases
135143
expect(counted).toMatchObject({ docCount: 1, tokenCount: 10 })
136144
})
145+
146+
it('counts an internal list request that omits the flag', async () => {
147+
const list = async (query: string) => {
148+
const response = await listInternalKnowledgeBasesRoute(
149+
createMockRequest('GET', undefined, {}, `http://localhost/api/knowledge?${query}`),
150+
{ params: Promise.resolve({}) }
151+
)
152+
expect(response.status).toBe(200)
153+
return (await response.json()).data
154+
}
155+
const workspaceQuery = `workspaceId=${ids.workspaceId}&scope=active`
156+
expect(await list(workspaceQuery)).toEqual([
157+
expect.objectContaining({ id: ids.knowledgeBaseId, docCount: 1, tokenCount: 10 }),
158+
])
159+
const [uncounted] = await list(`${workspaceQuery}&includeCounts=false`)
160+
expect(uncounted).not.toHaveProperty('tokenCount')
161+
})
137162
})

‎apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/copilot/components/user-input/hooks/use-mention-data.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export function useMentionData(props: UseMentionDataProps): MentionDataReturn {
250250
try {
251251
setIsLoadingKnowledge(true)
252252
const result = await requestJson(listKnowledgeBasesContract, {
253-
query: { workspaceId },
253+
query: { workspaceId, includeCounts: false },
254254
})
255255
const items = result.data
256256
const sorted = [...items].sort((a, b) => {

‎apps/sim/lib/api/contracts/knowledge/base.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ export type KnowledgeScope = z.output<typeof knowledgeScopeSchema>
2626
export const listKnowledgeBasesQuerySchema = z.object({
2727
workspaceId: z.string().min(1).optional(),
2828
scope: knowledgeScopeSchema.default('active'),
29-
/** Adds `docCount` and `tokenCount` for the documents the caller can see; costs a document scan. */
30-
includeCounts: booleanQueryFlagSchema.optional().default(false),
29+
/**
30+
* Adds `docCount` and `tokenCount` for the documents the caller can see; costs a document scan.
31+
* Absent means counted: a page loaded before this flag existed requires both totals on every
32+
* row, and current clients always send it.
33+
*/
34+
includeCounts: booleanQueryFlagSchema.optional().default(true),
3135
})
3236

3337
/**

0 commit comments

Comments
 (0)