Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ public/codemirror-bundle.js
.env
.cache/
dev-app-update.yml
/.idea/.gitignore
/.idea/misc.xml
/.idea/modules.xml
/.idea/switchboard.iml
/.idea/vcs.xml
30 changes: 30 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,36 @@ ipcMain.handle('read-session-jsonl', (_event, sessionId) => {
}
});

ipcMain.handle('get-session-tokens', (_event, sessionId) => {
const folder = getCachedFolder(sessionId);
if (!folder) return null;
const jsonlPath = path.join(PROJECTS_DIR, folder, sessionId + '.jsonl');
try {
const stat = fs.statSync(jsonlPath);
const readSize = Math.min(stat.size, 32768);
const buf = Buffer.alloc(readSize);
const fd = fs.openSync(jsonlPath, 'r');
fs.readSync(fd, buf, 0, readSize, stat.size - readSize);
fs.closeSync(fd);
const tail = buf.toString('utf-8');
const lines = tail.split('\n').filter(Boolean).reverse();
for (const line of lines) {
try {
const entry = JSON.parse(line);
const u = entry.message?.usage;
if (u && (entry.type === 'assistant' || entry.message?.role === 'assistant')) {
const contextTokens = (u.input_tokens || 0) + (u.cache_read_input_tokens || 0) + (u.cache_creation_input_tokens || 0);
const model = entry.message?.model || entry.model || '';
return { contextTokens, model };
}
} catch {}
}
return null;
} catch {
return null;
}
});

ipcMain.handle('archive-session', (_event, sessionId, archived) => {
const val = archived ? 1 : 0;
setArchived(sessionId, val);
Expand Down
1 change: 1 addition & 0 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ contextBridge.exposeInMainWorld('api', {
openTerminal: (id, projectPath, isNew, sessionOptions) => ipcRenderer.invoke('open-terminal', id, projectPath, isNew, sessionOptions),
search: (type, query, titleOnly) => ipcRenderer.invoke('search', type, query, titleOnly),
readSessionJsonl: (sessionId) => ipcRenderer.invoke('read-session-jsonl', sessionId),
getSessionTokens: (sessionId) => ipcRenderer.invoke('get-session-tokens', sessionId),

// Settings
getSetting: (key) => ipcRenderer.invoke('get-setting', key),
Expand Down
44 changes: 44 additions & 0 deletions public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function setActiveSession(id) {
else sessionStorage.removeItem('activeSessionId');
// Update file panel to show this session's open files/diffs
if (typeof switchPanel === 'function') switchPanel(id);
refreshCtxGauge(id);
}
// Persist slug group expand state across reloads
function getExpandedSlugs() {
Expand Down Expand Up @@ -341,6 +342,7 @@ window.api.onTerminalNotification((sessionId, message) => {
// --- CLI busy state (OSC 0 title spinner detection) ---
window.api.onCliBusyState((sessionId, busy) => {
setActivity(sessionId, busy);
if (!busy && sessionId === activeSessionId) refreshCtxGauge(sessionId);
});

// --- Single entry point for all sidebar renders ---
Expand Down Expand Up @@ -1126,5 +1128,47 @@ const updaterHandler = (type, data) => {
};
window.api.onUpdaterEvent(updaterHandler);

// --- Context window gauge in status bar ---
const ctxGaugeEl = document.getElementById('status-bar-ctx');
const CTX_MAX = 200000;
function fmtTokens(n) { return n >= 1000 ? Math.round(n / 1000) + 'K' : String(n); }
async function refreshCtxGauge(sessionId) {
if (!sessionId) { ctxGaugeEl.style.display = 'none'; return; }
try {
const result = await window.api.getSessionTokens(sessionId);
if (!result) { ctxGaugeEl.style.display = 'none'; return; }
const { contextTokens } = result;
const pct = Math.round((contextTokens / CTX_MAX) * 100);
ctxGaugeEl.style.display = '';
ctxGaugeEl.title = `Context : ${fmtTokens(contextTokens)} / ${fmtTokens(CTX_MAX)} tokens (${pct}%)`;
const fill = ctxGaugeEl.querySelector('.ctx-fill');
fill.style.width = Math.min(Math.max(pct, 1), 100) + '%';
fill.className = 'ctx-fill' + (pct >= 80 ? ' ctx-high' : pct >= 60 ? ' ctx-mid' : '');
ctxGaugeEl.querySelector('.ctx-text').textContent = fmtTokens(contextTokens) + ' / 200K';
} catch {}
}

// --- Quota gauge (5h session) in status bar ---
const quotaGaugeEl = document.getElementById('status-bar-quota');
async function refreshQuotaGauge() {
try {
const usage = await window.api.getUsage();
const pct = usage?.session;
const reset = usage?.sessionReset;
if (pct === undefined) { quotaGaugeEl.style.display = 'none'; return; }
quotaGaugeEl.style.display = '';
quotaGaugeEl.title = `5h quota : ${pct}%${reset ? ' — Resets ' + reset : ''}`;
const fill = quotaGaugeEl.querySelector('.quota-fill');
fill.style.width = Math.max(pct, 1) + '%';
fill.className = 'quota-fill' + (pct >= 80 ? ' quota-high' : pct >= 60 ? ' quota-mid' : '');
quotaGaugeEl.querySelector('.quota-pct').textContent = pct + '%';
} catch {}
}
refreshQuotaGauge();
setInterval(refreshQuotaGauge, 5 * 60 * 1000);
quotaGaugeEl.addEventListener('click', () => {
document.querySelector('.sidebar-tab[data-tab="stats"]')?.click();
});

// --- Initialize file panel (MCP bridge UI) ---
if (typeof initFilePanel === 'function') initFilePanel();
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<button id="update-restart-btn">Restart</button>
<button id="update-dismiss-btn">Later</button>
</div>
<div id="status-bar"><span id="status-bar-info"></span><span id="status-bar-activity"></span><span id="status-bar-updater"></span></div>
<div id="status-bar"><span id="status-bar-info"></span><span id="status-bar-quota" style="display:none;"><span class="quota-track"><span class="quota-fill"></span></span><span class="quota-pct"></span></span><span id="status-bar-ctx" style="display:none;"><span class="ctx-track"><span class="ctx-fill"></span></span><span class="ctx-text"></span></span><span id="status-bar-activity"></span><span id="status-bar-updater"></span></div>
<script src="../node_modules/@xterm/xterm/lib/xterm.js"></script>
<script src="../node_modules/@xterm/addon-fit/lib/addon-fit.js"></script>
<script src="../node_modules/@xterm/addon-web-links/lib/addon-web-links.js"></script>
Expand Down
67 changes: 66 additions & 1 deletion public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ body { display: flex; flex-direction: column; }
}

#status-bar-activity {
margin-left: auto;
white-space: nowrap;
color: #8088ff;
}
Expand All @@ -113,6 +112,72 @@ body { display: flex; flex-direction: column; }
font-style: italic;
}

#status-bar-quota {
display: flex;
align-items: center;
gap: 5px;
cursor: pointer;
opacity: 0.8;
flex-shrink: 0;
margin-left: auto;
transition: opacity 0.15s;
}
#status-bar-quota:hover { opacity: 1; }
.quota-track {
width: 48px;
height: 4px;
background: rgba(255,255,255,0.12);
border-radius: 2px;
overflow: hidden;
flex-shrink: 0;
}
.quota-fill {
display: block;
height: 100%;
background: #3ecf5a;
border-radius: 2px;
transition: width 0.4s ease;
}
.quota-fill.quota-mid { background: #e8a030; }
.quota-fill.quota-high { background: #e05070; }
.quota-pct {
font-size: 10px;
color: #7a7a90;
white-space: nowrap;
}

#status-bar-ctx {
display: flex;
align-items: center;
gap: 5px;
flex-shrink: 0;
opacity: 0.8;
transition: opacity 0.15s;
}
#status-bar-ctx:hover { opacity: 1; }
.ctx-track {
width: 48px;
height: 4px;
background: rgba(255,255,255,0.12);
border-radius: 2px;
overflow: hidden;
flex-shrink: 0;
}
.ctx-fill {
display: block;
height: 100%;
background: #6a9fd8;
border-radius: 2px;
transition: width 0.4s ease;
}
.ctx-fill.ctx-mid { background: #e8a030; }
.ctx-fill.ctx-high { background: #e05070; }
.ctx-text {
font-size: 10px;
color: #7a7a90;
white-space: nowrap;
}

/* ========== SIDEBAR ========== */
#sidebar {
width: 340px;
Expand Down