MCPcopy Index your code
hub / github.com/simstudioai/sim / createKnowledgeBase

Function createKnowledgeBase

apps/sim/lib/knowledge/service.ts:153–225  ·  view source on GitHub ↗
(
  data: CreateKnowledgeBaseData,
  requestId: string
)

Source from the content-addressed store, hash-verified

151 * Create a new knowledge base
152 */
153export async function createKnowledgeBase(
154 data: CreateKnowledgeBaseData,
155 requestId: string
156): Promise<KnowledgeBaseWithCounts> {
157 const kbId = generateId()
158 const now = new Date()
159
160 const hasPermission = await getUserEntityPermissions(data.userId, 'workspace', data.workspaceId)
161 if (hasPermission !== 'admin' && hasPermission !== 'write') {
162 throw new KnowledgeBasePermissionError(
163 'User does not have permission to create knowledge bases in this workspace'
164 )
165 }
166
167 const newKnowledgeBase = {
168 id: kbId,
169 name: data.name,
170 description: data.description ?? null,
171 workspaceId: data.workspaceId,
172 userId: data.userId,
173 tokenCount: 0,
174 embeddingModel: data.embeddingModel,
175 embeddingDimension: data.embeddingDimension,
176 chunkingConfig: data.chunkingConfig,
177 createdAt: now,
178 updatedAt: now,
179 deletedAt: null,
180 }
181
182 const duplicate = await db
183 .select({ id: knowledgeBase.id })
184 .from(knowledgeBase)
185 .where(
186 and(
187 eq(knowledgeBase.workspaceId, data.workspaceId),
188 eq(knowledgeBase.name, data.name),
189 isNull(knowledgeBase.deletedAt)
190 )
191 )
192 .limit(1)
193
194 if (duplicate.length > 0) {
195 throw new KnowledgeBaseConflictError(data.name)
196 }
197
198 try {
199 await db.insert(knowledgeBase).values(newKnowledgeBase)
200 } catch (error: unknown) {
201 if (getPostgresErrorCode(error) === '23505') {
202 throw new KnowledgeBaseConflictError(data.name)
203 }
204 throw error
205 }
206
207 logger.info(`[${requestId}] Created knowledge base: ${data.name} (${kbId})`)
208
209 return {
210 id: kbId,

Callers 3

executeFunction · 0.90
route.tsFile · 0.90
route.tsFile · 0.90

Calls 5

generateIdFunction · 0.90
getUserEntityPermissionsFunction · 0.90
getPostgresErrorCodeFunction · 0.90
infoMethod · 0.80
eqFunction · 0.50

Tested by

no test coverage detected