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
1 change: 1 addition & 0 deletions shell/plugins/agents/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ Item {
return {
providerId: String(record.id),
providerName: String(record.name || record.id),
updatedAt: String(record.updatedAt || (synced && aggregateData ? aggregateData.updatedAt : "") || ""),
ready: record.ready === true || synced,
usageStatusText: String(record.usageStatusText || ""),
authHelpText: String(record.authHelpText || ""),
Expand Down
71 changes: 66 additions & 5 deletions shell/plugins/agents/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Panel {

property bool cursorActive: false

// Countdowns and "updated" read this instead of Date.now() so the
// panel keeps telling the truth while it sits open.
// Countdowns and freshness read this instead of Date.now() so the
// panel and bar keep telling the truth while the shell runs.
property double nowMs: Date.now()

readonly property var limits: limitWindows(provider)
Expand All @@ -45,6 +45,8 @@ Panel {
readonly property bool balanceAlarming: !!balance && balance.funded > 0
&& balance.remaining / balance.funded <= 0.1
readonly property bool alarming: (!!headline && headline.percent >= 0.9) || balanceAlarming
readonly property int lowRemainingPercent: Math.max(1, Math.min(50, Number(setting("lowRemainingPercent", 10)) || 10))
property var notifiedProviders: ({})

function clamp(v, lo, hi) { return Math.max(lo, Math.min(hi, v)) }
function alpha(c, a) { return Qt.rgba(c.r, c.g, c.b, a) }
Expand Down Expand Up @@ -167,6 +169,54 @@ Panel {
return currencyPrefix(currency) + amount.toFixed(2)
}

function remainingPercent(w) {
return w ? Math.max(0, Math.min(100, Math.round((1 - w.percent) * 100))) : -1
}

function updatedAgeMs(p) {
var ms = p && p.updatedAt ? new Date(p.updatedAt).getTime() : NaN
return isFinite(ms) ? Math.max(0, nowMs - ms) : -1
}

function isStale(p) {
var age = updatedAgeMs(p)
return age < 0 || age > Math.max(1800000, usage.refreshIntervalSec * 2000)
}

function freshnessText(p) {
if (!p) return ""
var age = updatedAgeMs(p)
if (age < 0) return "Last update unknown"
var minutes = Math.floor(age / 60000)
var when = minutes < 1 ? "just now" : minutes < 60 ? minutes + "m ago"
: Math.floor(minutes / 60) + "h ago"
return (isStale(p) ? "Usage may be stale · " : "Updated ") + when
}

function checkLowAllowance() {
var next = Object.assign({}, notifiedProviders)
var changed = false
for (var i = 0; i < providers.length; i++) {
var p = providers[i]
var w = bindingWindow(p)
var b = p.balance
if ((!w && !(b && b.funded > 0)) || isStale(p) || p.usageStatusText !== "") continue
var left = w ? remainingPercent(w) : Math.max(0, Math.min(100, Math.round(b.remaining / b.funded * 100)))
if (left <= lowRemainingPercent && !next[p.providerId]) {
Quickshell.execDetached(["omarchy-notification-send", "--app-name", "Agent Usage",
"-u", "normal", p.providerName + " allowance low",
w ? left + "% left in " + w.title + (w.resetAt ? " · resets in " + formatDuration(resetMsFor(w)) : "")
: formatMoney(b.remaining, b.currency) + " prepaid credit left"])
next[p.providerId] = true
changed = true
} else if (left > lowRemainingPercent && next[p.providerId]) {
delete next[p.providerId]
changed = true
}
}
if (changed) notifiedProviders = next
}

function balanceDetailText(b) {
if (!b || !(b.funded > 0)) return ""
var text = formatMoney(b.spent, b.currency) + " spent of " + formatMoney(b.funded, b.currency) + " funded"
Expand Down Expand Up @@ -302,6 +352,7 @@ Panel {
implicitHeight: button.implicitHeight

onProviderIndexChanged: if (panelFlick) panelFlick.contentY = 0
onProvidersChanged: Qt.callLater(checkLowAllowance)
onOpenedChanged: if (opened) {
cursorActive = false
nowMs = Date.now()
Expand All @@ -315,11 +366,10 @@ Panel {
settings: root.settings
}

// Cheap enough to keep running: it only re-evaluates text bindings, and a
// stale "resets in 2h" on a panel that is open is worse than a timer.
// Keep reset countdowns and data freshness current in both the panel and bar.
Timer {
interval: 30000
running: root.opened
running: true
repeat: true
onTriggered: root.nowMs = Date.now()
}
Expand All @@ -341,6 +391,7 @@ Panel {
bar: root.bar
text: "󱚣"
active: root.alarming
tooltipText: root.provider ? root.provider.providerName + " · " + root.freshnessText(root.provider) : ""
onPressed: function(buttonCode) {
if (buttonCode === Qt.RightButton) root.launchAgent()
else if (buttonCode === Qt.MiddleButton) root.selectProvider(root.providerIndex + 1)
Expand Down Expand Up @@ -446,6 +497,16 @@ Panel {
}
}

Text {
textFormat: Text.PlainText
visible: !!root.provider
width: parent.width
text: root.freshnessText(root.provider)
color: root.isStale(root.provider) ? root.urgent : root.dim
font.family: root.fontFamily
font.pixelSize: Style.font.caption
}

Text {
visible: root.providers.length === 0
width: parent.width
Expand Down
2 changes: 2 additions & 0 deletions shell/plugins/agents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cross-device aggregation); `Agent.qml` is the per-record file watcher.

- **Hero** — the mark, the tool, and the plan it runs on ("Max 20x", "Pro").
Auth and endpoint problems replace the plan line and repeat in a card.
- **Freshness** — shows when the selected agent's usage record last updated and warns if it is older than two refresh intervals (at least 30 minutes).
- **Subscription switch** — one chip per enabled agent (`h`/`l` or click).
It appears only when more than one agent is enabled.
- **Limits** — the percentage of each allowance used, a matching meter, and
Expand Down Expand Up @@ -108,6 +109,7 @@ top-level keys can be set with
| Key | Default | What it does |
|---|---|---|
| `refreshIntervalSec` | `900` | How often the usage records regenerate |
| `lowRemainingPercent` | `10` | Alert once per shell session when a fresh rate limit or funded prepaid balance falls to this percentage or below; rearm after recovery |
| `syncMode` | `"Off"` | `"On"` writes this machine's snapshot and merges the others |
| `syncDir` | `""` | A folder synced by Syncthing, Dropbox, rsync, … |
| `syncFileName` | `<hostname>.json` | This machine's snapshot file |
Expand Down
4 changes: 3 additions & 1 deletion shell/plugins/agents/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "1.0.0",
"author": "Omarchy",
"license": "MIT",
"description": "Claude Code, Codex, and Fireworks usage, limits, and pace in a native Omarchy bar panel.",
"description": "Claude Code, Codex, and Fireworks usage with freshness status and low-allowance alerts.",
"kinds": ["bar-widget"],
"activation": "on-demand",
"entryPoints": {
Expand All @@ -24,12 +24,14 @@
"fireworks": { "enabled": true }
},
"refreshIntervalSec": 900,
"lowRemainingPercent": 10,
"syncMode": "Off",
"syncDir": "",
"syncFileName": "",
"syncDeviceId": ""
},
"schema": [
{ "key": "lowRemainingPercent", "type": "integer", "label": "Low allowance alert (%)", "min": 1, "max": 50, "step": 1, "defaultValue": 10 },
{ "key": "refreshIntervalSec", "type": "integer", "label": "Refresh interval (seconds)", "min": 30, "max": 3600, "step": 30, "defaultValue": 900 },
{ "key": "syncMode", "type": "enum", "label": "Synced aggregation", "options": ["Off", "On"], "defaultValue": "Off", "description": "When On, write this machine's local usage snapshot and merge snapshots from other machines." },
{ "key": "syncDir", "type": "path", "label": "Sync folder", "defaultValue": "", "description": "A folder synced by Syncthing, Dropbox, rsync, etc." },
Expand Down
20 changes: 20 additions & 0 deletions test/shell.d/agents-panel-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,31 @@ source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"
run_node_test <<'JS'
const fs = require('fs')
const panelSource = fs.readFileSync(root + '/shell/plugins/agents/Panel.qml', 'utf8')
const manifest = JSON.parse(fs.readFileSync(root + '/shell/plugins/agents/manifest.json', 'utf8'))

assert(/function launchAgent\(\)/.test(panelSource), 'agents panel launches the default agent')
assert(/root\.bar\.run\("omarchy-agent --pick"\)/.test(panelSource), 'agents panel uses the desktop agent launcher')
assert(/if \(buttonCode === Qt\.RightButton\) root\.launchAgent\(\)/.test(panelSource), 'agents right click launches the agent')
assert(/else if \(buttonCode === Qt\.MiddleButton\) root\.selectProvider\(root\.providerIndex \+ 1\)/.test(panelSource), 'agents middle click still advances the subscription')
assert(/else root\.toggle\(\)/.test(panelSource), 'agents left click still toggles the panel')
assert(!/if \(buttonCode === Qt\.RightButton\) root\.refreshNow\(\)/.test(panelSource), 'agents right click no longer refreshes')

function panelFunction(name, args) {
const body = panelSource.match(new RegExp('function ' + name + '\\([^)]*\\) \\{([^}]+)\\}'))?.[1]
assert(body, 'agents define ' + name)
return new Function(...args, body)
}

const remainingPercent = panelFunction('remainingPercent', ['w'])
assertEqual(remainingPercent({ percent: 0.61 }), 39, 'limit alert uses allowance remaining')
assertEqual(remainingPercent({ percent: 1.2 }), 0, 'remaining allowance clamps at zero')

const nowMs = Date.parse('2026-09-24T12:00:00Z')
const updatedAgeMs = panelFunction('updatedAgeMs', ['p', 'nowMs'])
const age = p => updatedAgeMs(p, nowMs)
const isStale = panelFunction('isStale', ['p', 'updatedAgeMs', 'usage'])
assertEqual(isStale({ updatedAt: '2026-09-24T11:45:00Z' }, age, { refreshIntervalSec: 900 }), false, 'recent usage remains fresh')
assertEqual(isStale({ updatedAt: '2026-09-24T11:29:00Z' }, age, { refreshIntervalSec: 900 }), true, 'old usage is marked stale')
assertEqual(isStale({}, age, { refreshIntervalSec: 900 }), true, 'missing update time is marked stale')
assertEqual(manifest.barWidget.defaults.lowRemainingPercent, 10, 'alert threshold has a default')
JS