feat: integrate token point usage v2
Some checks failed
Electron E2E / Electron E2E (macos-latest) (push) Has been cancelled
Electron E2E / Electron E2E (ubuntu-latest) (push) Has been cancelled
Electron E2E / Electron E2E (windows-latest) (push) Has been cancelled

This commit is contained in:
2026-09-06 14:24:50 +08:00
parent 9fed0cc7c4
commit 83ad10d95e
14 changed files with 907 additions and 201 deletions

View File

@@ -0,0 +1,24 @@
import type { WorksTokenPointBalance } from './works-square';
const TOKEN_POINT_VALUE_PATTERN = /^(0|[1-9]\d*)(?:\.(\d{1,2}))?$/u;
export function formatWorksTokenPointValue(value: string | null | undefined): string | null {
if (typeof value !== 'string') return null;
const match = TOKEN_POINT_VALUE_PATTERN.exec(value.trim());
if (!match) return null;
const whole = match[1].replace(/\B(?=(\d{3})+(?!\d))/gu, ',');
const fraction = (match[2] ?? '').replace(/0+$/u, '');
return fraction ? `${whole}.${fraction}` : whole;
}
export function isWorksTokenPointBalanceExhausted(
balance: WorksTokenPointBalance | null | undefined,
): boolean {
if (!balance) return false;
if (!balance.can_manage_membership && typeof balance.shared_available === 'boolean') {
return !balance.shared_available;
}
const remaining = formatWorksTokenPointValue(balance.total_remaining);
return remaining !== null && /^0(?:\.0+)?$/u.test(remaining);
}

View File

@@ -1,14 +0,0 @@
import type { WorksTokenUsage } from './works-square';
function isRemainingPercentExhausted(value: number | null | undefined): boolean {
return typeof value === 'number' && Number.isFinite(value) && value <= 0;
}
export function isWorksTokenUsageExhausted(
usage: WorksTokenUsage | null | undefined,
): boolean {
return Boolean(usage && (
isRemainingPercentExhausted(usage.five_hour_remaining_percent)
|| isRemainingPercentExhausted(usage.weekly_remaining_percent)
));
}

View File

@@ -1,11 +0,0 @@
export const WORKS_SQUARE_TOKEN_USAGE_STALE_EVENT = 'works-square-token-usage:stale';
export function dispatchWorksSquareTokenUsageStale(): void {
if (typeof window === 'undefined' || typeof window.dispatchEvent !== 'function') {
return;
}
if (typeof Event !== 'function') {
return;
}
window.dispatchEvent(new Event(WORKS_SQUARE_TOKEN_USAGE_STALE_EVENT));
}

View File

@@ -187,13 +187,26 @@ export type WorksSpeechTranscriptionInput = {
model?: string;
};
export type WorksTokenUsage = {
five_hour_remaining_percent: number | null;
five_hour_refresh_at?: string | null;
weekly_remaining_percent: number | null;
weekly_refresh_at?: string | null;
plan_code?: string | null;
plan_name?: string | null;
export type WorksTokenPointBalance = {
plan_code: string | null;
plan_name: string | null;
cycle_start: string | null;
cycle_end: string | null;
next_refresh_at: string | null;
weekly_allowance: string | null;
weekly_used: string | null;
weekly_reserved: string | null;
weekly_remaining: string | null;
permanent_total: string | null;
permanent_used: string | null;
permanent_reserved: string | null;
permanent_remaining: string | null;
total_remaining: string | null;
entitlement_source: 'self' | 'family_owner' | 'shared_group';
family_shared: boolean;
can_manage_membership: boolean;
upgrade_action: 'self_service' | 'contact_family_owner';
shared_available: boolean | null;
};
export type PlazaCard = {
@@ -402,16 +415,16 @@ export async function fetchMyWorksProjects(
return assertSuccess(response, 'page', 'Failed to load your Works Square projects');
}
export async function fetchWorksTokenUsage(accessToken: string): Promise<WorksTokenUsage> {
const response = await hostApiFetch<WorksActionResponse<'usage', WorksTokenUsage>>(
'/api/works/billing/token-usage',
export async function fetchWorksTokenPointBalance(accessToken: string): Promise<WorksTokenPointBalance> {
const response = await hostApiFetch<WorksActionResponse<'points', WorksTokenPointBalance>>(
'/api/works/billing/points',
{
headers: {
'X-NianCode-Access-Token': accessToken,
},
},
);
return assertSuccess(response, 'usage', 'Failed to load Works Square token usage');
return assertSuccess(response, 'points', 'Failed to load Works Square token points');
}
export async function fetchMyWorksProjectStatus(