The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
Fence Flash notices across account and focus changes
David91292ecef9f2
diff --git a/src/api/providers/openai-provider.ts b/src/api/providers/openai-provider.tsindex fb8b31a8..72388816 100644--- a/src/api/providers/openai-provider.ts+++ b/src/api/providers/openai-provider.ts@@ -1,4 +1,4 @@-import { parseFlashPolicy, recordFlashResponse } from '../../lib/flash-ui'+import { captureFlashGeneration, parseFlashPolicy, recordFlashResponse } from '../../lib/flash-ui'/*** OpenAI-Compatible Provider@@ -534,6 +534,7 @@ export class OpenAIProvider implements ProviderClient {signal: AbortSignal | undefined,fetcher: ChatFetcher,): Promise<Response> {+ const billingGeneration = captureFlashGeneration()// Sanierungspfad: a throttle or a gateway hiccup is not the request's// fault, and the user used to read the raw status line for it. The retry// sits HERE, around the request, so it can never replay a stream that has@@ -596,7 +597,7 @@ export class OpenAIProvider implements ProviderClient {else if (survived !== asked) this.rememberEffort(memoryKey, lane, 'minimal')}- recordFlashResponse(this.catalogKey(model), res)+ recordFlashResponse(this.catalogKey(model), res, billingGeneration)return res}diff --git a/src/components/chat/FlashChatNotice.tsx b/src/components/chat/FlashChatNotice.tsxindex 840b8626..48f48ca9 100644--- a/src/components/chat/FlashChatNotice.tsx+++ b/src/components/chat/FlashChatNotice.tsx@@ -1,7 +1,18 @@+import { useEffect } from 'react'import { useModelStore } from '../../stores/modelStore'-import { useFlashBillingStore } from '../../lib/flash-ui'+import { clearFlashNotices, useFlashBillingStore } from '../../lib/flash-ui'export function FlashChatNotice() {+ useEffect(() => {+ // Another tab can change the account while this surface is unfocused.+ window.addEventListener('blur', clearFlashNotices)+ window.addEventListener('focus', clearFlashNotices)+ return () => {+ window.removeEventListener('blur', clearFlashNotices)+ window.removeEventListener('focus', clearFlashNotices)+ clearFlashNotices()+ }+ }, [])const active = useModelStore((s) => s.models.find((m) => m.name === s.activeModel))const policy = active && 'flash' in active ? active.flash : undefinedconst notice = useFlashBillingStore((s) => policy ? s.entries[policy.billingKey] : undefined)@@ -32,4 +43,3 @@ export function FlashChatNotice() {</div>)}-diff --git a/src/components/chat/__tests__/flash-chat-notice.test.ts b/src/components/chat/__tests__/flash-chat-notice.test.tsindex 58a2f20d..84714e91 100644--- a/src/components/chat/__tests__/flash-chat-notice.test.ts+++ b/src/components/chat/__tests__/flash-chat-notice.test.ts@@ -4,7 +4,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'import { FlashChatNotice } from '../FlashChatNotice'import { useModelStore } from '../../../stores/modelStore'-import { parseFlashPolicy, recordFlashResponse, useFlashBillingStore } from '../../../lib/flash-ui'+import { captureFlashGeneration, clearFlashNotices, parseFlashPolicy, recordFlashResponse, useFlashBillingStore } from '../../../lib/flash-ui'const wire = { daily_tokens: 50000, default_max_output: 8192, request_seconds: 240, sessions_only: true, concurrent_requests: 1 }const policy = parseFlashPolicy(wire, 'flash', 'test-endpoint|model')!@@ -17,6 +17,22 @@ beforeEach(() => {afterEach(cleanup)describe('visible flash policy and billing', () => {+ it('clears notices across focus changes and fences late responses', () => {+ render(createElement(FlashChatNotice))+ const generation = captureFlashGeneration()+ const response = new Response('', { headers: { 'x-lu-chat-billing': 'credits', 'x-lu-flash-remaining': '0' } })+ act(() => recordFlashResponse(policy.billingKey, response, generation))+ expect(screen.queryByRole('alert')).not.toBeNull()+ act(() => window.dispatchEvent(new Event('blur')))+ expect(screen.queryByRole('alert')).toBeNull()+ act(() => recordFlashResponse(policy.billingKey, response, generation))+ expect(screen.queryByRole('alert')).toBeNull()+ act(() => window.dispatchEvent(new Event('focus')))+ act(() => recordFlashResponse(policy.billingKey, response, captureFlashGeneration()))+ expect(screen.queryByRole('alert')).not.toBeNull()+ act(clearFlashNotices)+ expect(screen.queryByRole('alert')).toBeNull()+ })it('publishes the server-provided limits before a request', () => {render(createElement(FlashChatNotice))fireEvent.click(screen.getByText(/50,000 input and output tokens/))diff --git a/src/lib/__tests__/flash-account-isolation.test.ts b/src/lib/__tests__/flash-account-isolation.test.tsnew file mode 100644index 00000000..46164b15--- /dev/null+++ b/src/lib/__tests__/flash-account-isolation.test.ts@@ -0,0 +1,18 @@+import { expect, it } from 'vitest'+import { useCloudAuthStore } from '../../stores/cloudAuthStore'+import { captureFlashGeneration, recordFlashResponse, useFlashBillingStore } from '../flash-ui'++it('clears notices on account changes and rejects the old in-flight response', () => {+ const account = { licenseActive: true, tier: 'test', access: true, quota: null }+ useCloudAuthStore.getState().setSignedIn({ id: 'account-a' }, account)+ const generation = captureFlashGeneration()+ const response = new Response('', { headers: { 'x-lu-chat-billing': 'flash', 'x-lu-flash-remaining': '40000' } })+ recordFlashResponse('test', response, generation)+ expect(useFlashBillingStore.getState().entries.test).toBeDefined()+ useCloudAuthStore.getState().setSignedIn({ id: 'account-b' }, account)+ recordFlashResponse('test', response, generation)+ expect(useFlashBillingStore.getState().entries).toEqual({})+ recordFlashResponse('test', response, captureFlashGeneration())+ useCloudAuthStore.getState().setSignedOut()+ expect(useFlashBillingStore.getState().entries).toEqual({})+})diff --git a/src/lib/flash-ui.ts b/src/lib/flash-ui.tsindex 013af576..b7b11084 100644--- a/src/lib/flash-ui.ts+++ b/src/lib/flash-ui.ts@@ -22,8 +22,15 @@ interface BillingNotice {ok: boolean}export const useFlashBillingStore = create<{ entries: Record<string, BillingNotice> }>(() => ({ entries: {} }))+let billingGeneration = 0+export const captureFlashGeneration = (): number => billingGeneration+export function clearFlashNotices(): void {+ billingGeneration += 1+ useFlashBillingStore.setState({ entries: {} })+}-export function recordFlashResponse(billingKey: string, response: Response): void {+export function recordFlashResponse(billingKey: string, response: Response, generation = billingGeneration): void {+ if (generation !== billingGeneration) returnconst billing = response.headers.get('x-lu-chat-billing')const raw = response.headers.get('x-lu-flash-remaining')const remaining = raw === null ? NaN : Number(raw)@@ -37,4 +44,3 @@ export function recordFlashResponse(billingKey: string, response: Response): voireturn { entries }})}-diff --git a/src/stores/cloudAuthStore.ts b/src/stores/cloudAuthStore.tsindex 13d48cb4..d9f7c037 100644--- a/src/stores/cloudAuthStore.ts+++ b/src/stores/cloudAuthStore.ts@@ -4,6 +4,7 @@// a persisted copy could only ever be stale.import { create } from 'zustand'+import { clearFlashNotices } from '../lib/flash-ui'import type { CloudQuota } from '../lib/render/cloud-jobs'export interface CloudUser {@@ -46,6 +47,10 @@ export const useCloudAuthStore = create<CloudAuthState>()((set) => ({setQuota: (quota) => set({ quota }),}))+useCloudAuthStore.subscribe((state, previous) => {+ if (state.user?.id !== previous.user?.id) clearFlashNotices()+})+/** The whole cloud axis in one predicate: signed in, actively licensed,* through the launch gate, and on a tier whose monthly credit budget is > 0* (self-host tiers report 0). */