Docs/Account SDK
Universal Developer

Account SDK

Account SDK

Overview

Account SDK provides user information, subscription status, and quota management capabilities for plugins that need to offer differentiated features based on user identity or subscription tier.

Introduction

Quick Start

EXAMPLE.TS
import { accountSDK, SubscriptionPlan } from '@talex-touch/utils'

// Check if user is logged in
const isLoggedIn = await accountSDK.isLoggedIn()

// Get current subscription plan
const plan = await accountSDK.getPlan()

// Check if paid user
if (await accountSDK.isPaidUser()) {
  // Show premium features
}

Subscription Plans

PlanEnum ValueDescription
FreeSubscriptionPlan.FREEBasic features with quota limits
ProSubscriptionPlan.PROMore quota, custom models
PlusSubscriptionPlan.PLUSPriority support, advanced analytics
TeamSubscriptionPlan.TEAMTeam collaboration, management
EnterpriseSubscriptionPlan.ENTERPRISEUnlimited quota, dedicated support

SDK Reference

Get SDK Instance

EXAMPLE.TS
import { accountSDK } from '@talex-touch/utils'

// Singleton, use directly
await accountSDK.getProfile()

Note: When using in Prelude script (index.js), you need to call accountSDK.setChannelSend(send) to inject the communication function first.


Auth Token & Device

Used by SDKs that need authenticated API access (e.g., Cloud Sync SDK).

EXAMPLE.TS
const token = await accountSDK.getAuthToken()
const deviceId = await accountSDK.getDeviceId()
MethodReturnDescription
getAuthToken()string | nullBearer auth token (null if unavailable)
getDeviceId()string | nullCurrent device id (null if unavailable)

User Information

getProfile()

Get complete user profile.

EXAMPLE.TS
const profile = await accountSDK.getProfile()
// {
//   id: 'user_xxx',
//   displayName: 'John Doe',
//   email: '[email protected]',
//   avatarUrl: 'https://...',
//   emailVerified: true,
//   createdAt: 1702123456789,
//   twoFactorEnabled: false,
//   socialConnections: [...]
// }
ReturnTypeDescription
profileUserProfile | nullUser profile, null if not logged in

isLoggedIn()

Check if user is logged in.

EXAMPLE.TS
if (await accountSDK.isLoggedIn()) {
  // Logged in
}

getUserId()

Get user ID.

EXAMPLE.TS
const userId = await accountSDK.getUserId()
// 'user_xxx' or null

getDisplayName()

Get display name.

EXAMPLE.TS
const name = await accountSDK.getDisplayName()
// 'John Doe'

getEmail()

Get user email.

EXAMPLE.TS
const email = await accountSDK.getEmail()
// '[email protected]'

getAvatarUrl()

Get avatar URL.

EXAMPLE.TS
const avatar = await accountSDK.getAvatarUrl()
// 'https://...'

Subscription Checks

getPlan()

Get current subscription plan.

EXAMPLE.TS
const plan = await accountSDK.getPlan()
// SubscriptionPlan.PRO

getSubscription()

Get full subscription details.

EXAMPLE.TS
const subscription = await accountSDK.getSubscription()
// {
//   id: 'sub_xxx',
//   plan: 'pro',
//   status: 'active',
//   billingCycle: 'monthly',
//   quota: { aiRequestsPerDay: 500, ... },
//   usage: { aiRequestsToday: 123, ... },
//   currentPeriodEnd: 1704067200000,
//   autoRenew: true
// }

isPaidUser()

Check if user is a paid subscriber.

EXAMPLE.TS
if (await accountSDK.isPaidUser()) {
  // Show paid features
}

isProOrAbove()

Check if Pro tier or higher.

EXAMPLE.TS
if (await accountSDK.isProOrAbove()) {
  // Pro / Plus / Team / Enterprise
}

isPlusOrAbove()

Check if Plus tier or higher.

EXAMPLE.TS
if (await accountSDK.isPlusOrAbove()) {
  // Plus / Team / Enterprise
}

isTeamOrAbove()

Check if Team tier or higher.

EXAMPLE.TS
if (await accountSDK.isTeamOrAbove()) {
  // Team / Enterprise
}

isEnterprise()

Check if Enterprise tier.

EXAMPLE.TS
if (await accountSDK.isEnterprise()) {
  // Enterprise only
}

isTrialing()

Check if in trial period.

EXAMPLE.TS
if (await accountSDK.isTrialing()) {
  const days = await accountSDK.getTrialDaysRemaining()
  console.log(`Trial ends in ${days} days`)
}

getDaysRemaining()

Get days remaining in current billing period.

EXAMPLE.TS
const days = await accountSDK.getDaysRemaining()
// 28

Quota Checks

getQuota()

Get current plan quota limits.

EXAMPLE.TS
const quota = await accountSDK.getQuota()
// {
//   aiRequestsPerDay: 500,
//   aiTokensPerMonth: 1000000,
//   maxPlugins: 20,
//   maxStorageBytes: 1073741824,
//   customModelAccess: true,
//   apiAccess: true,
//   ...
// }

getUsage()

Get current usage statistics.

EXAMPLE.TS
const usage = await accountSDK.getUsage()
// {
//   aiRequestsToday: 123,
//   aiRequestsThisMonth: 2456,
//   aiTokensThisMonth: 456789,
//   storageUsedBytes: 52428800,
//   pluginsInstalled: 8
// }

checkAiRequestQuota()

Check AI request quota.

EXAMPLE.TS
const result = await accountSDK.checkAiRequestQuota()
if (!result.allowed) {
  console.log(result.reason) // 'Daily AI request limit reached'
  console.log(`Resets at: ${new Date(result.resetAt)}`)
} else {
  console.log(`Remaining: ${result.remaining}`)
}

checkAiTokenQuota(estimatedTokens)

Check AI token quota.

EXAMPLE.TS
const result = await accountSDK.checkAiTokenQuota(1000)
if (!result.allowed) {
  // Quota exceeded
}
ParameterTypeDescription
estimatedTokensnumberEstimated tokens to consume (optional, default 0)

checkStorageQuota(additionalBytes)

Check storage quota.

EXAMPLE.TS
const result = await accountSDK.checkStorageQuota(1024 * 1024) // 1MB
if (!result.allowed) {
  console.log('Insufficient storage')
}

checkPluginQuota()

Check plugin installation quota.

EXAMPLE.TS
const result = await accountSDK.checkPluginQuota()
if (!result.allowed) {
  console.log('Plugin limit reached')
}

getUsagePercentage(type)

Get usage percentage for specific quota type.

EXAMPLE.TS
const aiUsage = await accountSDK.getUsagePercentage('aiRequests')
console.log(`AI usage: ${aiUsage.toFixed(1)}%`)

const storageUsage = await accountSDK.getUsagePercentage('storage')
if (storageUsage > 80) {
  console.warn('Storage running low')
}
ParameterTypeValues
typestring'aiRequests', 'aiTokens', 'storage', 'plugins'

Feature Access

hasApiAccess()

Check if user has API access.

EXAMPLE.TS
if (await accountSDK.hasApiAccess()) {
  // Allow API usage
}

hasCustomModelAccess()

Check if user can use custom models.

EXAMPLE.TS
if (await accountSDK.hasCustomModelAccess()) {
  // Show custom model options
}

hasPrioritySupport()

Check if user has priority support.

EXAMPLE.TS
if (await accountSDK.hasPrioritySupport()) {
  // Show priority support entry
}

hasAdvancedAnalytics()

Check if user has advanced analytics.

EXAMPLE.TS
if (await accountSDK.hasAdvancedAnalytics()) {
  // Show analytics dashboard
}

hasFeature(featureId)

Check if specific feature flag is enabled.

EXAMPLE.TS
if (await accountSDK.hasFeature('beta-ai-v2')) {
  // Enable beta feature
}

Team Management

getTeams()

Get all teams user belongs to.

EXAMPLE.TS
const teams = await accountSDK.getTeams()
// [
//   { id: 'team_xxx', name: 'Dev Team', role: 'admin', memberCount: 5 },
//   { id: 'team_yyy', name: 'Design', role: 'member', memberCount: 3 }
// ]

isInTeam()

Check if user is in any team.

EXAMPLE.TS
if (await accountSDK.isInTeam()) {
  // Show team features
}

isTeamOwner(teamId?)

Check if user is team owner.

EXAMPLE.TS
if (await accountSDK.isTeamOwner()) {
  // User owns some team
}

if (await accountSDK.isTeamOwner('team_xxx')) {
  // User owns specific team
}

isTeamAdmin(teamId?)

Check if user is team admin (includes owner).

EXAMPLE.TS
if (await accountSDK.isTeamAdmin()) {
  // Show admin features
}

Upgrade & Billing

getUpgradeOptions()

Get available upgrade options.

EXAMPLE.TS
const options = accountSDK.getUpgradeOptions()
// [
//   { plan: 'pro', name: 'Pro', priceMonthly: 9.99, features: [...] },
//   { plan: 'plus', name: 'Plus', priceMonthly: 19.99, recommended: true, ... }
// ]

getPlanComparison()

Get plan comparison table.

EXAMPLE.TS
const comparison = accountSDK.getPlanComparison()
// [
//   { feature: 'AI Requests/Day', free: 50, pro: 500, plus: 2000, ... },
//   { feature: 'Custom Models', free: false, pro: true, ... }
// ]

openUpgradePage(plan?)

Open upgrade page.

EXAMPLE.TS
await accountSDK.openUpgradePage() // Open upgrade page
await accountSDK.openUpgradePage(SubscriptionPlan.PLUS) // Jump to Plus

openBillingPage()

Open billing management page.

EXAMPLE.TS
await accountSDK.openBillingPage()

Account Actions

requestLogin()

Request user login (opens login dialog).

EXAMPLE.TS
const success = await accountSDK.requestLogin()
if (success) {
  // User logged in
}

logout()

Logout current user.

EXAMPLE.TS
await accountSDK.logout()

openAccountSettings()

Open account settings page.

EXAMPLE.TS
await accountSDK.openAccountSettings()

Technical Notes

  • Account SDK reads login state, subscription plan, and quota from the unified account service.
  • Client-side helpers consolidate permission and quota checks to avoid scattered logic.

Best Practices

1. Premium Feature Gating

EXAMPLE.TS
async function showPremiumFeature() {
  if (!await accountSDK.isPaidUser()) {
    // Show upgrade prompt
    const options = accountSDK.getUpgradeOptions()
    showUpgradeDialog(options)
    return
  }
  
  // Show premium feature
}

2. Quota Pre-check

EXAMPLE.TS
async function beforeAiRequest(estimatedTokens: number) {
  const quotaCheck = await accountSDK.checkAiTokenQuota(estimatedTokens)
  
  if (!quotaCheck.allowed) {
    throw new Error(`Quota exceeded: ${quotaCheck.reason}`)
  }
  
  // Proceed with request
}

3. Team Feature Adaptation

EXAMPLE.TS
async function initTeamFeatures() {
  if (!await accountSDK.isTeamOrAbove()) {
    return // Not team tier, skip
  }
  
  const teams = await accountSDK.getTeams()
  
  for (const team of teams) {
    if (team.role === 'owner' || team.role === 'admin') {
      // Show admin features
    }
  }
}

Type Definitions

EXAMPLE.TS
enum SubscriptionPlan {
  FREE = 'free',
  PRO = 'pro',
  PLUS = 'plus',
  TEAM = 'team',
  ENTERPRISE = 'enterprise',
}

interface UserProfile {
  id: string
  displayName: string
  username?: string
  email: string
  emailVerified: boolean
  avatarUrl?: string
  createdAt: number
  twoFactorEnabled: boolean
  socialConnections: SocialConnection[]
}

interface Subscription {
  id: string
  plan: SubscriptionPlan
  status: SubscriptionStatus
  quota: PlanQuota
  usage: UsageStats
  currentPeriodEnd: number
  autoRenew: boolean
}

interface QuotaCheckResult {
  allowed: boolean
  reason?: string
  remaining?: number
  resetAt?: number
}

Was this helpful?