()
| 18 | | { type: 'browser-opened'; url: string; opened: boolean } |
| 19 | |
| 20 | export async function runExtraUsage(): Promise<ExtraUsageResult> { |
| 21 | if (!getGlobalConfig().hasVisitedExtraUsage) { |
| 22 | saveGlobalConfig(prev => ({ ...prev, hasVisitedExtraUsage: true })) |
| 23 | } |
| 24 | // Invalidate only the current org's entry so a follow-up read refetches |
| 25 | // the granted state. Separate from the visited flag since users may run |
| 26 | // /extra-usage more than once while iterating on the claim flow. |
| 27 | invalidateOverageCreditGrantCache() |
| 28 | |
| 29 | const { isTeamOrEnterprise } = buildExtraUsageSessionState( |
| 30 | getAuthRuntime().getCurrentSession(), |
| 31 | ) |
| 32 | const hasBillingAccess = hasManagedPlanBillingAccess() |
| 33 | |
| 34 | if (!hasBillingAccess && isTeamOrEnterprise) { |
| 35 | // Mirror apps/claude-ai useHasUnlimitedOverage(): if overage is enabled |
| 36 | // with no monthly cap, there is nothing to request. On fetch error, fall |
| 37 | // through and let the user ask (matching web's "err toward show" behavior). |
| 38 | let extraUsage: ExtraUsage | null | undefined |
| 39 | try { |
| 40 | const utilization = await fetchUtilization() |
| 41 | extraUsage = utilization?.extra_usage |
| 42 | } catch (error) { |
| 43 | logError(error as Error) |
| 44 | } |
| 45 | |
| 46 | if (extraUsage?.is_enabled && extraUsage.monthly_limit === null) { |
| 47 | return { |
| 48 | type: 'message', |
| 49 | value: |
| 50 | 'Your organization already has unlimited extra usage. No request needed.', |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | try { |
| 55 | const eligibility = await checkAdminRequestEligibility('limit_increase') |
| 56 | if (eligibility?.is_allowed === false) { |
| 57 | return { |
| 58 | type: 'message', |
| 59 | value: 'Please contact your admin to manage extra usage settings.', |
| 60 | } |
| 61 | } |
| 62 | } catch (error) { |
| 63 | logError(error as Error) |
| 64 | // If eligibility check fails, continue — the create endpoint will enforce if necessary |
| 65 | } |
| 66 | |
| 67 | try { |
| 68 | const pendingOrDismissedRequests = await getMyAdminRequests( |
| 69 | 'limit_increase', |
| 70 | ['pending', 'dismissed'], |
| 71 | ) |
| 72 | if (pendingOrDismissedRequests && pendingOrDismissedRequests.length > 0) { |
| 73 | return { |
| 74 | type: 'message', |
| 75 | value: |
| 76 | 'You have already submitted a request for extra usage to your admin.', |
| 77 | } |
no test coverage detected