Skip to content

Prevent user from chating if usage above the limit #111

@sirily11

Description

@sirily11

in the chat api app/(chat)/api/chat/route.ts we currently only check if user can select certain model:

// Get user entitlements and available models to validate the selected model
    const userEntitlements = entitlementsByUserRole[user.role];
    const availableProviders = await getFilteredProviders(
      user,
      userEntitlements,
      isTestEnvironment,
    );

    // Check if the selected model is available to this user
    const selectedProvider = availableProviders[selectedChatModelProvider];
    const isModelAvailable = selectedProvider?.models.some(
      (model) => model.id === selectedChatModel,
    );

    if (!isModelAvailable) {
      return new ChatSDKError(
        'forbidden:chat',
        'Model not available for user',
      ).toResponse();

but not check whether user exceed the current usage limit. So you need to

  1. Refactor this code to a function call check userAvailablility that checks both model selection and usage limit
  2. Add user's spending limit to entitlement file
  3. Throw error if limit hit
  4. Add unit test with real db connection that similar to lib/db/queries/vector-store.spec.ts using real postgres db

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions