mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-25 23:08:11 -05:00
Compare commits
2 Commits
staging
...
fix/kb-ws-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e94d74a541 | ||
|
|
974f3b9eff |
@@ -16,6 +16,10 @@ mockKnowledgeSchemas()
|
||||
mockDrizzleOrm()
|
||||
mockConsoleLogger()
|
||||
|
||||
vi.mock('@/lib/workspaces/permissions/utils', () => ({
|
||||
getUserEntityPermissions: vi.fn().mockResolvedValue({ role: 'owner' }),
|
||||
}))
|
||||
|
||||
describe('Knowledge Base API Route', () => {
|
||||
const mockAuth$ = mockAuth()
|
||||
|
||||
@@ -86,6 +90,7 @@ describe('Knowledge Base API Route', () => {
|
||||
const validKnowledgeBaseData = {
|
||||
name: 'Test Knowledge Base',
|
||||
description: 'Test description',
|
||||
workspaceId: 'test-workspace-id',
|
||||
chunkingConfig: {
|
||||
maxSize: 1024,
|
||||
minSize: 100,
|
||||
@@ -133,11 +138,25 @@ describe('Knowledge Base API Route', () => {
|
||||
expect(data.details).toBeDefined()
|
||||
})
|
||||
|
||||
it('should require workspaceId', async () => {
|
||||
mockAuth$.mockAuthenticatedUser()
|
||||
|
||||
const req = createMockRequest('POST', { name: 'Test KB' })
|
||||
const { POST } = await import('@/app/api/knowledge/route')
|
||||
const response = await POST(req)
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(400)
|
||||
expect(data.error).toBe('Invalid request data')
|
||||
expect(data.details).toBeDefined()
|
||||
})
|
||||
|
||||
it('should validate chunking config constraints', async () => {
|
||||
mockAuth$.mockAuthenticatedUser()
|
||||
|
||||
const invalidData = {
|
||||
name: 'Test KB',
|
||||
workspaceId: 'test-workspace-id',
|
||||
chunkingConfig: {
|
||||
maxSize: 100, // 100 tokens = 400 characters
|
||||
minSize: 500, // Invalid: minSize (500 chars) > maxSize (400 chars)
|
||||
@@ -157,7 +176,7 @@ describe('Knowledge Base API Route', () => {
|
||||
it('should use default values for optional fields', async () => {
|
||||
mockAuth$.mockAuthenticatedUser()
|
||||
|
||||
const minimalData = { name: 'Test KB' }
|
||||
const minimalData = { name: 'Test KB', workspaceId: 'test-workspace-id' }
|
||||
const req = createMockRequest('POST', minimalData)
|
||||
const { POST } = await import('@/app/api/knowledge/route')
|
||||
const response = await POST(req)
|
||||
|
||||
@@ -19,7 +19,7 @@ const logger = createLogger('KnowledgeBaseAPI')
|
||||
const CreateKnowledgeBaseSchema = z.object({
|
||||
name: z.string().min(1, 'Name is required'),
|
||||
description: z.string().optional(),
|
||||
workspaceId: z.string().optional(),
|
||||
workspaceId: z.string().min(1, 'Workspace ID is required'),
|
||||
embeddingModel: z.literal('text-embedding-3-small').default('text-embedding-3-small'),
|
||||
embeddingDimension: z.literal(1536).default(1536),
|
||||
chunkingConfig: z
|
||||
|
||||
@@ -37,6 +37,13 @@ export const knowledgeBaseServerTool: BaseServerTool<KnowledgeBaseArgs, Knowledg
|
||||
}
|
||||
}
|
||||
|
||||
if (!args.workspaceId) {
|
||||
return {
|
||||
success: false,
|
||||
message: 'Workspace ID is required for creating a knowledge base',
|
||||
}
|
||||
}
|
||||
|
||||
const requestId = crypto.randomUUID().slice(0, 8)
|
||||
const newKnowledgeBase = await createKnowledgeBase(
|
||||
{
|
||||
|
||||
@@ -79,7 +79,7 @@ export const KnowledgeBaseArgsSchema = z.object({
|
||||
name: z.string().optional(),
|
||||
/** Description of the knowledge base (optional for create) */
|
||||
description: z.string().optional(),
|
||||
/** Workspace ID to associate with (optional for create/list) */
|
||||
/** Workspace ID to associate with (required for create, optional for list) */
|
||||
workspaceId: z.string().optional(),
|
||||
/** Knowledge base ID (required for get, query) */
|
||||
knowledgeBaseId: z.string().optional(),
|
||||
|
||||
@@ -86,18 +86,16 @@ export async function createKnowledgeBase(
|
||||
const kbId = randomUUID()
|
||||
const now = new Date()
|
||||
|
||||
if (data.workspaceId) {
|
||||
const hasPermission = await getUserEntityPermissions(data.userId, 'workspace', data.workspaceId)
|
||||
if (hasPermission === null) {
|
||||
throw new Error('User does not have permission to create knowledge bases in this workspace')
|
||||
}
|
||||
const hasPermission = await getUserEntityPermissions(data.userId, 'workspace', data.workspaceId)
|
||||
if (hasPermission === null) {
|
||||
throw new Error('User does not have permission to create knowledge bases in this workspace')
|
||||
}
|
||||
|
||||
const newKnowledgeBase = {
|
||||
id: kbId,
|
||||
name: data.name,
|
||||
description: data.description ?? null,
|
||||
workspaceId: data.workspaceId ?? null,
|
||||
workspaceId: data.workspaceId,
|
||||
userId: data.userId,
|
||||
tokenCount: 0,
|
||||
embeddingModel: data.embeddingModel,
|
||||
@@ -122,7 +120,7 @@ export async function createKnowledgeBase(
|
||||
chunkingConfig: data.chunkingConfig,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
workspaceId: data.workspaceId ?? null,
|
||||
workspaceId: data.workspaceId,
|
||||
docCount: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ export interface KnowledgeBaseWithCounts {
|
||||
export interface CreateKnowledgeBaseData {
|
||||
name: string
|
||||
description?: string
|
||||
workspaceId?: string
|
||||
workspaceId: string
|
||||
embeddingModel: 'text-embedding-3-small'
|
||||
embeddingDimension: 1536
|
||||
chunkingConfig: ChunkingConfig
|
||||
|
||||
Reference in New Issue
Block a user