diff --git a/api/routers/agent.py b/api/routers/agent.py index 62fdff06..e8b1fd8b 100644 --- a/api/routers/agent.py +++ b/api/routers/agent.py @@ -104,7 +104,7 @@ def registry_tool_lines(registry: MeshOpsRegistry) -> str: - **install_extension(github_url)** / **uninstall_extension(extension_id)** / **reload_extensions** — Manage extensions. - **download_model_weights(extension_id, variant?)** / **delete_model_weights(extension_id, variant?)** — Manage model weights. - **open_workflow(workflow_id)** / **delete_workflow(workflow_id)** — Open a workflow in the editor, or delete it. -- **set_setting(key, value)** — Change `uiScale` (one of `small`, `medium`, `large`, `very-large`), `useAtkinsonFont` or `showRamIndicator` (booleans). +- **set_setting(key, value)** — Change `uiScale` (one of `small`, `medium`, `large`, `very-large`), `useAtkinsonFont`, `showRamIndicator` or `showVramIndicator` (booleans). - **set_storage_folder(kind, path)** — Move the `models`, `workspace` or `workflows` folder. - **undo_mesh** / **redo_mesh** — Undo or redo the last change to the current mesh. - **navigate(page)** — Switch the app to another page. @@ -378,9 +378,9 @@ def _function(name: str, description: str, properties: dict | None = None, requi ), _function( "set_setting", - "Change a UI setting. uiScale is one of small, medium, large, very-large; useAtkinsonFont and showRamIndicator are booleans.", + "Change a UI setting. uiScale is one of small, medium, large, very-large; useAtkinsonFont, showRamIndicator and showVramIndicator are booleans.", { - "key": {"type": "string", "enum": ["uiScale", "useAtkinsonFont", "showRamIndicator"]}, + "key": {"type": "string", "enum": ["uiScale", "useAtkinsonFont", "showRamIndicator", "showVramIndicator"]}, "value": {"type": ["string", "boolean"], "description": "For uiScale one of: small, medium, large, very-large. A boolean otherwise."}, }, ["key", "value"], diff --git a/api/tests/test_agent_client_tools.py b/api/tests/test_agent_client_tools.py index 6c00e104..502b8c70 100644 --- a/api/tests/test_agent_client_tools.py +++ b/api/tests/test_agent_client_tools.py @@ -342,7 +342,7 @@ def test_every_client_tool_has_a_schema(self) -> None: def test_enums(self) -> None: props = self._tool("set_setting")["parameters"]["properties"] - self.assertEqual(props["key"]["enum"], ["uiScale", "useAtkinsonFont", "showRamIndicator"]) + self.assertEqual(props["key"]["enum"], ["uiScale", "useAtkinsonFont", "showRamIndicator", "showVramIndicator"]) # The app's scale names, not a number: the frontend accepts only these. self.assertEqual(props["value"]["type"], ["string", "boolean"]) self.assertIn("small, medium, large, very-large", props["value"]["description"]) @@ -372,7 +372,7 @@ def test_system_prompt_lists_the_new_tools(self) -> None: "modelsDir": "D:/models", "workspaceDir": "D:/ws", "workflowsDir": "D:/wf", "extensionsDir": "D:/ext", }, - "ui": {"uiScale": "large", "useAtkinsonFont": False, "showRamIndicator": True}, + "ui": {"uiScale": "large", "useAtkinsonFont": False, "showRamIndicator": True, "showVramIndicator": False}, } @@ -389,7 +389,7 @@ def test_list_extensions(self) -> None: def test_get_settings(self) -> None: text, payload = _run("get_settings") - for expected in ("D:/models", "D:/ws", "D:/wf", "D:/ext", "uiScale: large", "useAtkinsonFont: false", "showRamIndicator: true"): + for expected in ("D:/models", "D:/ws", "D:/wf", "D:/ext", "uiScale: large", "useAtkinsonFont: false", "showRamIndicator: true", "showVramIndicator: false"): self.assertIn(expected, text) self.assertIsNone(payload) self.assertIn("No settings", _run("get_settings", context={})[0]) @@ -452,7 +452,7 @@ def test_context_is_rendered_into_the_single_system_message(self) -> None: "- sf3d: SF3D", "- wf1: My Workflow", "D:/models", "D:/ws", "D:/wf", "D:/ext", - "uiScale: large", "useAtkinsonFont: false", "showRamIndicator: true", + "uiScale: large", "useAtkinsonFont: false", "showRamIndicator: true", "showVramIndicator: false", "Current mesh path: Default/mesh.glb", "- trellis/gen (image→mesh): Trellis", ): diff --git a/electron/main/gpu-memory.test.ts b/electron/main/gpu-memory.test.ts new file mode 100644 index 00000000..aeaedae9 --- /dev/null +++ b/electron/main/gpu-memory.test.ts @@ -0,0 +1,148 @@ +import assert from 'node:assert/strict' +import test from 'node:test' + +async function load() { + return import(new URL('./gpu-memory.ts', import.meta.url).href) +} + +const MIB = 1024 * 1024 + +test('parses a single GPU and converts MiB to bytes', async () => { + const { parseNvidiaSmiGpus, pickGpu, toGpuMemory } = await load() + const gpus = parseNvidiaSmiGpus('0, NVIDIA GeForce RTX 4090, 24564, 1234\n') + assert.deepEqual(gpus, [{ index: 0, name: 'NVIDIA GeForce RTX 4090', totalMiB: 24564, usedMiB: 1234 }]) + assert.deepEqual(toGpuMemory(pickGpu(gpus)), { + total: 24564 * MIB, + used: 1234 * MIB, + available: (24564 - 1234) * MIB, + name: 'NVIDIA GeForce RTX 4090', + }) +}) + +test('picks the GPU with the most memory on a multi-GPU machine', async () => { + const { parseNvidiaSmiGpus, pickGpu } = await load() + const stdout = [ + '0, NVIDIA GeForce GTX 1650, 4096, 300', + '1, NVIDIA GeForce RTX 3090, 24576, 9000', + '2, NVIDIA GeForce RTX 3060, 12288, 100', + ].join('\r\n') + const gpus = parseNvidiaSmiGpus(stdout) + assert.equal(gpus.length, 3) + assert.equal(pickGpu(gpus).index, 1) + assert.equal(pickGpu(gpus).name, 'NVIDIA GeForce RTX 3090') +}) + +test('keeps the first GPU on a tie', async () => { + const { parseNvidiaSmiGpus, pickGpu } = await load() + const gpus = parseNvidiaSmiGpus('0, A, 8192, 1\n1, B, 8192, 2\n') + assert.equal(pickGpu(gpus).index, 0) +}) + +test('rebuilds names containing commas and collapses extra spaces', async () => { + const { parseNvidiaSmiGpus } = await load() + const [gpu] = parseNvidiaSmiGpus(' 3 , Quadro RTX, 8000 Rev,B , 49152 , 0 ') + assert.deepEqual(gpu, { index: 3, name: 'Quadro RTX, 8000 Rev, B', totalMiB: 49152, usedMiB: 0 }) +}) + +test('skips lines with [N/A] or garbage and keeps the valid ones', async () => { + const { parseNvidiaSmiGpus, pickGpu } = await load() + const gpus = parseNvidiaSmiGpus('0, Tesla, [N/A], [N/A]\nnot a gpu\n1, RTX 4070, 12282, 512\n') + assert.equal(gpus.length, 1) + assert.equal(pickGpu(gpus).name, 'RTX 4070') +}) + +test('empty or error output yields no GPU', async () => { + const { parseNvidiaSmiGpus, pickGpu } = await load() + for (const stdout of ['', '\n\n', 'NVIDIA-SMI has failed because it couldn\'t communicate with the NVIDIA driver.', ', , ,', '0, X, 0, 0']) { + assert.equal(pickGpu(parseNvidiaSmiGpus(stdout)), null, JSON.stringify(stdout)) + } +}) + +test('sampler returns the parsed GPU and passes the timeout', async () => { + const { createGpuMemorySampler, NVIDIA_SMI_ARGS } = await load() + const calls: Array<{ args: string[]; timeoutMs: number }> = [] + const sample = createGpuMemorySampler({ + platform: 'win32', + timeoutMs: 3000, + run: async (args: string[], timeoutMs: number) => { + calls.push({ args, timeoutMs }) + return { kind: 'ok', stdout: '0, RTX, 8192, 2048\n' } + }, + }) + const mem = await sample() + assert.deepEqual(mem, { total: 8192 * MIB, used: 2048 * MIB, available: 6144 * MIB, name: 'RTX' }) + assert.deepEqual(calls, [{ args: NVIDIA_SMI_ARGS, timeoutMs: 3000 }]) +}) + +test('sampler remembers a missing nvidia-smi and stops spawning', async () => { + const { createGpuMemorySampler } = await load() + let runs = 0 + const sample = createGpuMemorySampler({ + platform: 'linux', + run: async () => { runs++; return { kind: 'missing' } }, + }) + assert.equal(await sample(), null) + assert.equal(await sample(), null) + assert.equal(await sample(), null) + assert.equal(runs, 1) +}) + +test('sampler retries after a transient failure (timeout / non-zero exit)', async () => { + const { createGpuMemorySampler } = await load() + const results = [{ kind: 'failed' }, { kind: 'ok', stdout: '0, RTX, 4096, 0' }] + let runs = 0 + const sample = createGpuMemorySampler({ + platform: 'linux', + run: async () => results[runs++], + }) + assert.equal(await sample(), null) + assert.equal((await sample())?.name, 'RTX') + assert.equal(runs, 2) +}) + +test('sampler shares one in-flight run between concurrent calls', async () => { + const { createGpuMemorySampler } = await load() + let runs = 0 + let release: (value: unknown) => void = () => {} + const sample = createGpuMemorySampler({ + platform: 'win32', + run: () => { + runs++ + return new Promise((resolve) => { release = resolve }) + }, + }) + const a = sample() + const b = sample() + assert.equal(a, b) + release({ kind: 'ok', stdout: '0, RTX, 4096, 1024' }) + const [ra, rb] = await Promise.all([a, b]) + assert.equal(runs, 1) + assert.deepEqual(ra, rb) + + // Once settled, the next call starts a fresh run. + const c = sample() + release({ kind: 'failed' }) + assert.equal(await c, null) + assert.equal(runs, 2) +}) + +test('sampler never spawns on macOS', async () => { + const { createGpuMemorySampler } = await load() + let runs = 0 + const sample = createGpuMemorySampler({ + platform: 'darwin', + run: async () => { runs++; return { kind: 'ok', stdout: '0, X, 1024, 0' } }, + }) + assert.equal(await sample(), null) + assert.equal(runs, 0) +}) + +test('sampler swallows a throwing runner', async () => { + const { createGpuMemorySampler } = await load() + const sample = createGpuMemorySampler({ + platform: 'linux', + run: async () => { throw new Error('boom') }, + }) + assert.equal(await sample(), null) +}) + diff --git a/electron/main/gpu-memory.ts b/electron/main/gpu-memory.ts new file mode 100644 index 00000000..18a46a62 --- /dev/null +++ b/electron/main/gpu-memory.ts @@ -0,0 +1,157 @@ +/** + * VRAM sampling for the status-bar indicator (NVIDIA only, via nvidia-smi). + * + * The renderer polls this every couple of seconds, so the sampler: + * - never runs two nvidia-smi processes at once (concurrent calls share one), + * - kills a stalled nvidia-smi after a timeout (the driver can hang while a + * model is generating) and reports null for that sample, + * - stops spawning for the rest of the session once nvidia-smi turns out not + * to exist (AMD / Intel / no GPU), and + * - returns null on macOS without spawning anything. + */ + +import { spawn } from 'child_process' + +export interface NvidiaGpu { + index: number + name: string + totalMiB: number + usedMiB: number +} + +/** Bytes. Mirrors the `system:gpuMemory` IPC contract. */ +export interface GpuMemory { + total: number + used: number + available: number + name: string +} + +const MIB = 1024 * 1024 + +export const NVIDIA_SMI_ARGS = [ + '--query-gpu=index,name,memory.total,memory.used', + '--format=csv,noheader,nounits', +] + +// ─── Pure helpers ───────────────────────────────────────────────────────────── + +/** + * Parses `nvidia-smi --query-gpu=index,name,memory.total,memory.used + * --format=csv,noheader,nounits`, e.g. "0, NVIDIA GeForce RTX 4090, 24564, 1234". + * The name sits between the index and the two numbers, so a name that itself + * contains commas is rebuilt from the middle fields. Lines that do not parse + * (blank, "[N/A]", error text) are skipped. + */ +export function parseNvidiaSmiGpus(stdout: string): NvidiaGpu[] { + const gpus: NvidiaGpu[] = [] + for (const raw of stdout.split(/\r?\n/)) { + const parts = raw.split(',').map((s) => s.trim()) + if (parts.length < 4) continue + const index = Number(parts[0]) + const totalMiB = Number(parts[parts.length - 2]) + const usedMiB = Number(parts[parts.length - 1]) + const name = parts.slice(1, -2).join(', ').replace(/\s+/g, ' ').trim() + if (parts[0] === '' || !Number.isInteger(index)) continue + if (!Number.isFinite(totalMiB) || !Number.isFinite(usedMiB)) continue + if (totalMiB <= 0 || usedMiB < 0) continue + gpus.push({ index, name, totalMiB, usedMiB }) + } + return gpus +} + +/** The GPU with the most memory (where heavy models end up); first one wins ties. */ +export function pickGpu(gpus: NvidiaGpu[]): NvidiaGpu | null { + let best: NvidiaGpu | null = null + for (const gpu of gpus) { + if (!best || gpu.totalMiB > best.totalMiB) best = gpu + } + return best +} + +export function toGpuMemory(gpu: NvidiaGpu): GpuMemory { + const total = gpu.totalMiB * MIB + const used = Math.min(gpu.usedMiB * MIB, total) + return { total, used, available: total - used, name: gpu.name || `GPU ${gpu.index}` } +} + +// ─── Sampler ────────────────────────────────────────────────────────────────── + +/** + * Outcome of one nvidia-smi run. `missing` means the binary is not there and + * never will be this session; `failed` covers timeouts and non-zero exits, + * which may clear up on the next sample. + */ +export type NvidiaSmiResult = + | { kind: 'ok'; stdout: string } + | { kind: 'missing' } + | { kind: 'failed' } + +export type NvidiaSmiRunner = (args: string[], timeoutMs: number) => Promise + +export const runNvidiaSmi: NvidiaSmiRunner = (args, timeoutMs) => + new Promise((resolve) => { + let settled = false + const finish = (result: NvidiaSmiResult): void => { + if (settled) return + settled = true + clearTimeout(timer) + resolve(result) + } + + const proc = spawn('nvidia-smi', args, { + stdio: ['ignore', 'pipe', 'ignore'], + windowsHide: true, + }) + const timer = setTimeout(() => { + proc.kill() + finish({ kind: 'failed' }) + }, timeoutMs) + + let out = '' + proc.stdout?.on('data', (d: Buffer) => { out += d.toString() }) + proc.on('close', (code) => finish(code === 0 ? { kind: 'ok', stdout: out } : { kind: 'failed' })) + proc.on('error', (err: NodeJS.ErrnoException) => + finish(err.code === 'ENOENT' ? { kind: 'missing' } : { kind: 'failed' })) + }) + +export interface GpuMemorySamplerOptions { + run?: NvidiaSmiRunner + platform?: NodeJS.Platform + timeoutMs?: number +} + +export function createGpuMemorySampler(options: GpuMemorySamplerOptions = {}): () => Promise { + const run = options.run ?? runNvidiaSmi + const platform = options.platform ?? process.platform + const timeoutMs = options.timeoutMs ?? 3000 + + let unavailable = platform === 'darwin' + let inFlight: Promise | null = null + + const sampleOnce = async (): Promise => { + try { + const result = await run(NVIDIA_SMI_ARGS, timeoutMs) + if (result.kind === 'missing') { + unavailable = true + return null + } + if (result.kind !== 'ok') return null + const gpu = pickGpu(parseNvidiaSmiGpus(result.stdout)) + return gpu ? toGpuMemory(gpu) : null + } catch { + return null + } + } + + return () => { + if (unavailable) return Promise.resolve(null) + if (!inFlight) { + inFlight = sampleOnce().finally(() => { inFlight = null }) + } + return inFlight + } +} + +/** Session-wide sampler used by the `system:gpuMemory` IPC handler. */ +export const sampleGpuMemory = createGpuMemorySampler() diff --git a/electron/main/ipc-handlers.ts b/electron/main/ipc-handlers.ts index 7d656e74..a43d1efa 100644 --- a/electron/main/ipc-handlers.ts +++ b/electron/main/ipc-handlers.ts @@ -64,6 +64,7 @@ import { resolveExtensionPathWithinRoot, } from './extension-path-guard' import { detectGpuInfo, describeGpuInfo, torchFlavorFor, type GpuInfo } from './gpu-detect' +import { sampleGpuMemory } from './gpu-memory' import { SETUP_LAUNCHER_SOURCE } from './setup-launcher' import { assertCompatibleExtensionUpdateType, @@ -914,6 +915,10 @@ export function setupIpcHandlers(pythonBridge: PythonBridge, getWindow: WindowGe return { total, used: total - free, available: free } }) + // GPU memory (VRAM) — NVIDIA only via nvidia-smi + // (null on macOS, without NVIDIA, or when a sample times out). + ipcMain.handle('system:gpuMemory', () => sampleGpuMemory()) + ipcMain.handle('app:info', () => ({ version: app.getVersion(), userData: app.getPath('userData'), diff --git a/electron/preload/electron-api.ts b/electron/preload/electron-api.ts index 364ba965..9a309e27 100644 --- a/electron/preload/electron-api.ts +++ b/electron/preload/electron-api.ts @@ -62,6 +62,9 @@ export function createElectronApi(ipcRenderer: IpcRendererLike, webFrame: WebFra system: { memory: (): Promise<{ total: number; used: number; available: number }> => ipcRenderer.invoke('system:memory') as Promise<{ total: number; used: number; available: number }>, + /** VRAM in bytes of the largest NVIDIA GPU; null without NVIDIA, on macOS, or when a sample timed out. */ + gpuMemory: (): Promise<{ total: number; used: number; available: number; name: string } | null> => + ipcRenderer.invoke('system:gpuMemory') as Promise<{ total: number; used: number; available: number; name: string } | null>, }, // Python / FastAPI bridge diff --git a/package.json b/package.json index 41881a13..0ef0908f 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "prepare-resources": "node scripts/download-python-embed.js", "test": "npm run test:py && npm run test:node", "test:py": "node scripts/run-pytests.mjs", - "test:node": "node --test --experimental-strip-types --experimental-loader ./scripts/node-ts-extensionless-loader.mjs src/shared/types/assetLibrary.test.ts src/areas/generate/assetLibraryProjection.test.ts src/areas/generate/assetLibraryService.test.ts src/areas/generate/assetLibraryUi.test.ts src/areas/library/libraryView.test.ts src/areas/library/gridWindow.test.ts src/areas/generate/orcaSlicerLink.test.ts src/shared/utils/agentStream.test.ts src/shared/thumbnails/assetThumbnailQueue.test.ts src/shared/thumbnails/assetThumbnailStore.test.ts src/shared/components/agent/clientTools.test.ts src/shared/components/agent/agentContext.test.ts src/shared/utils/ollamaHealth.test.ts src/shared/utils/notification.test.ts src/shared/utils/agentConversations.test.ts electron/main/artifact-registry-service.test.ts electron/main/asset-thumbnail-cache.test.ts electron/main/extension-path-guard.test.ts electron/main/storage-directory-guard.test.ts electron/main/storage-ownership.test.ts electron/main/startup-error.test.ts electron/main/api-port.test.ts electron/main/agent-conversations.test.ts electron/preload/artifact-registry-preload.test.ts src/shared/stores/meshHistory.test.ts src/areas/generate/meshEditPanel.test.ts src/areas/generate/sculpt/SculptMesh.test.ts src/areas/generate/sculpt/SculptMesh.perf.test.ts src/areas/generate/sculpt/sculptLogic.test.ts src/shared/pages/pageRegistry.test.ts src/shared/pages/startPage.test.ts src/areas/home/recents.test.ts src/areas/workflows/workflowRunRecord.test.ts && node --test electron/main/*.test.mjs src/**/*.test.mjs", + "test:node": "node --test --experimental-strip-types --experimental-loader ./scripts/node-ts-extensionless-loader.mjs src/shared/types/assetLibrary.test.ts src/areas/generate/assetLibraryProjection.test.ts src/areas/generate/assetLibraryService.test.ts src/areas/generate/assetLibraryUi.test.ts src/areas/library/libraryView.test.ts src/areas/library/gridWindow.test.ts src/areas/generate/orcaSlicerLink.test.ts src/shared/utils/agentStream.test.ts src/shared/thumbnails/assetThumbnailQueue.test.ts src/shared/thumbnails/assetThumbnailStore.test.ts src/shared/components/agent/clientTools.test.ts src/shared/components/agent/agentContext.test.ts src/shared/utils/ollamaHealth.test.ts src/shared/utils/notification.test.ts src/shared/utils/agentConversations.test.ts electron/main/artifact-registry-service.test.ts electron/main/asset-thumbnail-cache.test.ts electron/main/extension-path-guard.test.ts electron/main/storage-directory-guard.test.ts electron/main/storage-ownership.test.ts electron/main/startup-error.test.ts electron/main/api-port.test.ts electron/main/agent-conversations.test.ts electron/main/gpu-memory.test.ts electron/preload/artifact-registry-preload.test.ts src/shared/stores/meshHistory.test.ts src/areas/generate/meshEditPanel.test.ts src/areas/generate/sculpt/SculptMesh.test.ts src/areas/generate/sculpt/SculptMesh.perf.test.ts src/areas/generate/sculpt/sculptLogic.test.ts src/shared/pages/pageRegistry.test.ts src/shared/pages/startPage.test.ts src/areas/home/recents.test.ts src/areas/workflows/workflowRunRecord.test.ts && node --test electron/main/*.test.mjs src/**/*.test.mjs", "package": "cross-env CSC_IDENTITY_AUTO_DISCOVERY=false npm run build && npm run prepare-resources && electron-builder", "package:mac": "cross-env CSC_IDENTITY_AUTO_DISCOVERY=false npm run build && npm run prepare-resources && electron-builder --mac --arm64", "lint": "eslint ." diff --git a/src/areas/settings/components/ApplicationSection.tsx b/src/areas/settings/components/ApplicationSection.tsx index 22583caf..52fa6a13 100644 --- a/src/areas/settings/components/ApplicationSection.tsx +++ b/src/areas/settings/components/ApplicationSection.tsx @@ -9,7 +9,7 @@ const START_PAGE_OPTIONS: { value: StartPage; label: string }[] = [ ] export function ApplicationSection(): JSX.Element { - const { showRamIndicator, setShowRamIndicator } = useAppStore() + const { showRamIndicator, setShowRamIndicator, showVramIndicator, setShowVramIndicator } = useAppStore() const [startPage, setStartPage] = useState(readStartPage) function changeStartPage(page: StartPage) { @@ -32,6 +32,12 @@ export function ApplicationSection(): JSX.Element { > + + + ) diff --git a/src/shared/components/agent/agentContext.test.ts b/src/shared/components/agent/agentContext.test.ts index d00ea575..f13031bb 100644 --- a/src/shared/components/agent/agentContext.test.ts +++ b/src/shared/components/agent/agentContext.test.ts @@ -32,7 +32,7 @@ const inputs = (over: Partial = {}): AgentContextInputs => ( processExtensions: [processExt], models: [{ id: 'triposr/main', name: 'TripoSR' }], storage: STORAGE, - ui: { uiScale: 'large', useAtkinsonFont: true, showRamIndicator: false }, + ui: { uiScale: 'large', useAtkinsonFont: true, showRamIndicator: false, showVramIndicator: true }, ...over, }) @@ -49,7 +49,7 @@ test('the context carries the page, mesh, workflows, extensions, models, storage ], models: [{ id: 'triposr/main', name: 'TripoSR' }], storage: STORAGE, - ui: { uiScale: 'large', useAtkinsonFont: true, showRamIndicator: false }, + ui: { uiScale: 'large', useAtkinsonFont: true, showRamIndicator: false, showVramIndicator: true }, }) }) @@ -67,7 +67,7 @@ test('storage keeps only the four folders, never the HF token', () => { test('buildAgentContext reads the stores and the async sources', async () => { useNavStore.setState({ currentPage: 'workflows' }) - useAppStore.setState({ currentJob: null, meshStats: null, uiScale: 'small', useAtkinsonFont: false, showRamIndicator: true }) + useAppStore.setState({ currentJob: null, meshStats: null, uiScale: 'small', useAtkinsonFont: false, showRamIndicator: true, showVramIndicator: false }) useWorkflowsStore.setState({ workflows: [{ id: 'w', name: 'W', description: '', nodes: [], edges: [], createdAt: '', updatedAt: '' }] }) useExtensionsStore.setState({ modelExtensions: [modelExt], processExtensions: [] }) @@ -83,6 +83,6 @@ test('buildAgentContext reads the stores and the async sources', async () => { workflows: [{ id: 'w', name: 'W' }], extensions: [{ id: 'triposr/main', name: 'MAIN', input: 'image', output: 'mesh' }], models: [{ id: 'triposr/main', name: 'TripoSR' }], - ui: { uiScale: 'small', useAtkinsonFont: false, showRamIndicator: true }, + ui: { uiScale: 'small', useAtkinsonFont: false, showRamIndicator: true, showVramIndicator: false }, }) }) diff --git a/src/shared/components/agent/agentContext.ts b/src/shared/components/agent/agentContext.ts index 79c078d9..ecf81db1 100644 --- a/src/shared/components/agent/agentContext.ts +++ b/src/shared/components/agent/agentContext.ts @@ -25,7 +25,7 @@ export interface AgentContextInputs { models: { id: string; name: string }[] | null /** Null when the settings could not be read. */ storage: { modelsDir: string; workspaceDir: string; workflowsDir: string; extensionsDir: string } | null - ui: { uiScale: UiScale; useAtkinsonFont: boolean; showRamIndicator: boolean } + ui: { uiScale: UiScale; useAtkinsonFont: boolean; showRamIndicator: boolean; showVramIndicator: boolean } } /** Pure assembly of the context from already-read state. */ @@ -44,7 +44,7 @@ export function assembleAgentContext(inputs: AgentContextInputs): Record @@ -65,7 +65,7 @@ function fake(overrides: Partial = {}): Fake { const rec = (...call: unknown[]) => { calls.push(call) } const ok = { success: true } const state = { - ui: { uiScale: 'medium' as UiScale, useAtkinsonFont: false, showRamIndicator: true }, + ui: { uiScale: 'medium' as UiScale, useAtkinsonFont: false, showRamIndicator: true, showVramIndicator: true }, dirs: { modelsDir: 'C:\\Modly\\models', workspaceDir: 'C:\\Modly\\workspace', workflowsDir: 'C:\\Modly\\workflows', extensionsDir: 'C:\\Modly\\extensions' }, history: { undo: 1, redo: 1 }, dirItems: {} as Record, @@ -101,6 +101,7 @@ function fake(overrides: Partial = {}): Fake { setUiScale: (v) => { state.ui.uiScale = v }, setUseAtkinsonFont: (v) => { state.ui.useAtkinsonFont = v }, setShowRamIndicator: (v) => { state.ui.showRamIndicator = v }, + setShowVramIndicator: (v) => { state.ui.showVramIndicator = v }, }, storage: { get: async () => state.dirs, @@ -260,7 +261,8 @@ test('set_setting sets each UI option and validates key and value', async () => assert.equal(await run('set_setting', { key: 'uiScale', value: 'large' }, f), 'Set the interface scale to "large".') assert.equal(await run('set_setting', { key: 'useAtkinsonFont', value: true }, f), 'Set the Atkinson Hyperlegible font to true.') assert.equal(await run('set_setting', { key: 'showRamIndicator', value: 'false' }, f), 'Set the RAM indicator to false.') - assert.deepEqual(f.ui, { uiScale: 'large', useAtkinsonFont: true, showRamIndicator: false }) + assert.equal(await run('set_setting', { key: 'showVramIndicator', value: false }, f), 'Set the VRAM indicator to false.') + assert.deepEqual(f.ui, { uiScale: 'large', useAtkinsonFont: true, showRamIndicator: false, showVramIndicator: false }) assert.match(await run('set_setting', { key: 'uiScale', value: 'huge' }, f), /^Error: uiScale must be one of: small, medium, large, very-large/) assert.match(await run('set_setting', { key: 'showRamIndicator', value: 1 }, f), /^Error: showRamIndicator must be true or false/) diff --git a/src/shared/components/agent/clientTools.ts b/src/shared/components/agent/clientTools.ts index 7be3c55f..c5645af7 100644 --- a/src/shared/components/agent/clientTools.ts +++ b/src/shared/components/agent/clientTools.ts @@ -33,6 +33,7 @@ export interface UiSettings { uiScale: UiScale useAtkinsonFont: boolean showRamIndicator: boolean + showVramIndicator: boolean } export interface ToolResultBody { @@ -69,6 +70,7 @@ export interface ClientToolDeps { setUiScale: (value: UiScale) => void setUseAtkinsonFont: (value: boolean) => void setShowRamIndicator: (value: boolean) => void + setShowVramIndicator: (value: boolean) => void } storage: { get: () => Promise @@ -454,7 +456,8 @@ export const clientTools: Record = { const key = args.key as keyof UiSettings if (key === 'uiScale') deps.ui.setUiScale(args.value as UiScale) else if (key === 'useAtkinsonFont') deps.ui.setUseAtkinsonFont(toBool(args.value)!) - else deps.ui.setShowRamIndicator(toBool(args.value)!) + else if (key === 'showRamIndicator') deps.ui.setShowRamIndicator(toBool(args.value)!) + else deps.ui.setShowVramIndicator(toBool(args.value)!) return `Set ${SETTING_LABELS[key]} to ${JSON.stringify(deps.ui.get()[key])}.` }, }), @@ -548,6 +551,7 @@ const SETTING_LABELS: Record = { uiScale: 'the interface scale', useAtkinsonFont: 'the Atkinson Hyperlegible font', showRamIndicator: 'the RAM indicator', + showVramIndicator: 'the VRAM indicator', } const UI_SCALES: UiScale[] = ['small', 'medium', 'large', 'very-large'] @@ -752,10 +756,11 @@ export function createClientToolDeps(): ClientToolDeps { reload: () => wfs().load(), }, ui: { - get: () => ({ uiScale: app().uiScale, useAtkinsonFont: app().useAtkinsonFont, showRamIndicator: app().showRamIndicator }), + get: () => ({ uiScale: app().uiScale, useAtkinsonFont: app().useAtkinsonFont, showRamIndicator: app().showRamIndicator, showVramIndicator: app().showVramIndicator }), setUiScale: (v) => app().setUiScale(v), setUseAtkinsonFont: (v) => app().setUseAtkinsonFont(v), setShowRamIndicator: (v) => app().setShowRamIndicator(v), + setShowVramIndicator: (v) => app().setShowVramIndicator(v), }, storage: { get: () => window.electron.settings.get(), diff --git a/src/shared/components/layout/MemoryIndicator.tsx b/src/shared/components/layout/MemoryIndicator.tsx index fe09eac1..e200a389 100644 --- a/src/shared/components/layout/MemoryIndicator.tsx +++ b/src/shared/components/layout/MemoryIndicator.tsx @@ -1,57 +1,73 @@ import { useEffect, useState } from 'react' +import { useAppStore } from '@shared/stores/appStore' const GB = 1024 ** 3 +const RAM_INTERVAL_MS = 2000 +const VRAM_INTERVAL_MS = 2000 + +interface MemorySample { + total: number + used: number + available: number +} function fmtGB(bytes: number): string { return (bytes / GB).toFixed(1) } -export default function MemoryIndicator(): JSX.Element | null { - const [mem, setMem] = useState<{ total: number; used: number; available: number } | null>(null) +function getBarColors(pct: number): { barColor: string; textColor: string } { + if (pct >= 90) return { barColor: 'bg-red-500', textColor: 'text-red-300' } + if (pct >= 75) return { barColor: 'bg-amber-500', textColor: 'text-amber-300' } + return { barColor: 'bg-emerald-500', textColor: 'text-zinc-300' } +} + +/** + * Polls `sample` every `intervalMs` while `enabled`, skipping a tick while the + * previous call is still running. A null or failed sample keeps the last value, + * so a transient miss does not flicker the bar away. Disabling clears the value. + */ +function useSampler(sample: () => Promise, intervalMs: number, enabled: boolean): T | null { + const [value, setValue] = useState(null) useEffect(() => { + if (!enabled) { + setValue(null) + return + } let cancelled = false + let inFlight = false const tick = async () => { + if (inFlight) return + inFlight = true try { - const next = await window.electron.system.memory() - if (!cancelled) setMem(next) + const next = await sample() + if (!cancelled && next) setValue(next) } catch { // Renderer should not break if memory sampling fails. + } finally { + inFlight = false } } tick() - const id = setInterval(tick, 2000) + const id = setInterval(tick, intervalMs) return () => { cancelled = true clearInterval(id) } - }, []) + }, [sample, intervalMs, enabled]) - if (!mem) return null - - const pct = mem.total > 0 ? Math.min(100, Math.round((mem.used / mem.total) * 100)) : 0 - - let barColor = 'bg-emerald-500' - let textColor = 'text-zinc-300' - if (pct >= 90) { - barColor = 'bg-red-500' - textColor = 'text-red-300' - } else if (pct >= 75) { - barColor = 'bg-amber-500' - textColor = 'text-amber-300' - } + return value +} - const tooltip = - `Used: ${fmtGB(mem.used)} GB\n` + - `Available: ${fmtGB(mem.available)} GB\n` + - `Total: ${fmtGB(mem.total)} GB` +const sampleRam = () => window.electron.system.memory() +const sampleVram = () => window.electron.system.gpuMemory() +function MemoryBar({ label, sample, tooltip }: { label: string; sample: MemorySample; tooltip: string }): JSX.Element { + const pct = sample.total > 0 ? Math.min(100, Math.round((sample.used / sample.total) * 100)) : 0 + const { barColor, textColor } = getBarColors(pct) return ( -
- RAM +
+ {label}
- {fmtGB(mem.used)} / {fmtGB(mem.total)} GB + {fmtGB(sample.used)} / {fmtGB(sample.total)} GB
) } + +function tooltipFor(heading: string, sample: MemorySample): string { + return ( + `${heading}\n` + + ` Used: ${fmtGB(sample.used)} GB\n` + + ` Available: ${fmtGB(sample.available)} GB\n` + + ` Total: ${fmtGB(sample.total)} GB` + ) +} + +export default function MemoryIndicator(): JSX.Element | null { + const isMac = useAppStore(s => s.platform === 'darwin') + const showRam = useAppStore(s => s.showRamIndicator) + const showVram = useAppStore(s => s.showVramIndicator) && !isMac + + const ram = useSampler(sampleRam, RAM_INTERVAL_MS, showRam) + const vram = useSampler(sampleVram, VRAM_INTERVAL_MS, showVram) + + const ramBar = showRam && ram ? ram : null + const vramBar = showVram && vram && vram.total > 0 ? vram : null + if (!ramBar && !vramBar) return null + + return ( +
+ {ramBar && } + {vramBar && ( + + )} +
+ ) +} diff --git a/src/shared/components/layout/TopBar.tsx b/src/shared/components/layout/TopBar.tsx index 056ddf8a..102b99c0 100644 --- a/src/shared/components/layout/TopBar.tsx +++ b/src/shared/components/layout/TopBar.tsx @@ -4,7 +4,7 @@ import { useAgentPanelStore } from '@shared/stores/agentPanelStore' import MemoryIndicator from './MemoryIndicator' export default function TopBar(): JSX.Element { - const { patchUpdateReady, platform, showRamIndicator } = useAppStore() + const { patchUpdateReady, platform, showRamIndicator, showVramIndicator } = useAppStore() const isMac = platform === 'darwin' const agentOpen = useAgentPanelStore((s) => s.open) const toggleAgent = useAgentPanelStore((s) => s.toggle) @@ -46,7 +46,7 @@ export default function TopBar(): JSX.Element {
{/* Memory indicator */} - {showRamIndicator && } + {(showRamIndicator || showVramIndicator) && } {/* Agent panel toggle */}