perf(tui): keep long streams responsive

This commit is contained in:
Xubin Ren
2026-08-17 20:56:10 +08:00
parent cf82b89307
commit 0d54ad96e2
6 changed files with 126 additions and 28 deletions
+13 -1
View File
@@ -20,6 +20,8 @@ type Choice =
| { kind: "model"; name: string; label: string; detail: string }
| { kind: "access"; mode: "restricted" | "full"; label: string; detail: string }
const CONTROLS_CACHE_MS = 10_000
interface RuntimeControlsOptions {
apiUrl: string
apiToken: string
@@ -49,6 +51,7 @@ export class RuntimeControls {
private modelPresets: Array<{ name: string; model: string }>
private canUseFullAccess: boolean
private controlsLoaded = false
private controlsLoadedAt = 0
private controlsPromise: Promise<void> | null = null
private scope: WorkspaceScopePayload
@@ -104,6 +107,11 @@ export class RuntimeControls {
this.contextText.content = text
}
/** Warm the small settings payload while the user is reading the first frame. */
preload(): void {
void this.load().catch(() => {})
}
updateWorkspaceScope(scope: WorkspaceScopePayload): void {
this.scope = scope
this.render()
@@ -175,11 +183,14 @@ export class RuntimeControls {
}
private async load(force = false): Promise<void> {
if (force) this.controlsLoaded = false
if (force && Date.now() - this.controlsLoadedAt >= CONTROLS_CACHE_MS) {
this.controlsLoaded = false
}
if (this.controlsLoaded) return
if (this.controlsPromise) return this.controlsPromise
if (!this.options.apiUrl || !this.options.apiToken) {
this.controlsLoaded = true
this.controlsLoadedAt = Date.now()
return
}
this.controlsPromise = (async () => {
@@ -197,6 +208,7 @@ export class RuntimeControls {
this.modelPresets = [...presets.values()]
this.canUseFullAccess = controls.canUseFullAccess
this.controlsLoaded = true
this.controlsLoadedAt = Date.now()
})().finally(() => { this.controlsPromise = null })
return this.controlsPromise
}