|
1 | 1 | import { db } from '@sim/db' |
2 | | -import { knowledgeConnector } from '@sim/db/schema' |
| 2 | +import { knowledgeBaseTagDefinitions, knowledgeConnector } from '@sim/db/schema' |
3 | 3 | import { createLogger } from '@sim/logger' |
4 | 4 | import { and, desc, eq, isNull } from 'drizzle-orm' |
5 | 5 | import { type NextRequest, NextResponse } from 'next/server' |
6 | 6 | import { z } from 'zod' |
7 | 7 | import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid' |
8 | 8 | import { generateRequestId } from '@/lib/core/utils/request' |
9 | 9 | import { dispatchSync } from '@/lib/knowledge/connectors/sync-engine' |
10 | | -import { createTagDefinition, getNextAvailableSlot } from '@/lib/knowledge/tags/service' |
| 10 | +import { getSlotsForFieldType } from '@/lib/knowledge/constants' |
| 11 | +import { createTagDefinition } from '@/lib/knowledge/tags/service' |
11 | 12 | import { getCredential } from '@/app/api/auth/oauth/utils' |
12 | 13 | import { checkKnowledgeBaseAccess, checkKnowledgeBaseWriteAccess } from '@/app/api/knowledge/utils' |
13 | 14 | import { CONNECTOR_REGISTRY } from '@/connectors/registry' |
@@ -123,15 +124,25 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{ |
123 | 124 | const disabledIds = new Set((sourceConfig.disabledTagIds as string[] | undefined) ?? []) |
124 | 125 | const enabledDefs = connectorConfig.tagDefinitions.filter((td) => !disabledIds.has(td.id)) |
125 | 126 |
|
| 127 | + const existingDefs = await db |
| 128 | + .select({ tagSlot: knowledgeBaseTagDefinitions.tagSlot }) |
| 129 | + .from(knowledgeBaseTagDefinitions) |
| 130 | + .where(eq(knowledgeBaseTagDefinitions.knowledgeBaseId, knowledgeBaseId)) |
| 131 | + |
| 132 | + const usedSlots = new Set<string>(existingDefs.map((d) => d.tagSlot)) |
| 133 | + |
126 | 134 | const skippedTags: string[] = [] |
127 | 135 | for (const td of enabledDefs) { |
128 | | - const slot = await getNextAvailableSlot(knowledgeBaseId, td.fieldType) |
129 | | - if (!slot) { |
| 136 | + const slots = getSlotsForFieldType(td.fieldType) |
| 137 | + const available = slots.find((s) => !usedSlots.has(s)) |
| 138 | + |
| 139 | + if (!available) { |
130 | 140 | skippedTags.push(td.displayName) |
131 | 141 | logger.warn(`[${requestId}] No available ${td.fieldType} slots for "${td.displayName}"`) |
132 | 142 | continue |
133 | 143 | } |
134 | | - tagSlotMapping[td.id] = slot |
| 144 | + usedSlots.add(available) |
| 145 | + tagSlotMapping[td.id] = available |
135 | 146 | } |
136 | 147 |
|
137 | 148 | if (skippedTags.length > 0 && Object.keys(tagSlotMapping).length === 0) { |
|
0 commit comments