Skip to content

Commit f138017

Browse files
committed
improvement(knowledge): one scoped search shape, page and pool cleanups, admission ahead of the embedding beside the scope reads
1 parent 29d4ac8 commit f138017

8 files changed

Lines changed: 690 additions & 718 deletions

File tree

‎apps/sim/lib/billing/core/billing-attribution.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -742,13 +742,16 @@ export async function resolveBillingAttribution({
742742

743743
/** The organization payer is independent of the person making the request. */
744744
export async function resolveOrganizationBillingPayer(organizationId: string) {
745-
const [owner] = await db
746-
.select({ userId: member.userId })
747-
.from(member)
748-
.where(and(eq(member.organizationId, organizationId), eq(member.role, 'owner')))
749-
.limit(1)
745+
/** The owner and the subscription are independent reads; neither waits on the other. */
746+
const [[owner], payerSubscription] = await Promise.all([
747+
db
748+
.select({ userId: member.userId })
749+
.from(member)
750+
.where(and(eq(member.organizationId, organizationId), eq(member.role, 'owner')))
751+
.limit(1),
752+
getOrganizationSubscription(organizationId, { onError: 'throw' }),
753+
])
750754
if (!owner) throw new Error('Organization billing owner is unavailable')
751-
const payerSubscription = await getOrganizationSubscription(organizationId, { onError: 'throw' })
752755
if (payerSubscription && payerSubscription.referenceId !== organizationId)
753756
throw new Error('Organization subscription belongs to a different payer')
754757
return { organizationId, billedAccountUserId: owner.userId, payerSubscription }

‎apps/sim/lib/knowledge/access/availability.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ export async function resolveKnowledgeAccessAvailability(
6464
throw new Error('Knowledge access requires one resource owner')
6565
/** A caller that brings its own billing snapshot is answered from that snapshot, uncached. */
6666
if (context.ownerBilling) return readKnowledgeAccessAvailability(context)
67-
const key = `${context.organizationId ?? ''}|${context.workspaceId ?? ''}|${context.userId ?? ''}`
67+
/** An organization's answer depends on the organization alone; a workspace's on its viewer too. */
68+
const key = context.organizationId
69+
? `${context.organizationId}||`
70+
: `|${context.workspaceId ?? ''}|${context.userId ?? ''}`
6871
const availability = await availabilityCache.fetch(key, { context })
6972
if (!availability) throw new Error('Knowledge access availability could not be resolved')
7073
return availability
@@ -89,14 +92,14 @@ async function readKnowledgeAccessAvailability(
8992
return { sourceMirrored: false, memberScoped: false }
9093
}
9194
if (context.organizationId) {
92-
return {
93-
sourceMirrored:
94-
!isHosted || (await isOrganizationOnEnterprisePlan(context.organizationId, 'throw')),
95-
memberScoped: await isScopedCredentialGroupsAvailable({
95+
const [enterprise, memberScoped] = await Promise.all([
96+
isHosted ? isOrganizationOnEnterprisePlan(context.organizationId, 'throw') : true,
97+
isScopedCredentialGroupsAvailable({
9698
kind: 'organization',
9799
organizationId: context.organizationId,
98100
}),
99-
}
101+
])
102+
return { sourceMirrored: enterprise, memberScoped }
100103
}
101104
if (!context.workspaceId) throw new Error('Knowledge access requires a resource owner')
102105
const ownerBilling =

0 commit comments

Comments
 (0)