Files
simstudioai--sim/apps/sim/lib/billing/core/api-access.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

46 lines
2.0 KiB
TypeScript

import { getHighestPrioritySubscription } from '@/lib/billing/core/subscription'
import { isPaid } from '@/lib/billing/plan-helpers'
import { isBillingEnabled, isFreeApiDeploymentGateEnabled } from '@/lib/core/config/env-flags'
import { getWorkspaceBilledAccountUserId } from '@/lib/workspaces/utils'
/** The programmatic-execution paywall is active only when billing is enforced AND the gate flag is on. */
function isApiExecutionGateActive(): boolean {
return isBillingEnabled && isFreeApiDeploymentGateEnabled
}
/**
* Message for the 402 returned when a free-plan account attempts programmatic
* workflow execution (API key, public API, or MCP server).
*/
export const API_EXECUTION_REQUIRES_PAID_PLAN_MESSAGE =
'Programmatic workflow execution requires a paid plan. Upgrade to Pro or higher to use the API.'
/**
* Whether `userId` may run workflows programmatically. Always allowed when
* billing enforcement is off (self-hosted / `BILLING_ENABLED` unset) and when no
* user is resolved; otherwise requires a paid plan.
*
* `getHighestPrioritySubscription` rolls up organization memberships, so a free
* individual belonging to a paid org/workspace is entitled.
*/
export async function isApiExecutionEntitled(userId: string | undefined): Promise<boolean> {
if (!isApiExecutionGateActive() || !userId) return true
const subscription = await getHighestPrioritySubscription(userId)
return isPaid(subscription?.plan)
}
/**
* Workspace-scoped variant of {@link isApiExecutionEntitled} that gates on the
* workspace's billed account. Short-circuits when billing is off before any DB
* lookup, so the billed-account query only runs when billing is enforced.
*/
export async function isWorkspaceApiExecutionEntitled(
workspaceId: string | undefined
): Promise<boolean> {
if (!isApiExecutionGateActive() || !workspaceId) return true
const billedUserId = await getWorkspaceBilledAccountUserId(workspaceId)
return isApiExecutionEntitled(billedUserId ?? undefined)
}